Hello all - hope you're well, looking for a little advice on a project which is driving me up the wall.
Simple overview: I have 2 push buttons, one connected to pin 7 and the other on pin 8. For each state (Button 1 (B1) pressed, B1 released, B2 pressed, B2 released I'm doing something different (in this case writing some text to an i2c OLED display). Button presses could come in any order.
Here's the code I've written, it works but only seems to get state change actions for the first button that is pressed (either B1 or B2, doesn't matter) then the other one doesn't change function.
I highly suspect it's something to do with my loops, where the first button press gets stuck in that loop & doesn't proceed to check the other input but I'm struggling to resolve it. Essentially I want it to keep checking each of the 2 inputs and alter the action (OLED display) upon state change on either of the pushbuttons.
Thanks in advance!
#include <Adafruit_SSD1306.h>
//Define LCD SPI Connections:
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_RESET);
//Switch inputs
#define pushSwitch 7 // this is the push switch input
#define pushSwitch2 8 // this is the push switch input
int buttonState = 0;
bool oldState = HIGH;
int showType = 0;
void setup() {
//Relay & card switch functions
pinMode(pushSwitch, INPUT_PULLUP);Â //Â attaches a resistor from input to Vcc
pinMode(pushSwitch2, INPUT_PULLUP);Â //Â attaches a resistor from input to Vcc
// Setup for LCD display
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);Â //Initialize with the I2C address 0x3C.
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("~Starting~");
display.println("~ Up! ~");
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("<-1-Ready");
display.println("Ready-2->");
display.display();
Â
}
void loop() {
Â
//Charger 1 loop - when pushSwitch pressed
if (digitalRead(pushSwitch) == LOW ) {Â // LOW if pressed
//display.clearDisplay(); //don't wipe all screen
display.setCursor(0,0);
display.setTextSize(2);
display.setTextColor(BLACK); //overwrite previous text in black
display.println("<-1-Ready"); //overwrite previous text in black
display.setCursor(0,0); //only do first line
display.setTextColor(WHITE);
display.println("<Charging<");
display.display();
//return to ready state - when pushSwitch released
while (digitalRead(pushSwitch) == HIGH) {
display.setCursor(0,0);
display.setTextSize(2);
display.setTextColor(BLACK); //overwrite previous text in black
display.println("<Charging<"); //overwrite previous text in black
display.setCursor(0,0); //only do first line
display.setTextColor(WHITE);
display.println("<-1-Ready");
display.display();
}
}
//Charger 2 loop - when pushSwitch2 pressed
if (digitalRead(pushSwitch2) == LOW ) {Â // LOW if pressed
//display.clearDisplay(); //don't wipe all screen
display.setCursor(0,16);
display.setTextSize(2);
display.setTextColor(BLACK); //overwrite previous text in black
display.println("Ready-2->"); //overwrite previous text in black
display.setCursor(0,16); //only do second line
display.setTextColor(WHITE);
display.println(">Charging>");
display.display();
//return to ready state - when pushSwitch2 released
while (digitalRead(pushSwitch2) == HIGH) {
display.setCursor(0,16);
display.setTextSize(2);
display.setTextColor(BLACK); //overwrite previous text in black
display.println(">Charging>"); //overwrite previous text in black
display.setCursor(0,16); //only do second line
display.setTextColor(WHITE);
display.println("Ready-2->");
display.display();
}
}
}