Separate state-handling and motor code into separate files, fix conflict between motor and display

This commit is contained in:
Alexandertp 2023-12-20 21:07:09 +01:00
parent 6cf254fa55
commit 228ccc7d04
5 changed files with 108 additions and 84 deletions

View File

@ -1,51 +1,37 @@
#include <Arduino_MKRIoTCarrier.h>
#include <WiFiNINA.h>
#include <ArduinoMqttClient.h>
#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,31 +51,16 @@ 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) {
@ -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);
}

2
arduino/motor.h Normal file
View File

@ -0,0 +1,2 @@
void motor_anticlockwise();
void motor_setup();

42
arduino/motor.ino Normal file
View File

@ -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));
}

9
arduino/state.h Normal file
View File

@ -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();

37
arduino/state.ino Normal file
View File

@ -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;
};
}