So the title pretty much says it. I am trying to use a for condition with three if...else statements inside it. My desire is for the pin 8 to read high and for the code to see that this is the proper order of pins going high and to thus play the sound and step the value of x from 0 to 1 or if the pins were set high out of order to play the sound but restart the loop. Not sure if that makes sense. Here is the segment of my code that is giving me issues. I changed the third if statement to "if (digitalRead(10) == HIGH && x ==0)" and then the code was executed. This makes me assume I am not setting up my for condition right or possibly using it wrong. Any thoughts?
for (int x = 0; x < 3; x++)
{
if (digitalRead(8) == HIGH && x ==0){
play sound;
}
else if (digitalRead(8) == HIGH && x!=0){
play sound;
return;
}
if (digitalRead(9) == HIGH && x ==1){
play sound;
}
else if (digitalRead(9) == HIGH && x!=1){
play sound;
return;
}
if (digitalRead(10) == HIGH && x ==2){
play sound;
digitalWrite(11,HIGH);
delay (100);
digitalWrite(11,LOW);
delay (100);
return;
}
else if (digitalRead(10) == HIGH && x!=2){
play sound;
return;
}
}
Can you explain in English (not code) what you want to happen.
I can't figure out what's the purpose of the FOR loop.
There seems little point in these tests (for example)
if (digitalRead(8) == HIGH && x ==0){
......
else if (digitalRead(8) == HIGH && x!=0){
because between them they are the same as
if (digitalRead(8) == HIGH) {
...R
PS. Don't start another Thread for the same project. Click Report to Moderator and ask to have the Threads merged so we have all the relevant data in one place
Total Overview - I have three light fixtures(sconces). I have connected an Adafruit momentary touch sensor to each sconce. My Arduino Uno has a wave shield attached. I have four sounds on an SD card in the wave shield. My desire is for the sconces to be touch in the right order and then(and only then) for a secret door to be opened. I currently have the sounds playing for each sconce and the final sconce to play a sound and then open the door but there is nothing stopping someone from starting with that final sconce(which kind of bypasses the whole puzzle). This is where the For loop comes in, I think.
The way I would approach that problem is to have a char array with the order that the buttons should be pressed. For example
char requiredOrder = "CBA"
and then I would have another array into which I would put the button IDs as they are pressed. When 3 have been pressed I would compare the characters in the two arrays. If they match - great. If they don't ...
thanks everyone for the help, but I got it using my If and For statements/conditions. My issue was that I had three "If" statements running in my "For" loop, therefore it was not incrementing Int x until the loop was done, which due to the return statements it never was. I changed my "For" from
For (x = 0; x < 3; x++)
to
For (x = 0; x < 3)
and then added a line under each IF statement changing X from 0 to 1 to 2 depending on if the previously pressed sensors were the correct ones and if not then the loop restarted.
Thanks again for your help, just wanted to leave the solution here for anyone in the future.