Hi
I got a problem with my arduino uno, So i want to make a circuit with 2 button, 1 led and a buzzer, when i push on a button the led and the melody work, and when i push on the second button that turn off the led and the melody but that don't work,
int ledPin = 13;
int inputPin1 = 3;
int inputPin2 = 2;
int speakerPin = 9;
int a = 0;
int length = 15;
char notes[] = "ccggaagffeeddc ";
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 200;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(inputPin1, INPUT);
pinMode(inputPin2, INPUT);
}
void loop() {
if (digitalRead(inputPin1) == LOW) {
digitalWrite(ledPin, LOW);
a=0;
}
else if (digitalRead(inputPin2) == HIGH) {
digitalWrite(ledPin, HIGH);
digitalWrite(speakerPin, HIGH);
a=1;
}
if (a == 1){
for (int i = 0; i < length; i++){
if (digitalRead(inputPin2)==HIGH)
{i=20;}
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
}
delay(tempo / 2);
}
}
Here i need to still pushing on the button for turn on the buzzer.