How can I check during my whole code when a button is pressed?

Hello I am making a project where I have 4 buttons, a menu button (also to confirm the selectors), two selectors and a stop/reset button. I am finding it difficult to make the stop button work even though I have it in a while loop. Can somebody please help? I have already added in other while loops the " && stopState != HIGH" but it doesn't seem to work. Basically this button needs to reset at any given point the whole code as if I just pressed the arduino reset button.
Thanks!

const int pos = 0;
const int buzzer = 8;
const int menuBtn = A0;
const int select1Btn = A1;
const int select2Btn = A2;
const int stopBtn = A3;
const int temp = A4;

const int led1 = A5;
const int led2 = A6;
const int led3 = A7;
const int led4 = A8;

int trigger = 0;
int triggerVal = 0;
int tempValue;
double voltLm35 = 0.0;
double tempLm35 = 0.0;
double tempC = 0;
double tempF =0;

int cont = 0;
int cont2 = 0;
int s1State = 0;
int s1OldState = 0;
int s2State = 0;
int s2OldState = 0;
int menuState = 0;
int menuOldState = 0;
int stopState = 0;
int stopOldState = 0;
  int C = 0;
  int F = 0;
int contLoop =0;
int contC =0;
int contF = 0;
int ascendingTemp =0;
int lm35Value;


void setup()
{
  Serial.begin(9600);
  
  //initialize buzzer as output
  pinMode(buzzer, OUTPUT); // buzzer 

  //initialize buttons and lm35 as input
  pinMode(menuBtn, INPUT); //  menu
  pinMode(select1Btn, INPUT); // selection 1
  pinMode(select2Btn, INPUT); // selection 2
  pinMode(stopBtn, INPUT); // stop
  pinMode(temp, INPUT); // lm35

}
void loop()
{
    while (cont == 0){ 
      delay(600);
      stopState = 0;
      stopOldState = 0;
       
       while(stopState != HIGH ){  // check if stop button is pressed stopState == stopOldState
          //while(contLoop == 0){
          stopState = digitalRead(stopBtn);  
              Serial.print("\nCelcius or Farenheit? (select1: Celsius, select2: Farenheit)");
              cont2 = 0;
              
              choosingScale();
              stopState = digitalRead(stopBtn);
              delay(800);
              if(C == 1){
                choosingTriggerC();
                delay(800);
                digitalWrite(buzzer,LOW);
                Serial.print("\nStarting Temperature: ");           
                menuOldState = 0;
                menuState = 0;
                while(stopState != HIGH){
                    delay(600);
                  menuState = digitalRead(menuBtn); 
                  tempValue = analogRead(temp); 
                  
                  tempC=(((5 * tempValue)*100)/1024);
                  
                   Serial.print("Temperature:  ");
                   Serial.print(tempC,2); //2 decimal variable
                   Serial.println("°C");
                   Serial.println("\n trigger: ");
                  Serial.print(triggerVal);
                  
                    ascendingTemp = triggerVal/4;
                    Serial.print("\n ascending: ");
                    Serial.print(ascendingTemp);
              
                     if(tempValue <= ascendingTemp){
                      digitalWrite(led1, HIGH);
                      digitalWrite(led2, LOW);
                      digitalWrite(led3, LOW);
                      digitalWrite(led4, LOW); 
                     }
                     if(tempValue >= ascendingTemp*2){
                        digitalWrite(led1, HIGH);
                        digitalWrite(led2, HIGH);
                        digitalWrite(led3, LOW);
                        digitalWrite(led4, LOW); 
                     }
                     if(tempValue >= ascendingTemp*3 ){
                        digitalWrite(led1, HIGH);
                        digitalWrite(led2, HIGH);
                        digitalWrite(led3, HIGH);
                        digitalWrite(led4, LOW); 
                     }
                     if(tempValue >= trigger){
                        digitalWrite(buzzer,HIGH);
                        digitalWrite(led1, HIGH);
                        digitalWrite(led2, HIGH);
                        digitalWrite(led3, HIGH);
                        digitalWrite(led4, HIGH);    
                     }  
               }
              }
               
              if (F == 1){
                choosingTriggerF();
                delay(800);
                digitalWrite(buzzer,LOW);
                Serial.print("\nStarting Temperature: ");           
                menuOldState = 0;
                menuState = 0;
                while(stopState != HIGH){
                    delay(800);
                    menuState = digitalRead(menuBtn); 
                    tempValue = analogRead(temp); 
                    tempC=(((5 * tempValue)*100)/1024); 
                    tempF = (tempC * 9.0 / 5.0) + 32.0;
                   Serial.print("    Temperature:  ");
                   Serial.print(tempF,2); //2 decimal variable
                   Serial.println("°F");
                   Serial.println("\n trigger: ");
                   Serial.print(triggerVal);
                  
                    ascendingTemp = triggerVal/4;
                    Serial.print("\n ascending: ");
                    Serial.print(ascendingTemp);
              
                     if(tempValue <= ascendingTemp){
                      digitalWrite(led1, HIGH);
                      digitalWrite(led2, LOW);
                      digitalWrite(led3, LOW);
                      digitalWrite(led4, LOW); 
                     }
                     if(tempValue >= ascendingTemp*2){
                        digitalWrite(led1, HIGH);
                        digitalWrite(led2, HIGH);
                        digitalWrite(led3, LOW);
                        digitalWrite(led4, LOW); 
                     }
                     if(tempValue >= ascendingTemp*3 ){
                        digitalWrite(led1, HIGH);
                        digitalWrite(led2, HIGH);
                        digitalWrite(led3, HIGH);
                        digitalWrite(led4, LOW); 
                     }
                     if(tempValue >= trigger){
                        digitalWrite(buzzer,HIGH);
                        digitalWrite(led1, HIGH);
                        digitalWrite(led2, HIGH);
                        digitalWrite(led3, HIGH);
                        digitalWrite(led4, HIGH);    
                     }  
               }

            //contLoop++;
          }
      } //stop state loop
    } //while loop
  cont++; //ends all the system
  delay(10); // Delay a little bit to improve simulation performance
}
        
            
      

void choosingTriggerF(){
    s1OldState = 0;
    s2OldState = 0;
    menuState = 0;
    menuOldState = 0;
    cont2 = 0;
    int trigger = 50; //start temp 
    int contAdd =0;
    int contSub = 0;
    Serial.print("\n Choose a Trigger (50 F - 68 F): " );
    Serial.print("\n Trigger: 50" ); //
    while(cont2 == 0 && stopState != HIGH){
        delay(200); //helps for last button press to not be counted in this 
        s1State = digitalRead(select1Btn); //checks if there are button presses in each loop
         s2State = digitalRead(select2Btn);
         menuState = digitalRead(menuBtn);

         if (s1State != s1OldState) { 
              if(trigger < 67){ //prevents it from going past 68
                 trigger += 2;
                 floor(trigger);
                 s1OldState = 0; //returns old state to LOW
                 Serial.print("\n Trigger:" );
                 Serial.print(trigger );
            }
         }
         else if (s2State != s2OldState) { //prevents it from going below 50   
              if(trigger > 51){
                 trigger -= 2;
                 s2OldState = 0; //returns old state to LOW
                 floor(trigger);

                 
                 Serial.print("\n Trigger:" );
                 Serial.print(trigger);
              }
         }
        
        if( menuOldState != menuState){
              Serial.print("\n Trigger Confirmed: " );
              Serial.print(trigger );
              menuState = 0;
              menuOldState = menuState; //old state and new both to LOW
              cont2++;
           }
        }
        return trigger;
}
////////////////////////////////////////////////////////////////////////
void choosingTriggerC(){
    s1OldState = 0;
    s2OldState = 0;
    menuState = 0;
    menuOldState = 0;
    cont2 = 0;
    int trigger = 10;
    int contAdd =0;
    int contSub = 0;
    Serial.print("\n Choose a Trigger (10 C - 20 C): " );
    Serial.print("\n Trigger: 10" );
    while(cont2 == 0 && stopState != HIGH){
      delay(200); //helps for last button press to not be counted in this 
         s1State = digitalRead(select1Btn); //checks if there are button presses in each loop
         s2State = digitalRead(select2Btn);
         menuState = digitalRead(menuBtn);

         if (s1State != s1OldState) { 
              if(trigger < 19){ //prevents it to go past 20
                 trigger += 2;
                 s1OldState = 0; //returns old state to LOW
                 Serial.print("\n Trigger:" );
                 Serial.print(trigger );
            }
         }
         else if (s2State != s2OldState) { //prevents it to go below 8
              if(trigger > 11){
                 trigger -= 2;
                 s2OldState = 0; //returns old state to LOW
                 Serial.print("\n Trigger:" );
                 Serial.print(trigger);
              }
         }
        
        if( menuOldState != menuState){
              Serial.print("\n Trigger Confirmed: " );
              Serial.print(trigger);
              triggerVal = trigger;
              menuState = 0;
              menuOldState = menuState; //old state and new both to LOW
              cont2++;
           }
        }
        return trigger;
}

////////////////////////////////////////////////////////////////////////////////////////
void choosingScale(){
          contC = 0;
          contF = 0;
    while(cont2 == 0 && stopState != HIGH){ //only executes one time
          s1State = digitalRead(select1Btn); //reads the selector states (pressed or not)
          s2State = digitalRead(select2Btn);
          menuState = digitalRead(menuBtn); //reads menu state
          
          if (s1State != s1OldState) { // if selector1 was pressed Celcius was chosen
            if ( contC == 0){ //will execute only one time
              Serial.print("\n Celcius" ); 
              C = 1;
              F = 0;
              contC++;
              s1OldState = s1State;
             }
           }
          else if (s2State != s2OldState) {
            if(contF == 0){
              Serial.print("\n Farenheit" );
              F = 1;
              C = 0;
              contF++;
             s2OldState = s2State;
            }
           }

           if( menuOldState != menuState){ //when menuBtn is pressed confirm the scale
               if (F == 1){
                Serial.print("\n Scale confirmed: Farenheit" );
               }
               if (C == 1){
                Serial.print("\n Scale confirmed: Celcius" );
               }
               menuState = 0;
               menuOldState = menuState; //both states low
               cont2++;
           }     
   }
}

Suggest you scan your switches ~50ms.
This will effectively debounce your switches.
Look at when switches change state.

Use a State Machine to manage program conditions.

Avoid using while( ) in you sketches as it often blocks code execution.

Don’t use delay( ) as it freezes code execution for that time period.

would be better to scan all buttons in loop() and take some action based on the button while, as @LarryD suggests, keep track of your state

consider

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

code is very difficult to follow, especially with all the nested while loops

won't your program do nothing if cont is ever not zero?

And ... the conventional approach is to connect the button between the pin and ground, configure the pin as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and for the button to pull the pin LOW.

seems like you duplicated code for celsius and farenheit. should the code operate on just one scale and the scale just affects the input and display?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.