Hey guys. I had most of the elements of this code working in a test but I've recently broken something. I cant for the life of me work out what's wrong with it, but I've tested the hardware on other code and it's all working fine. I can turn on the bluetoothcontrolenabled via bluetooth commands however it doesn't print anything to the lcd screen anymore and the buttons don't work anymore. I'm pretty sure it's something I've done with the switch case but i'm suffering from a melted brain right now.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
// Pin definitions
const int sensorpin = 2;
const int solenoid1pin = 12;
const int solenoid2pin = 17;
const int solenoid3pin = 8;
const int buzzerpin = 9;
const int button1 = 6;
const int button2 = 5;
const int button3 = 4;
const int button4 = 3;
const int button5 = 16;
// Other variable declarations
bool solenoid2Activated = false;
bool lastSensorState = LOW;
int cycleCounter = 0;
int totalCycleCounter = 0;
const int maxCycles = 20;
int selectedMenu = 0;
int numberofrepititions = 20;
LiquidCrystal_I2C lcd_1(0x27, 16, 2);
unsigned long lastButton1Time = 0;
unsigned long lastButton2Time = 0;
unsigned long debounceDelay = 50;
unsigned long menuChangeDelay = 400;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastClearTime = 0;
const long lcdClearInterval = 2000;
const long lcdClearIntervalManual = 5000;
const long lcdClearIntervalFastAuto = 5000;
enum MenuState {
MENU_IDLE,
MENU_FAST_AUTO,
MENU_HALF_DROP,
MENU_MANUAL
};
enum SubState {
SUB_STATE_1,
SUB_STATE_2
};
SubState currentSubState = SUB_STATE_1;
MenuState currentMenuState = MENU_IDLE;
// Bluetooth Serial Communication
SoftwareSerial BTSerial(10, 11); // RX, TX
// Variables for button states
int button1State;
int button2State;
int button3State;
int button4State;
int button5State;
bool lastButton5State = LOW;
// Variables for tracking state and count
bool bluetoothControlEnabled = false;
void setup() {
BTSerial.begin(9600); // Initialize Bluetooth Serial
lcd_1.init();
lcd_1.setCursor(0, 0);
lcd_1.backlight();
lcd_1.display();
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(sensorpin, INPUT);
pinMode(solenoid1pin, OUTPUT);
pinMode(solenoid2pin, OUTPUT);
pinMode(solenoid3pin, OUTPUT);
pinMode(buzzerpin, OUTPUT);
}
void resetCounters() {
cycleCounter = 0;
totalCycleCounter = 0;
digitalWrite(solenoid1pin, LOW);
digitalWrite(solenoid2pin, LOW);
digitalWrite(solenoid3pin, LOW);
}
void printMenuName(const char *menuName) {
lcd_1.setCursor(6, 0);
lcd_1.print(menuName);
}
void loop() {
static MenuState lastMenuState = MENU_IDLE; // Track the last menu state
if (BTSerial.available()) {
String btCommand = BTSerial.readStringUntil('\n');
if (btCommand == "BT-control-on") {
bluetoothControlEnabled = true;
} else if (btCommand == "BT-control-off") {
bluetoothControlEnabled = false;
}
// Read physical button states
// Update button states based on the control mode
if (!bluetoothControlEnabled) {
// Read physical button states
button1State = digitalRead(button1);
button2State = digitalRead(button2);
button3State = digitalRead(button3);
button4State = digitalRead(button4);
button5State = digitalRead(button5);
} else {
// Retain the button states as they are (controlled by Bluetooth)
}
if (bluetoothControlEnabled) {
BTSerial.println("BluetoothControlEnabled");
} else {
BTSerial.println("BluetoothControlDisabled");
}
// Read characters from Bluetooth and update button states
if (BTSerial.available()) {
char btChar = BTSerial.read();
switch (btChar) {
case 'A': button1State = HIGH; break;
case 'B': button1State = LOW; break;
case 'C': button2State = HIGH; break;
case 'D': button2State = LOW; break;
case 'E': button3State = HIGH; break;// ... [Add Manual state functionality]
case 'F': button3State = LOW; break;
case 'G': button4State = HIGH; break;
case 'H': button4State = LOW; break;
case 'I': button5State = HIGH; break;
case 'J': button5State = LOW; break;
}
}
// Update cycle and total cycle counters on LCD
lcd_1.setCursor(0, 0);
lcd_1.print(cycleCounter);
lcd_1.setCursor(0, 1);
lcd_1.print("Total:");
lcd_1.print(totalCycleCounter);
// Menu selection logic
unsigned long currentTime = millis();
if (currentTime - lastDebounceTime1 > debounceDelay) {
if (button1State == HIGH) {
selectedMenu = (selectedMenu + 1) % 4;
lastDebounceTime1 = currentTime;
delay(menuChangeDelay);
}
}
if (currentTime - lastDebounceTime2 > debounceDelay) {
if (button2State == HIGH) {
selectedMenu = (selectedMenu + 3) % 4;
lastDebounceTime2 = currentTime;
delay(menuChangeDelay);
}
}
// Menu state logic
switch (selectedMenu) {
case 1:
currentMenuState = MENU_MANUAL;
break;
case 2:
currentMenuState = MENU_FAST_AUTO;
break;
// ... [Add more cases for additional menus]
default:
currentMenuState = MENU_IDLE;
break;
}
// Menu functionality
switch (currentMenuState) {
case MENU_IDLE:
printMenuName("Idle");
digitalWrite(solenoid2pin, LOW); // Activate solenoid2 to dump
digitalWrite(solenoid1pin, LOW);
if (lastMenuState != MENU_IDLE) {
printMenuName("Idle");
// Send menu name via Bluetooth
}
// ... [Add Idle state functionality]
break;
case MENU_MANUAL:
printMenuName("Manual");
BTSerial.println("MANUAL");
if (button3State == HIGH && digitalRead(sensorpin) == LOW && digitalRead(solenoid2pin) == LOW) {
lcd_1.println("Button 1 Pressed");
digitalWrite(solenoid1pin, HIGH);
} else {
digitalWrite(solenoid1pin, LOW);
}
if (button5State == HIGH) {
lcd_1.println("Button 2 Pressed");
digitalWrite(solenoid2pin, HIGH);
} else {
digitalWrite(solenoid2pin, LOW);
}
if (button4State == HIGH) {
lcd_1.println("Button 3 Pressed");
digitalWrite(solenoid3pin, HIGH);
} else {
digitalWrite(solenoid3pin, LOW);
}
break;
case MENU_FAST_AUTO:
if (button5State == HIGH && lastButton5State == LOW) {
// Toggle the substate
if (currentSubState == SUB_STATE_1) {
currentSubState = SUB_STATE_2;
} else {
currentSubState = SUB_STATE_1;
}
}
lastButton5State = button5State; // Update the last button state
// Switch-case logic for MENU_FAST_AUTO
if (currentMenuState == MENU_FAST_AUTO) {
switch (currentSubState) {
case SUB_STATE_1:
lcd_1.clear();
printMenuName("AUTO STANDBY");
BTSerial.println("AUTO STANDBY");
digitalWrite(solenoid2pin, LOW); // Activate solenoid2 to dump
digitalWrite(solenoid1pin, LOW);
// ... [Your specific logic for this sub-state]
break;
case SUB_STATE_2:
lcd_1.clear();
printMenuName("AUTO CYCLING");
BTSerial.println("AUTO CYCLING");
// ... [Add Fast Auto state functionality]
// Raise the cylinder if sensorpin is LOW and solenoid2pin is LOW
if (digitalRead(sensorpin) == LOW && digitalRead(solenoid2pin) == LOW) {
digitalWrite(solenoid1pin, HIGH); // Activate solenoid1 to raise
} else {
digitalWrite(solenoid1pin, LOW); // Deactivate solenoid1
}
// If sensorpin is HIGH, activate solenoid2pin for 1 second
if (digitalRead(sensorpin) == HIGH) {
digitalWrite(solenoid2pin, HIGH); // Activate solenoid2
delay(1000); // Note: Consider using non-blocking delay
digitalWrite(solenoid2pin, LOW); // Deactivate solenoid2
cycleCounter++;
totalCycleCounter++;
}
break;
// ... [Add more cases for additional menu states]
}
// Clear the LCD display at set intervals
if (currentTime - lastClearTime > lcdClearInterval) {
lcd_1.clear();
lastClearTime = currentTime;
}
}
// ... [Add Fast Auto state functionality]
break;
// ... [Add more cases for additional menu states]
}
// Clear the LCD display at set intervals
if (currentTime - lastClearTime > lcdClearInterval) {
lcd_1.clear();
lastClearTime = currentTime;
}}}
// ... [Add any additional functions here]