logic help
in
Programming Questions
•
3 years ago
Hello,
I'm trying to make a color recognition program where when a color, say, red, that the camera picks up is in a certain area of the screen, a unique sound will play in each divided area. I can get it to work fine but, of course, the sound keeps playing when the color is in that area. I've been messing around with the logic of this, trying to get the sound to play only once, but something is just off and the program is behaving strangely. I will copy the part of the code that isn't working right. How do I get it to just play this sound once when entering the each section or "chamber??" I know I've seen tutorials that deal with a similar problem but I just can't find them. Any help is much appreciated!! :
I'm trying to make a color recognition program where when a color, say, red, that the camera picks up is in a certain area of the screen, a unique sound will play in each divided area. I can get it to work fine but, of course, the sound keeps playing when the color is in that area. I've been messing around with the logic of this, trying to get the sound to play only once, but something is just off and the program is behaving strangely. I will copy the part of the code that isn't working right. How do I get it to just play this sound once when entering the each section or "chamber??" I know I've seen tutorials that deal with a similar problem but I just can't find them. Any help is much appreciated!! :
- //faulty logic to get the yellow circle to play a unique note once during entry in to each chamber. myswitch is //a boolean variable defined before setup as just"boolean myswitch;"
if ((colorX < one)&&(colorX>0)) {
if (myswitch) {
println("FIRST CHAMBER");
snip[1].trigger();
delay(dl);
myswitch=false;
}
}
else if ((colorX > one)&&(colorX<two)) {
if (myswitch) {
println("SECOND CHAMBER");
snip[2].trigger();
delay(dl);
myswitch=false;
}
}
else if ((colorX>two)&&(colorX<three)) {
if (myswitch) {
println("THIRD CHAMBER");
snip[3].trigger();
delay(dl);
myswitch=false;
}
}
else if ((colorX>three)&&(colorX<width)) {
if (myswitch) {
println("FOURTH CHAMBER");
snip[4].trigger();
delay(dl);
myswitch=false;
}
} else {println("-----true-----"); myswitch=true;}
1