Hello all,
I am trying to exit a switch case early by using a button and failing miserably. I've spent hours tweaking the program and double checking hardware.
Can anyone help me figure out why this program seems to ignore my attempts to exit the switch case when I press the resetBtn?
Any help would be appreciated.
const byte buzzer = 11;
const byte beepCycles = 16;
const byte resetBtn = A2;
byte beepCount = 0;
byte state = 0;
bool turnOn = true;
unsigned long waitStart;
unsigned long shortPeriod = 500;
unsigned long longPeriod = 5000;
unsigned long currentTime;
void setup()
{
 pinMode(buzzer, OUTPUT);
 pinMode(13, OUTPUT);
 pinMode(resetBtn,INPUT_PULLUP);
 //digitalWrite(ledPin, HIGH);
 //digitalWrite(resetBtn, HIGH);
 waitStart = millis();
}
void loop()
{
 switch (state)
 {
  case 0:
   currentTime = millis();
   digitalRead(resetBtn);
   if(resetBtn==LOW){          // this is where i am trying to exit the switch case
     state = 2;
     //digitalWrite(13, HIGH);
     break;
    }
   if (currentTime - waitStart >= shortPeriod && turnOn==true)
   {
    tone(buzzer,5000);
    waitStart = currentTime;
    beepCount++;
    state = 1;
    turnOn=false;
    if (beepCount == beepCycles)
    {
     state = 2;
    }
   }
   break;
  Â
  case 1:
   currentTime = millis();
   digitalRead(resetBtn);
   if(resetBtn==LOW){          // this is where i am trying to exit the switch case
     state = 2;
     //digitalWrite(13, HIGH);
     break;
    }
   if (currentTime - waitStart >= shortPeriod && turnOn==false)
   {
    noTone(buzzer);
    waitStart = currentTime;
    beepCount++;
    state = 0;
    turnOn=true;
    if (beepCount == beepCycles)
    {
     state = 2;
    }
   }
   break;
  Â
  case 2:
   break;
 }
}
Thank you for everyone's input.
I was trying, unsuccessfully, to modify a portion of an existing program. I went back and rewrote the whole thing as a series of if statements. Everything is working as expected.
Here is the functional program I ended up with. I understand this goes nowhere and just stops. The variable alarmVal is controlled elsewhere. This is only a small 'chunk' I was trying to debug.
/*
* The purpose of this is to beep a set number of times. At any point, if the
* resetBtn is pushed, the program is to stop beeping and reset the alarmVal to 0.
* It may not be elegant, but it works as intended.
*/
const byte buzzer = 11;Â Â //peizo buzzer pin
const byte beepCycles = 16; //beeps * 2
const byte resetBtn = A2;Â //resetBtn pin
byte beepCount = 0;Â Â Â Â //initial beep count
int alarmVal = 1;Â Â Â Â Â //initial value for testing
bool turnOn = true;Â Â Â Â //initial value for testing
unsigned long waitStart;
unsigned long shortPeriod = 500;
unsigned long longPeriod = 5000;
unsigned long currentTime;
void setup()
{
 pinMode(buzzer, OUTPUT);
 pinMode(resetBtn,INPUT_PULLUP);
 waitStart = millis();
}
void loop(){
 if(alarmVal==1){               Â
  currentTime = millis();        Â
  bool value = digitalRead(resetBtn);    //save state of resetBtn
  if(value==LOW){              //if Btn pressed
   alarmVal=0;               //reset alarmVal
   noTone(buzzer);             //turn off buzzer
  }
  if (currentTime - waitStart >= shortPeriod){ //timer conditions for tone cycle
    tone(buzzer,5000);            //initial buzzer
    waitStart = currentTime;         //save value for next loop
    beepCount++;               //increment beepCount
    alarmVal = 2;               //set alarmVal for noTone cycle
    if (beepCount == beepCycles){       //if all cycles are complete
     alarmVal = 0;              //reset alarmVal
    }
  }
 }
  Â
 if(alarmVal==2){                //rinse and repeat...
  currentTime = millis();
  bool value = digitalRead(resetBtn);
  if(value==LOW){         Â
   alarmVal = 0;
   noTone(buzzer);
  }
  if (currentTime - waitStart >= shortPeriod){
    noTone(buzzer);
    waitStart = currentTime;
    beepCount++;
    alarmVal = 1;
    if (beepCount == beepCycles)
    {
     alarmVal = 0;
    }
  }  Â
 }
}
To make it elegant, pull different functionality apart and use common software techniques.
Instead of thinking in code, you can also think in data streams that flow through the Arduino. The resetButton creates a stream of data (it is pressed or it is not pressed). To get that data stream into the loop(), use digitalRead() just once at the beginning of the loop(). You check the resetButton twice in the loop(), then that data stream has to split into two streams, that makes no sense.