I have a problem with getting my buzzer to only do the tones I programmed in once. I know I have to add some sort of code but I'm relatively new to this so I don't really know. I want it to start playing the tones I have in the code once I press the button. After that it should immediately stop, without looping everything until I press the button again. Basically I have these LEDs that light up in a row with every time I press my button and once the last LED lights up, the buzzer should play its melody. After that, I can press my button again and the whole thing starts from the beginning. If anyone could help, I'd be more than grateful!
This is my code:
#define button 3
#define LED 1
#define twoLED 2
#define threeLED 4
#define fourLED 5
#define fiveLED 6
#define sixLED 7
#define sevenLED 8
#define eightLED 9
#define nineLED 10
#define tenLED 11
int state = 0;
int old = 0;
int buttonPoll = 0;
void setup() {
pinMode (3,INPUT);
pinMode (11,OUTPUT);
pinMode(LED, OUTPUT);
pinMode(twoLED, OUTPUT);
pinMode(threeLED, OUTPUT);
pinMode(fourLED, OUTPUT);
pinMode(fiveLED, OUTPUT);
pinMode(sixLED, OUTPUT);
pinMode(sevenLED, OUTPUT);
pinMode(eightLED, OUTPUT);
pinMode(nineLED, OUTPUT);
pinMode(tenLED, OUTPUT);
digitalWrite(LED, LOW);
digitalWrite(twoLED,LOW);
digitalWrite(threeLED,LOW);
digitalWrite(fourLED, LOW);
digitalWrite(fiveLED, LOW);
digitalWrite(sixLED, LOW);
digitalWrite(sevenLED, LOW);
digitalWrite(eightLED, LOW);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
}
void loop() {
buttonPoll = digitalRead (3);
if (buttonPoll ==1) {
delay (50);
buttonPoll = digitalRead (3);
if (buttonPoll == 0) {
state = old + 1;
}
}
else {
delay(100);
}
switch (state) {
case 1:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,LOW);
digitalWrite(threeLED,LOW);
digitalWrite(fourLED, LOW);
digitalWrite(fiveLED, LOW);
digitalWrite(sixLED, LOW);
digitalWrite(sevenLED, LOW);
digitalWrite(eightLED, LOW);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
old = state;
break;
case 2:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,HIGH);
digitalWrite(threeLED,LOW);
digitalWrite(fourLED, LOW);
digitalWrite(fiveLED, LOW);
digitalWrite(sixLED, LOW);
digitalWrite(sevenLED, LOW);
digitalWrite(eightLED, LOW);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
old = state;
break;
case 3:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,HIGH);
digitalWrite(threeLED,HIGH);
digitalWrite(fourLED, LOW);
digitalWrite(fiveLED, LOW);
digitalWrite(sixLED, LOW);
digitalWrite(sevenLED, LOW);
digitalWrite(eightLED, LOW);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
old = state;
break;
case 4:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,HIGH);
digitalWrite(threeLED,HIGH);
digitalWrite(fourLED, HIGH);
digitalWrite(fiveLED, LOW);
digitalWrite(sixLED, LOW);
digitalWrite(sevenLED, LOW);
digitalWrite(eightLED, LOW);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
old = state;
break;
case 5:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,HIGH);
digitalWrite(threeLED,HIGH);
digitalWrite(fourLED, HIGH);
digitalWrite(fiveLED, HIGH);
digitalWrite(sixLED, LOW);
digitalWrite(sevenLED, LOW);
digitalWrite(eightLED, LOW);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
old = state;
break;
case 6:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,HIGH);
digitalWrite(threeLED,HIGH);
digitalWrite(fourLED, HIGH);
digitalWrite(fiveLED, HIGH);
digitalWrite(sixLED, HIGH);
digitalWrite(sevenLED, LOW);
digitalWrite(eightLED, LOW);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
old = state;
break;
case 7:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,HIGH);
digitalWrite(threeLED,HIGH);
digitalWrite(fourLED, HIGH);
digitalWrite(fiveLED, HIGH);
digitalWrite(sixLED, HIGH);
digitalWrite(sevenLED, HIGH);
digitalWrite(eightLED, LOW);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
old = state;
break;
case 8:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,HIGH);
digitalWrite(threeLED,HIGH);
digitalWrite(fourLED, HIGH);
digitalWrite(fiveLED, HIGH);
digitalWrite(sixLED, HIGH);
digitalWrite(sevenLED, HIGH);
digitalWrite(eightLED, HIGH);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
old = state;
break;
case 9:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,HIGH);
digitalWrite(threeLED,HIGH);
digitalWrite(fourLED, HIGH);
digitalWrite(fiveLED, HIGH);
digitalWrite(sixLED, HIGH);
digitalWrite(sevenLED, HIGH);
digitalWrite(eightLED, HIGH);
digitalWrite(nineLED, HIGH);
digitalWrite(tenLED, LOW);
old = state;
break;
case 10:
digitalWrite(LED, HIGH);
digitalWrite(twoLED,HIGH);
digitalWrite(threeLED,HIGH);
digitalWrite(fourLED, HIGH);
digitalWrite(fiveLED, HIGH);
digitalWrite(sixLED, HIGH);
digitalWrite(sevenLED, HIGH);
digitalWrite(eightLED, HIGH);
digitalWrite(nineLED, HIGH);
digitalWrite(tenLED, HIGH);
old = state;
tone (11,261);
delay(250);
tone(11, 330);
delay(250);
tone(11,391.99);
delay(250);
tone(11,440);
delay(250);
tone(11, 391.995);
delay(250);
tone(11, 330);
delay(250);
}
break;
default:
digitalWrite(LED, LOW);
digitalWrite(twoLED,LOW);
digitalWrite(threeLED,LOW);
digitalWrite(fourLED, LOW);
digitalWrite(fiveLED, LOW);
digitalWrite(sixLED, LOW);
digitalWrite(sevenLED, LOW);
digitalWrite(eightLED, LOW);
digitalWrite(nineLED, LOW);
digitalWrite(tenLED, LOW);
old = 0;
break;
}
}