Hi,
I'm brand new to Arduino. I am creating a paper automata and as the handle is cranked an ir sensor is activated on each turn and a song is played one note at a time. my code works the way i want except i'd like it to start over after all notes are played but instead it just plays a single continuous note...not sure what to do to fix it. Would really appreciate advice
//Setting up the notes values
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
//Setting pin values
const int buzzerPin = 8;
const int buttonPin = 11;
//LEDs not neccesary but carried over from other program
const int ledPin1 = 12;
const int ledPin2 = 13;
//Creating counters
int counter = 0;
int presses = 0;
//song notes and timing --- one I'm using for notes.
int notesAll[] = {f, f, f, g, g, a, cH, a, f, c, f, f, g, g, a, f,c, f, f, g, g, a, cH, a, f, dH, g, aS, a, f, f, f, g, g, a, cH, a, f, c, f, f, g, g, a, f,c, f, f, g, g, a, cH, a, f, dH, g, aS, a, f, f, f, f, g, g, a, cH, a, f, c, f, f, g, g, a, f,c, f, f, g, g, a, cH, a, f, dH, g, aS, a, f, f, f, g, g, a, cH, a, f, c, f, f, g, g, a, f,c, f, f, g, g, a, cH, a, f, dH, g, aS, a, f};
int timeAll[] = {500, 500, 350, 500, 350, 350, 350, 350, 500, 350, 500, 350, 500, 350, 500, 500, 500, 350, 500, 350, 350, 350, 350, 600, 600, 500, 350, 600, 600, 500, 500, 350, 500, 350, 350, 350, 350, 500, 350, 500, 350, 500, 350, 500, 500, 500, 350, 500, 350, 350, 350, 350, 600, 600, 500, 350, 600, 600, 500, 500, 350, 500, 350, 350, 350, 350, 500, 350, 500, 350, 500, 350, 500, 500, 500, 500, 350, 500, 350, 350, 350, 350, 600, 600, 500, 350, 600, 600, 500, 500, 350, 500, 350, 350, 350, 350, 500, 350, 500, 350, 500, 350, 500, 500, 500, 500, 350, 500, 350, 350, 350, 350, 600, 600, 500, 350, 600, 600};
//Setting up
void setup()
{
//Setup pin modes
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
//Creating a function to turn the buzzer on and off with a specific note and time
void beep(int note, int duration)
{
//Play tone on buzzerPin
tone(buzzerPin, note, duration);
//Play different LED depending on value of 'counter'
if (counter % 2 == 0)
{
digitalWrite(ledPin1, HIGH);
delay(duration);
digitalWrite(ledPin1, LOW);
}
else
{
digitalWrite(ledPin2, HIGH);
delay(duration);
digitalWrite(ledPin2, LOW);
}
//Stop tone on buzzerPin
noTone(buzzerPin);
delay(50);
//Increment counter
counter++;
}
//Main program loop
void loop()
{
while(1)
//Check for button press
if(digitalRead(buttonPin) == LOW){
presses = presses + 1;
beep(notesAll[presses],timeAll[presses]);
//I would put a delay here but the beep does the delay for me.
}
}