When I click on the button, it just won't stop. It even start before I press the button.
please help "error code"
Thankyou
/*
Two melodies on two buttons and one speaker
Plays a melody determined by button pushed
circuit:
- 8 ohm speaker on digital pin 8
- push button on digital pin 2
- push button on digital pin 7
created 21 Jan 2010
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Tone
*/
#include "pitches.h"
// notes in the melody:
int halloweenMelody[] = {
NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4
};
int starwarsMelody[] = {
NOTE_G3, NOTE_G3, NOTE_G3, NOTE_D4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_D4
};
int halloween = 2;
int starwars = 7;
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
};
int counterDurations[] = {
6, 6, 6, 1, 1, 6, 6, 6, 1, 2, 6, 6, 6, 1, 2, 6, 6, 6, 1
};
void setup(){
//make the button's pin input
pinMode(halloween, INPUT);
pinMode(starwars, INPUT);
}
void loop() {
//if the button is pressed
if (digitalRead(halloween) == 1) {
//iterate over the notes of the melody
for (int thisNote=0; thisNote <20; thisNote++) {
//to calculate the note duration, take one second. Divided by the note type
int noteDuration = 1000 / noteDurations [thisNote];
tone(8, halloweenMelody [thisNote], noteDuration);
//to distinguish the notes, set a minimum time between them
//the note's duration +30% seems to work well
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
//stop the tone playing
noTone(8);
} }
//if the button is pressed
if (digitalRead(starwars) == 1) {
//iterate over the notes of the melody
for (int thisNote=0; thisNote <19; thisNote++) {
//to calculate the note duration, take one second. Divided by the note type
int noteDuration = 1000 / counterDurations [thisNote];
tone(8, starwarsMelody [thisNote], noteDuration);
//to distinguish the notes, set a minimum time between them
//the note's duration +30% seems to work well
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
//stop the tone playing
noTone(8);
}}}