Switch button
I have connect Arduino with wave shield and 2 switch buttons to play wave when the buttons are pressed but it played it automatically with out any press and it repeat I don't now why ..
void loop()
{
trackNum = findGPSLatLng();
Serial.println(trackNum);
// Get if a button was pushed
buttonState1 = digitalRead(buttonPin0);
buttonState2 = digitalRead(buttonPin1);
if(buttonState1 == HIGH && previous1 == LOW && millis() - time > debounce)
{
audioSelectFlag = 1;
}
else if(buttonState2 == HIGH && previous2 == LOW && millis() - time > debounce)
{
audioSelectFlag = 2;
}
time = millis();
// here is the wave files to play
switch (trackNum) {
case 5:
if(audioSelectFlag == 1 || audioSelectFlag == 2)
playcomplete("1.WAV");
break;
case 1:
if(audioSelectFlag == 1)
playcomplete("1.WAV");
else if(audioSelectFlag == 2)
playcomplete("1b.WAV");
break;
case 2:
if(audioSelectFlag == 1)
playcomplete("2a.WAV");
else if(audioSelectFlag == 2)
playcomplete("2b.WAV");
break;
case 3:
if(audioSelectFlag == 1)
playcomplete("3a.WAV");
else if(audioSelectFlag == 2)
playcomplete("3b.WAV");
break;
}
}
Discussions
8 years ago
I have them declared before
int previous1 = LOW;
int previous2 = HIGH;
you mean like this
while (digitalRead(buttonPin0) == HIGH) {
switch (trackNum)
{ case 1:
?
Answer 8 years ago
Post the entire code not just the void loop.
8 years ago
I have them before
int previous1 = LOW;
int previous2 = HIGH;
You mean like this
while (digitalRead(buttonPin) == HIGH) {
switch (trackNum) {
case 1:
etc..
?
mmm I will try to modify the code ..thanks a lot
8 years ago
For one thing "previous1" and "previous2" are never used in the code...
Also there is nothing stopping the switch statement executing. You should have a loop waiting for a keypress/ release cycle before the switch is executed.