const int Rpin = 23;
const int Gpin = 22;
int Rold = LOW; // default/idle value
int Gold = LOW; // default/idle value
int mode = 0;
void setup() {
Serial.begin(115200);
Serial.println("Press the button.");
pinMode(Rpin, INPUT);
pinMode(Gpin, INPUT);
}
void loop() {
// Read the current state of Rpin
int Rnew = digitalRead(Rpin);
// Check if the state of Rpin has changed (button press detected)
if (Rnew != Rold) {
if (Rnew == HIGH) {
mode = 1; // Toggle to mode 1 when Rpin is HIGH
}
// Update Rold to store the last known state
Rold = Rnew;
}
// Read the current state of Gpin
int Gnew = digitalRead(Gpin);
// Check if the state of Gpin has changed (button press detected)
if (Gnew != Gold) {
if (Gnew == HIGH) {
mode = 2; // Toggle to mode 2 when Gpin is HIGH
}
// Update Gold to store the last known state
Gold = Gnew;
}
// Output the current mode
Serial.println(mode);
// Small delay to stabilize the loop and prevent excessive updates
delay(100);
}