On/Off Switch

HI!

I want to use a button as a on/off switch but i don’t know how it works…
I’ve seen post that show how to do it with a led but i cannot understand how to do it for a “more complex” code.

this is my code for now:

const int buttonPin = 2;     // the number of the pushbutton pin
int redPin = 13;             // RED LED is connected to 13
int greenPin = 12;           //GREEN LED connected to 12
int piezoBuzzerPin = 8;      // Piezo Buzzer is connected to 9
int flexPin = A0;
#include <Servo.h>           //SERVO LIBRARY
Servo servo1; 

// Variables will change:
int redState = LOW;         // the current state of the output pin
int greenState = LOW;         // the current state of the output pin
int buttonState = LOW;             // the current reading from the input pin
int lastButtonState = HIGH;   // the previous reading from the input pin
int reading;

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(flexPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(piezoBuzzerPin, OUTPUT);   //declare buzzer as output
Serial.begin(9600);                //starts serial monitor
servo1.attach(11);                 //servo attached at pin 10
}



void loop() {
  int flexSensorReading = analogRead(flexPin); 
  reading = digitalRead(buttonPin);
   if (reading != lastButtonState) {
      lastDebounceTime = millis();
      lastButtonState = reading;
   } 

   if ((millis() - lastDebounceTime) > debounceDelay) {
       if (buttonState != lastButtonState) {
           buttonState = lastButtonState;
           if (buttonState == HIGH && flexSensorReading > 195 && flexSensorReading < 250) { 
                 
                 redState = !redState;
                 digitalWrite(redPin, redState);     //red led on
                 tone(piezoBuzzerPin, 200);          //piezo on
                 delay(1000);                        //delay 100 ms
                 tone(piezoBuzzerPin, 25);
                 delay(1000);
                 servo1.write(180);     //servo closes the "door" at 180 angle
            }
             else if (buttonState == HIGH && flexSensorReading > 170 && flexSensorReading < 185) {
               
               greenState = !greenState;
               digitalWrite(greenPin, greenState);
               digitalWrite(redPin, LOW);     //red led off
               digitalWrite(greenPin, HIGH); //green pin is on
               digitalWrite(piezoBuzzerPin,LOW); //piezo is off
               servo1.write(0);     //servo opens the "door" at 180 angle
             }
             check();
           }
       }
   }
void check(){

  while(Serial.available() > 0){ 
 
 if (Serial.parseInt()==123){
  digitalWrite(redPin, LOW);     //red led off
  digitalWrite(greenPin, HIGH); //green pin is on
 noTone(piezoBuzzerPin); //piezo is off
  servo1.write(180);     //servo opens the "door" at 180 angle
  Serial.print("serial monitor works"); 
  break;
  }
  }
  }

Please define "more complex"

Instead of going straight into the action code from the button test

if (buttonState == HIGH && flexSensorReading > 195 && flexSensorReading < 250) {

just save the setting in a variable

if (buttonState == HIGH) {
   goodToGo = true;
}
else {
   goodToGo = false;
}
if (goodToGo == true && flexSensorReading > 195 && flexSensorReading < 250) {

and then you can also use the value in the variable for other things.

(I’m not sure if my ELSE is correct in this context, but you should get the idea)

…R