Hello, I have another issue. I cannot get this code to work-
#include <Servo.h>
Servo servol;
const int motorPin = 11;
const int buzzerPin = 9;
int songLength = 18;
char notes[] = "cdfda ag cdfdg gf ";
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int tempo = 150;
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
servol.attach(10);
}
void loop()
{
int position;
int g = 0;
for (g = 0; g < 5; g++) {
servol.write(180);
delay(1000);
servol.write(0);
delay(1000);
}
int i =0;
for (i = 0; i < songLength; i++) // step through the song arrays
{
int duration = beats[i] * tempo; // length of note/rest in ms
if (notes[i] == ' ') // is this a rest?
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
int frequency(char note) ;
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}
// We only want to play the song once, so we'll pause forever:
while(true){}
// If you'd like your song to play over and over,
// remove the above statement
}
int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (int i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return(frequencies[i]); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0
int position;
}
int songlength2 = 23;
char notes2[] = "a g f e efffefffa g f e fff agfe";
int beats2[] = {1,1,1,1,.5,.5,1,2,.5,.5,1,2,1,1,1,1,.5,.5,1,6,8,8,8};
int tempo2 = 104;
for(h=0; h <= 255; h++){
int duration = beats2[h] * tempo; // length of note/rest in ms
if (notes[i] == ' ') // is this a rest?
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
tone(buzzerPin, frequency(notes[h]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}
// We only want to play the song once, so we'll pause forever:
while(true){}
// If you'd like your song to play over and over,
// remove the above statement
int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (j = 0; j < numNotes; j++) // Step through the notes
{
if (names[j] == note) // Is this the one?
{
return(frequencies[j]); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0
int position;
}
The errors I am receiving are these.
Arduino_1:105: error: expected unqualified-id before 'for'
Arduino_1:105: error: expected constructor, destructor, or type conversion before '<=' token
Arduino_1:105: error: expected constructor, destructor, or type conversion before '++' token