From 228ccc7d0496e1b6872416fbaab882ccb182f9d7 Mon Sep 17 00:00:00 2001 From: Alexandertp Date: Wed, 20 Dec 2023 21:07:09 +0100 Subject: [PATCH] Separate state-handling and motor code into separate files, fix conflict between motor and display --- arduino/arduino.ino | 102 ++++++++------------------------------------ arduino/motor.h | 2 + arduino/motor.ino | 42 ++++++++++++++++++ arduino/state.h | 9 ++++ arduino/state.ino | 37 ++++++++++++++++ 5 files changed, 108 insertions(+), 84 deletions(-) create mode 100644 arduino/motor.h create mode 100644 arduino/motor.ino create mode 100644 arduino/state.h create mode 100644 arduino/state.ino diff --git a/arduino/arduino.ino b/arduino/arduino.ino index d0563b1..295fa01 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -1,51 +1,37 @@ #include #include #include - +#include "state.h" +#include "motor.h" #include "secrets.h" MKRIoTCarrier carrier; WiFiSSLClient wifi_client; MqttClient mqtt_client(wifi_client); -// Motor pins -int motor_pin1 = 8; // Blue - 28BYJ48 pin 1 -int motor_pin2 = 9; // Pink - 28BYJ48 pin 2 -int motor_pin3 = 10; // Yellow - 28BYJ48 pin 3 -int motor_pin4 = 11; // Orange - 28BYJ48 pin 4 - -// Motor variables -int motor_speed = 1200; +//motor control variables int motor_count = 0; int motor_count_max = 64; -int motor_lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001}; -uint32_t green_led_color = carrier.leds.Color(0,255,0); +uint32_t green_led_color = carrier.leds.Color(0, 255, 0); -enum state { - STATE_CONNECTING, - STATE_LOGGED_OUT, - STATE_LOGGED_IN, - STATE_INPUT_PASSCODE, -}; -enum state current_state; - -void setup(void) +void setup() { - current_state = STATE_CONNECTING; Serial.begin(9600); Serial.println("Started"); + carrier.noCase(); carrier.begin(); + carrier.display.init(240, 240); + motor_setup(); + pinMode(TFT_BACKLIGHT, OUTPUT); digitalWrite(TFT_BACKLIGHT, HIGH); - carrier.display.setCursor(20, 100); - carrier.display.setTextSize(3); - carrier.display.print("Connecting.."); + change_state(STATE_CONNECTING); Serial.println("Connecting to WiFi..."); @@ -65,32 +51,17 @@ void setup(void) Serial.println("Connected to MQTT"); + carrier.Buzzer.beep(); + mqtt_client.subscribe("dispense"); - carrier.display.fillScreen(0x000); - - current_state = STATE_LOGGED_OUT; - carrier.display.setCursor(15, 100); - carrier.display.setTextSize(2); - carrier.display.print("Press Green Button"); - carrier.leds.begin(); - carrier.leds.fill(green_led_color, 2, 1); - carrier.leds.setBrightness(5); - carrier.leds.show(); - - carrier.Buttons.begin(); - // Pinouts for motor - DO NOT ADD CODE AFTER THIS - pinMode(motor_pin1, OUTPUT); - pinMode(motor_pin2, OUTPUT); - pinMode(motor_pin3, OUTPUT); - pinMode(motor_pin4, OUTPUT); - //do NOT add code after this. DON'T DO IT. + change_state(STATE_LOGGED_OUT); } -void loop(void) +void loop() { - carrier.Buttons.update(); - + carrier.Buttons.update(); + // Rotate motor if needed if (motor_count) { motor_clockwise(); @@ -111,44 +82,7 @@ void loop(void) } } - switch (current_state) { - case STATE_LOGGED_OUT: - if(carrier.Buttons.onTouchDown(TOUCH2)) { - current_state = INPUT_PASSCODE; - } - break; - - default: - break; - }; + loop_state(); -} - - - - - - -//motorgøgl, pls dont touch -void motor_clockwise() -{ - for (int i = 7; i >= 0; i--) { - motor_set_output(i); - delayMicroseconds(motor_speed); - } -} -void motor_anticlockwise() -{ - for (int i = 0; i < 8; i++) { - motor_set_output(i); - delayMicroseconds(motor_speed); - } -} - -void motor_set_output(int out) -{ - digitalWrite(motor_pin1, bitRead(motor_lookup[out], 0)); - digitalWrite(motor_pin2, bitRead(motor_lookup[out], 1)); - digitalWrite(motor_pin3, bitRead(motor_lookup[out], 2)); - digitalWrite(motor_pin4, bitRead(motor_lookup[out], 3)); + delay(50); } diff --git a/arduino/motor.h b/arduino/motor.h new file mode 100644 index 0000000..8ad2b7d --- /dev/null +++ b/arduino/motor.h @@ -0,0 +1,2 @@ +void motor_anticlockwise(); +void motor_setup(); \ No newline at end of file diff --git a/arduino/motor.ino b/arduino/motor.ino new file mode 100644 index 0000000..c2a90da --- /dev/null +++ b/arduino/motor.ino @@ -0,0 +1,42 @@ +//motorgøgl, pls dont touch +int motor_speed = 1200; +int motor_lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001}; +// Motor pins +// Pins 8 and 9 conflict with the display +int motor_pin1 = 10; // Blue - 28BYJ48 pin 1 +int motor_pin2 = 11; // Pink - 28BYJ48 pin 2 +int motor_pin3 = 12; // Yellow - 28BYJ48 pin 3 +int motor_pin4 = 13; // Orange - 28BYJ48 pin 4 + + +void motor_setup() +{ + pinMode(motor_pin1, OUTPUT); + pinMode(motor_pin2, OUTPUT); + pinMode(motor_pin3, OUTPUT); + pinMode(motor_pin4, OUTPUT); +} + +void motor_clockwise() +{ + for (int i = 7; i >= 0; i--) { + motor_set_output(i); + delayMicroseconds(motor_speed); + } +} + +void motor_anticlockwise() +{ + for (int i = 0; i < 8; i++) { + motor_set_output(i); + delayMicroseconds(motor_speed); + } +} + +void motor_set_output(int out) +{ + digitalWrite(motor_pin1, bitRead(motor_lookup[out], 0)); + digitalWrite(motor_pin2, bitRead(motor_lookup[out], 1)); + digitalWrite(motor_pin3, bitRead(motor_lookup[out], 2)); + digitalWrite(motor_pin4, bitRead(motor_lookup[out], 3)); +} diff --git a/arduino/state.h b/arduino/state.h new file mode 100644 index 0000000..ba969f4 --- /dev/null +++ b/arduino/state.h @@ -0,0 +1,9 @@ +enum state { + STATE_CONNECTING, + STATE_LOGGED_OUT, + STATE_LOGGED_IN, + STATE_INPUT_PASSCODE, +}; + +void change_state(enum state new_state); +void loop_state(); \ No newline at end of file diff --git a/arduino/state.ino b/arduino/state.ino new file mode 100644 index 0000000..fcbc7ba --- /dev/null +++ b/arduino/state.ino @@ -0,0 +1,37 @@ +enum state current_state; + +void change_state(enum state new_state) +{ + carrier.display.fillScreen(0x000); + + switch (new_state) { + case STATE_CONNECTING: + carrier.display.setCursor(20, 100); + carrier.display.setTextSize(3); + carrier.display.print("Connecting.."); + break; + + case STATE_LOGGED_OUT: + carrier.display.setCursor(15, 100); + carrier.display.setTextSize(2); + carrier.display.print("Press Green Button"); + carrier.leds.fill(green_led_color, 2, 1); + carrier.leds.setBrightness(5); + carrier.leds.show(); + break; + case STATE_INPUT_PASSCODE: + break; + } + current_state = new_state; +} + +void loop_state() +{ + switch (current_state) { + case STATE_LOGGED_OUT: + if (carrier.Buttons.onTouchDown(TOUCH2)) { + change_state(STATE_INPUT_PASSCODE); + } + break; + }; +} \ No newline at end of file