This is a program that has three different pushbutton inputs. Both the demo and lock sub-sketches work, but the switch case statement for the storm sketch is not progressing past the first case(case=1)
Any idea why? Im using the switch case statements in the following manner.
1.) once the storm trigger is detected, set switch variable to 1.
2.) now that the switch variable is equal to one, case 1 will run and exit until the enclosed if statements time requirement is met, an output then takes place, then one is added to the switch variable to progress it to case two.
3.) repeat until end.
const int fogger = 11;
const int mister = 12;
const int music = 6;
const int shutoff = 7;
const int lock = 8;
const int stormTrigger = 9;
const int momentaryTrigger = 13;
const int lockTrigger = 10;
int stState = LOW; //Initaialize Storm trigger State
int mtState = LOW; //Initialize momentary fog/mist trigger state
int ltState = LOW; //Initialize lock state :)
long lastSTDebounceTime = 0;//last time these were debounced
long lastMTDebounceTime = 0;
long lastLTDebounceTime = 0;
long debounceDelay = 200;// the debounce time
int const lockTime = 3000;//sets the time for lock to be unlocked
int unlockStartTime = 0;//Keeps track of when unlock started
int const demoFoggerTime = 120000;
int const demoMisterTime = 5000;
int momentaryFogTime = 0;//Keeps track of fog start time
int momentaryMisterTime = 0; // leeps track of mister start time
int switchValue = 0; //tracks switch value
int stormStartTime = 0; //tracks storm time
int stormIntervalTime = 0;//tracks time between events
void setup() {
pinMode(fogger, OUTPUT);
pinMode(mister, OUTPUT);
pinMode(music, OUTPUT);
pinMode(shutoff, OUTPUT);
pinMode(lock, OUTPUT);
pinMode(stormTrigger, INPUT);
pinMode(momentaryTrigger, INPUT);
pinMode(lockTrigger, INPUT);
digitalWrite(fogger, HIGH);
digitalWrite(mister, HIGH);
digitalWrite(music, HIGH);
digitalWrite(shutoff,HIGH);
}
void loop(){
/////LOCK SKETCH////
ltState = digitalRead(lockTrigger); //read locktrigger high/low and put value in ltState Variable
if( (millis() - lastLTDebounceTime) >= debounceDelay) { //if current time - last debounce for trigger is > 200ms then
if (ltState == HIGH){ //If Lock trigger is being pushed
digitalWrite(lock, LOW); // Turns on lock
lastLTDebounceTime = millis(); //SET last Lock debounce to current time
unlockStartTime = millis(); //SET unlock start time to current time
}}
if ( (millis() - unlockStartTime) >= lockTime) { //If current time- the time when the lock opened is >= to variable =3000 then close
digitalWrite(lock, HIGH);
}
////Mister/Fogger Demo Sketch///
mtState = digitalRead(momentaryTrigger); //Read momentary trigger
if( (millis() - lastMTDebounceTime) >= debounceDelay) { //if current time- last debounce for momentary trigger is >= 200ms then
if ((mtState == HIGH) && (switchValue == 0)) { //if momentary switch is on and the storm is NOT ocurring
digitalWrite(fogger, LOW);
digitalWrite(mister, LOW);
lastMTDebounceTime = millis(); //set mt trigger debounce time to current
momentaryFogTime = millis(); //set fogger time
momentaryMisterTime = millis();// set mister Time
}}
if ( (millis() - momentaryFogTime) >= demoFoggerTime) { //If current time- the time when the fogger has been going is >= to variable =120000 then switch off
digitalWrite(fogger, HIGH);
}
if ( (millis() - momentaryMisterTime) >= demoMisterTime) { //If current time- the time when the mister has been going is >= to variable = 5000 then switch off
digitalWrite(mister, HIGH);
}
//Storm Mode//
stState = digitalRead(stormTrigger);//read storm trigger
if( ((millis() - lastSTDebounceTime) >= debounceDelay) && (switchValue == 0)) { //if current time- last debounce time is >=200ms then
if ((stState == HIGH) && (switchValue == 0)) {
switchValue=1; //n
stormStartTime = millis();//set event start time to current time
}}
switch(switchValue) {
case 1:
if ((millis()- stormStartTime) >= 100) {
digitalWrite(music, LOW);//music on
digitalWrite(fogger, LOW);//LOW is on???
digitalWrite(shutoff, LOW);
stormIntervalTime = millis();//set varialbe to current time
switchValue+1;
delay(20);
}
break;
case 2: ////
if ((millis() - stormIntervalTime) >= 3000){
digitalWrite(mister,LOW);
digitalWrite(music,HIGH); //music back off
stormIntervalTime = millis();
switchValue+1;
delay(20);
}
break;
case 3://///
if ((millis() - stormIntervalTime) >= 3000){
digitalWrite(mister,HIGH);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 4: /////
if ((millis() - stormIntervalTime) >=6000){
digitalWrite(mister,LOW);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 5: ////////
if ((millis() - stormIntervalTime) >= 3000){
digitalWrite(mister,HIGH);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 6: ///////
if ((millis() - stormIntervalTime) >=8000){
digitalWrite(mister,LOW);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 7: ////////
if ((millis() - stormIntervalTime) >= 3000){
digitalWrite(mister,HIGH);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 8: ///////
if ((millis() - stormIntervalTime) >=12000){
digitalWrite(mister,LOW);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 9: ////////
if ((millis() - stormIntervalTime) >= 3000){
digitalWrite(mister,HIGH);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 10: ///////
if ((millis() - stormIntervalTime) >=8000){
digitalWrite(mister,LOW);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 11: ////////
if ((millis() - stormIntervalTime) >= 4000){
digitalWrite(mister,HIGH);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 12: ///////
if ((millis() - stormIntervalTime) >=13000){
digitalWrite(mister,LOW);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 13: ////////
if ((millis() - stormIntervalTime) >= 2000){
digitalWrite(mister,HIGH);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 14: ///////
if ((millis() - stormIntervalTime) >=15000){
digitalWrite(mister,LOW);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 15: ////////
if ((millis() - stormIntervalTime) >= 3000){
digitalWrite(mister,HIGH);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 16: ///////
if ((millis() - stormIntervalTime) >=8000){
digitalWrite(mister,LOW);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 17: ////////
if ((millis() - stormIntervalTime) >= 1500){
digitalWrite(mister,HIGH);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 18: ///////
if ((millis() - stormIntervalTime) >=14000){
digitalWrite(mister,LOW);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 19: ////////
if ((millis() - stormIntervalTime) >= 3000){
digitalWrite(mister,HIGH);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 20: ///////
if ((millis() - stormIntervalTime) >=18000){
digitalWrite(mister,LOW);
stormIntervalTime = millis();
switchValue+1;
}
break;
case 21: ///////
if ((millis() - stormIntervalTime) >=2000){
digitalWrite(mister,HIGH);
digitalWrite(fogger, HIGH);//LOW is on???
digitalWrite(shutoff, HIGH);
stormIntervalTime = millis();
switchValue = 0;
}
break;
}}