hello world!
setup:
3 switches
3 lights
3 sounds
each switch should turn on one light and play one sound. but only one at a time and while one is running, it shouldn't listen to the others. how can i stop or block the pins from reading while another function is running?
problem in my sketch is, that while one is on and i press another switch(or the same switch again), it notes all the presses and performs them after the running one is finished.
i already spent 2 days trying with while, if, else if, switch case, return, break.. but still no success. the result was either, that it doesn't play to the end or it played the other stuff right behind. it shouldn't.
it seems to me, that i don't understand the basic flow of loops?!
i simplified the sketch for you and left out one light and all the audio stuff. the "returns" might be stupid as well, because didn't make any difference as i remember. the Serial.prints are just for checking
i'm losing my faith..
int knopf1 = 3; // Knopf == SWITCH
int knopf2 = 4;
int knopf3 = 5;int val1 = 0; //BUTTONSTATE
int val2 = 0;
int val3 = 0;int lastval1; //LAST BUTTONSTATE
int lastval2;
int lastval3;int licht1 = 6; // licht == light
int licht2 = 7;
int licht3 = 8;int x;
void setup() {
Serial.begin(9600);
pinMode(licht1, OUTPUT);
pinMode(licht2, OUTPUT);pinMode(knopf1, INPUT);
pinMode(knopf2, INPUT);
}void loop(){
val1 = digitalRead(knopf1);
val2 = digitalRead(knopf2);if(val1 == HIGH && lastval1 == LOW && x == 0){ // i use switches as buttons, this section is for checking the state
x = 1;
lastval1 = val1;
Serial.println("case 1a");
Serial.print("x = ");
Serial.println(x);
return;
}
else if(val1 == LOW && lastval1 == HIGH && x == 0){
x = 1;
lastval1 = val1;
Serial.println("case1b");
Serial.print("x = ");
Serial.println(x);
return;
}
else if(val2 == HIGH && lastval2 == LOW && x == 0){
x = 2;
lastval2 = val2;
Serial.println("case 2a");
Serial.print("x = ");
Serial.println(x);
return;
}
else if(val2 == LOW && lastval2 == HIGH && x == 0){
x = 2;
lastval2 = val2;
Serial.println("case2b");
Serial.print("x = ");
Serial.println(x);
return;
}Serial.print(" zwischencheck x = "); //just a check
Serial.println(x);while(x == 1){ // actual functions start here
digitalWrite(licht1, HIGH);
digitalWrite(licht2, LOW);
delay(3000);
x = 0;
}
while(x == 2){
digitalWrite(licht1, LOW);
digitalWrite(licht2, HIGH);
delay(3000);
x = 0;
}
}