Beginner Programmer: Error Messages and Peer Verification

Hey guys,

I just finished all the included Arduino circuits, and today I built my own custom circuit board. The idea is that if you press one button on the circuit board, a piezo element will play a small song, then the other button will play another song. All the while the LEDs will be blinking. I just wrote up some code and I am getting error messages that I am not sure how to fix. I would also appreciate any tips you can give me. Thanks a Lot.

HERE ARE THE ERROR MESSAGES:

sketch_feb08a:8: error: initializer fails to determine size of 'ledPins'
sketch_feb08a.ino: In function 'int frequency(char)':
sketch_feb08a:24: error: initializer fails to determine size of 'names'
sketch_feb08a:25: error: initializer fails to determine size of 'frequencies'
sketch_feb08a:26: error: 'numNotes' was not declared in this scope
sketch_feb08a.ino: In function 'void setup()':
sketch_feb08a:46: error: expected `;' before ')' token
sketch_feb08a.ino: In function 'void loop()':
sketch_feb08a:66: error: 'songLenth1' was not declared in this scope
sketch_feb08a:88: error: 'songLenth2' was not declared in this scope

HERE IS THE CODE:

//Code Project 1: Piezo Element and Push Button LEDs

//Beginning Variables
const int button1Pin = 12; // pushbutton 1 pin
const int button2Pin = 13; // pushbutton 2 pin
const int buzzerPin = 9; //Piezo Element

int ledPins[] = (5,6,10,11);

int songLength1 = 31; //Twinkle Twinkle Little Star
char notes1[] = "ccggaagffeeddcggffeedggffeedccggaagffeeddc ";
int beats1[] = {1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1};
int tempo1 = 100;

int songLength2 = 28; //Do You Know the Muffin Man
char notes2[] = "gccdecccaddcbgggccdecccddggc ";
int beats2[] = {1,1,1.5,.75,1,1,1.5,.75,1,1,1.5,.75,1,1,1.5,1,1,1,.75,1,1,1.5,1,1,1,1,1,2,1};
int tempo2 = 100;

int frequency(char note) //Piezo Element Notes
{
int i;
const int numnotes = 8;
char names[] = ( 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c' );
int frequencies[] = (262, 294, 330, 349, 392, 440, 494, 523);
for (i=0; i < numNotes; i++)
{
if (names == note)

  • {*
    _ return(frequencies*);_
    _
    }_
    _
    }_
    _
    return(0);_
    _
    }_
    _
    //Ending Variables*_
    //Beginning Setup
    void setup()
    {
    * pinMode(buzzerPin, OUTPUT);*
    * pinMode(button1Pin, INPUT);*
    * pinMode(button2Pin, INPUT);*

* int index;*
* for(index = 0); index <= 7; index++)*
* {*
* pinMode(ledPins[index], OUTPUT);*
* }*
}
//Ending Setup
//Beginning Loop
void loop()
{
* int button1State, button2State; //Push Button*

* button1State = digitalRead(button1Pin); //Push Button*
* button2State = digitalRead(button2Pin); //Push Button*

* if (button1State == HIGH) //If Button1 is pushed, Play "Twinkle Twinkle Little Star"*
* {*
* int i, duration; //Piezo Element*

* for (i = 0; i < songLenth1; i++)*
* {*
duration = beats1 * tempo1;

_ if (notes1 == ' ')
* {
delay(duration);
}
else*

* {
tone(buzzerPin, frequency(notes1), duration);
delay(duration);
}
delay(tempo1/10);
}
}*_

* if (button2State == HIGH) //If Button2 is pushed, Play "Do You Know the Muffin Man"*
* {*
* int i, duration; //Piezo Element*

* for (i = 0; i < songLenth2; i++)*
* {*
duration = beats2 * tempo2;

_ if (notes2 == ' ')
* {
delay(duration);
}
else*

* {
tone(buzzerPin, frequency(notes2), duration);
delay(duration);
}
delay(tempo2/10);
}
}
oneAfterAnotherLoop(); //LED Light Sequence*_

}
//Ending Loop
//Begin LED Light Sequence
void oneAfterAnotherLoop()
{
int index;
int delayTime = 100;
for(index = 0; index <= 7; index++)
* {*
* digitalWrite(ledPins[index], HIGH);*
* delay(delayTime);*
* }*

for(index = 7; index >= 0; index--)
* {*
* digitalWrite(ledPins[index], LOW);*
* delay(delayTime);*
* }*
}
//End LED Light Sequence
//End Code

Initialize arrays using curly brackets:

int ledPins[] = {5,6,10,11};
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c' };
  int frequencies[] ={262, 294, 330, 349, 392, 440, 494, 523};

Spelling ( C++ doesn't care how you spell, the names must match exactly, including case ):

sketch_feb08a:66: error: 'songLenth1' was not declared in this scope
sketch_feb08a:88: error: 'songLenth2' was not declared in this scope

Too many brackets:

for(index = 0); index <= 7; index++)

Fix them and see what other errors you get ( if any ).

Then re-post your code inside code tags ( hit # icon while typing post, place code between the tags it inserts into your post. )

You are a saint pYro_65.
No more error messages, but my code doesn't do quite what I want it to, and I don't know how to rewrite it.

What happens now is it will play both songs without waiting for a button push, and it plays both songs then lights up the leds instead of the leds lighting up the whole time.

Thoughts and comments are much appreciated.

//Code Project 1: Piezo Element and Push Button LEDs

//Beginning Variables
const int button1Pin = 12;  // pushbutton 1 pin
const int button2Pin = 13;  // pushbutton 2 pin
const int buzzerPin = 9;  //Piezo Element

int ledPins[] = {5,6,10,11};

int songLength1 = 31;  //Twinkle Twinkle Little Star
char notes1[] = "ccggaagffeeddcggffeedggffeedccggaagffeeddc ";
int beats1[] = {1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1};
int tempo1 = 200;

int songLength2 = 28;  //Do You Know the Muffin Man
char notes2[] = "gccdecccaddcbgggccdecccddggc ";
int beats2[] = {1,1,1.5,.75,1,1,1.5,.75,1,1,1.5,.75,1,1,1.5,1,1,1,.75,1,1,1.5,1,1,1,1,1,2,1};
int tempo2 = 200;

int frequency(char note)     //Piezo Element Notes
{
  int i;
  const int numnotes = 8;
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  for (i=0; i < numnotes; i++)
  {
    if (names[i] == note)
    {
      return(frequencies[i]);
    }
  }
  return(0);
}
//Ending Variables


//Beginning Setup
void setup() 
{
  pinMode(buzzerPin, OUTPUT);
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  
  int index;
  for(index = 0; index <= 7; index++)
  {
    pinMode(ledPins[index], OUTPUT);
  }
}
//Ending Setup


//Beginning Loop
void loop()
{
  int button1State, button2State;          //Push Button 
  
  button1State = digitalRead(button1Pin);  //Push Button
  button2State = digitalRead(button2Pin);  //Push Button
    
  if (button1State == HIGH)                //If Button1 is pushed, Play "Twinkle Twinkle Little Star"
  {
  int i, duration;                         //Piezo Element
  
  for (i = 0; i < songLength1; i++)
    {
    duration = beats1[i] * tempo1;
    
    if (notes1[i] == ' ')
      {
      delay(duration);
      }
    else
      {
      tone(buzzerPin, frequency(notes1[i]), duration);
      delay(duration);
      }
    delay(tempo1/10);
    }
  }
  

  if (button2State == HIGH)                //If Button2 is pushed, Play "Do You Know the Muffin Man"
  {
  int i, duration;                         //Piezo Element
  
  for (i = 0; i < songLength2; i++)
    {
    duration = beats2[i] * tempo2;
    
    if (notes2[i] == ' ')
      {
      delay(duration);
      }
    else
      {
      tone(buzzerPin, frequency(notes2[i]), duration);
      delay(duration);
      }
    delay(tempo2/10);
    }
  }

  oneAfterAnotherLoop();                    //LED Light Sequence
 
}
//Ending Loop

//Begin LED Light Sequence
void oneAfterAnotherLoop()
{
 int index;
 int delayTime = 100;

 for(index = 0; index <= 7; index++)
  {
   digitalWrite(ledPins[index], HIGH);
   delay(delayTime);
  }
 
 for(index = 7; index >= 0; index--)
  {
   digitalWrite(ledPins[index], LOW);
   delay(delayTime);
  }
} 
//End LED Light Sequence

//End Code

then lights up the leds instead of the leds lighting up the whole time.

You tell it to with the call: oneAfterAnotherLoop();

You could split it into two functions, one with the on loop, the other with the off loop.
Then call the on function at the start of loop() and the off function at the end.

If you want them to gradually turn on while the buzzers are going, you have to design your approach differently.

In the IDE open up the Digital->BlinkWithoutDelay example sketch. It is the staple diet for beginners learning how to run multiple actions together.

So I've been playing around with it today.

I figured out why it wasn't working to the buttons, I just had the buttonstate on HIGH instead of LOW.

I did a little research and want confirmation: the Arduino is not capable of running simultaneous functions (i.e. blinking LEDs and playing the Piezo Element at the same time). Is this true or is there a way?

It is currently programmed to turn on the first two LEDs with button 1 and the second two LEDs with button 2.

Thanks for all the help.

Current Code is below.

//Code Project 1: Piezo Element and Push Button LEDs

//Begin Code

//Begin Variables
const int button1Pin = 12;  //pushbutton 1
const int button2Pin = 13;  //pushbutton 2
const int buzzerPin = 9;    //Piezo Element

const int ledPin1 = 5;      //LED 1
const int ledPin2 = 6;      //LED 2
const int ledPin3 = 10;     //LED 3
const int ledPin4 = 11;     //LED 4

int songLength1 = 43;  //Twinkle Twinkle Little Star
char notes1[] = "ccggaagffeeddcggffeedggffeedccggaagffeeddc ";
int beats1[] = {1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1};
int tempo1 = 300;

int songLength2 = 27;  //Row Row Row Your Boat
char notes2[] = "cccdeedefgjjjgggeeecccgfedc ";
int beats2[] = {3,3,2,1,3,2,1,2,1,3.5,1,1,1,1,1,1,1,1,1,1,1,1,2,1,2,1,3,1};
int tempo2 = 250;

int frequency(char note)     //Piezo Element Notes and Frequencies
{
  int i;
  const int numnotes = 8;
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'j' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  for (i=0; i < numnotes; i++)
  {
    if (names[i] == note)
    {
      return(frequencies[i]);
    }
  }
  return(0);
}
//End Variables


//Begin Setup
void setup() 
{
  pinMode(buzzerPin, OUTPUT);
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  
}
//End Setup


//Begin Loop
void loop()
{
  int button1State, button2State;          //Push Button 
  
  button1State = digitalRead(button1Pin);  //Push Button 1 State
  button2State = digitalRead(button2Pin);  //Push Button 2 State
    
  if ((button1State == LOW)                //If Button 1 is pushed, Play "Twinkle Twinkle Little Star"
  && !
     (button2State == LOW))
  {
    
  digitalWrite(ledPin1, HIGH);             //Turn on first two LEDs
  digitalWrite(ledPin2, HIGH);
  
  int i, duration;                         //Piezo Element
  
  for (i = 0; i < songLength1; i++)
    {
    duration = beats1[i] * tempo1;
    
    if (notes1[i] == ' ')
      {
      delay(duration);
      }
    else
      {
      tone(buzzerPin, frequency(notes1[i]), duration);
      delay(duration);
      }
    delay(tempo1/10);
    }
    
  }
  
  else
  {
    digitalWrite(ledPin1, LOW);            //Keep LEDs off
    digitalWrite(ledPin2, LOW);
  }
  

  if ((button2State == LOW)                //If Button 2 is pushed, Play "Do You Know the Muffin Man"
  && !
     (button1State == LOW))
  {
    
  digitalWrite(ledPin3, HIGH);             //Turn on next two LEDs
  digitalWrite(ledPin4, HIGH);
  
  int i, duration;                         //Piezo Element
  
  for (i = 0; i < songLength2; i++)
    {
    duration = beats2[i] * tempo2;
    
    if (notes2[i] == ' ')
      {
      delay(duration);
      }
    else
      {
      tone(buzzerPin, frequency(notes2[i]), duration);
      delay(duration);
      }
    delay(tempo2/10);
    }
    
  }
  
  else
  {
    digitalWrite(ledPin3, LOW);              //Keep LEDs off
    digitalWrite(ledPin4, LOW);
  }

}
//End Loop



//End Code

Using delay(), no, you can't make it do more than one thing at a time.

Look at the example sketches "Blink without delay" and "Debounce".

I did a little research and want confirmation: the Arduino is not capable of running simultaneous functions (i.e. blinking LEDs and playing the Piezo Element at the same time). Is this true or is there a way?

Yes, it's true. The Arduino does not do multitasking. That requires an operating system. True multitasking also requires two or more processors.

However, many people sat "at the same time" when using time scales that are many orders of magnitude longer than what the Arduino uses.

It is possible to turn a pin on and off while doing other things "at the same (human-scale) time".

Note that !(buttonState==LOW) is the same as (buttonState==HIGH).

Note that !(buttonState==LOW) is the same as (buttonState==HIGH).

Noted. I didn't know that before.

It is possible to turn a pin on and off while doing other things "at the same (human-scale) time".

Is this at a similar experience level to the rest of what I've been doing? And if it is, can you steer me in the right direction?

One other thing I would like to change: When I press a button, a song plays, but while the song is playing I cannot press the other button to play the other song (That is to say, I cannot interrupt the playing song with another song). I've tried a couple different things, but none of them worked. Any ideas?

It won't respond to a button while playing, because you don't have it checking for a button press while it is playing.

Take a look at "Blink Without Delay" and "Debounce".