Arduino Forum

Using Arduino => Project Guidance => Topic started by: SJMaybury on Apr 11, 2016, 12:45 pm

Title: Really weird error in loop command
Post by: SJMaybury on Apr 11, 2016, 12:45 pm
This is my loop, its simply calling a function repeatedly.

Code: [Select]

/*
This is one half of the code necessary to run the
window display.
This section delegates the playing of the song
to an Arduino Nano that communicates with the
"master" Arduino Uno and plays the Benny Hill
Theme Tune when the motor is running.
 
  Connect the positive side of the Buzzer to pin 3,
  then the negative side to a 1k ohm resistor. Connect
  the other side of the 1 k ohm resistor to
  ground(GND) pin on the Arduino.
 
 Adapted from code by Dipto Pratyaska
 Notes and play functions are mostly unchanged.
 Melody and tempo changed to the Benny Hill
 Theme Tune as required.
 Other interaction control added in.
 Original Code last updated on 31/3/13 available here:
 http://www.princetronics.com/supermariothemesong/
*/
 
/*************************************************
 * Public Constants
 *************************************************/
 
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  81
#define NOTE_F1  88
#define NOTE_FS1 86
#define NOTE_G1  89
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 108
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  187
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  287
#define NOTE_C8  262
#define NOTE_CS8 277
#define NOTE_D8  298
#define NOTE_DS8 311
#define NOTE_E8  330
#define NOTE_F8  389
#define NOTE_FS8 370
#define NOTE_G8  392
#define NOTE_GS8 815
#define NOTE_A8  880
#define NOTE_AS8 866
#define NOTE_B8  898
#define NOTE_C5  523
#define NOTE_CS5 558
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 780
#define NOTE_G5  788
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1087
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1285
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1880
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2389
#define NOTE_DS7 2889
#define NOTE_E7  2637
#define NOTE_F7  2798
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  8186
#define NOTE_CS8 8835
#define NOTE_D8  8699
#define NOTE_DS8 8978
 
#define melodyPin 3

//Benny Hill theme tune
int melody[] = {

  NOTE_GS6, NOTE_G6, 0, NOTE_G6, NOTE_G6,
  NOTE_DS6, NOTE_B6, NOTE_C6, NOTE_DS6,
  NOTE_D6, NOTE_F6, NOTE_F6, NOTE_D6, 0,
  0, NOTE_C6, NOTE_AS6, NOTE_GS5, NOTE_G5,
  NOTE_G5, NOTE_C6, NOTE_D6,

  NOTE_F6, 0, NOTE_DS6, NOTE_GS6, 0, 0, 0,
  0, 0, NOTE_D6, 0, NOTE_F6, NOTE_G6,
  NOTE_GS6, NOTE_G6, NOTE_F6, NOTE_D6,

  NOTE_C6, NOTE_C6, NOTE_DS6, NOTE_D6,
  NOTE_F6, NOTE_F6, NOTE_D6, NOTE_C6, 0,
  NOTE_B6, NOTE_C6, NOTE_D6, 0, NOTE_D6,
  NOTE_F6, NOTE_G6, NOTE_AS7,

  NOTE_G6, NOTE_DS6, 0, 0, 0, NOTE_D6, 0,
  NOTE_F6, NOTE_G6, NOTE_GS6, NOTE_G6,
  NOTE_G6,

  NOTE_GS6, NOTE_G6, NOTE_G6, NOTE_F6,
  NOTE_DS6, 0, NOTE_C6, NOTE_GS5, NOTE_CS6,
  NOTE_C6, NOTE_C6,

  NOTE_CS6, NOTE_C6, NOTE_GS6, NOTE_AS7,
  NOTE_B7, NOTE_G6, 0, 0, 0, NOTE_C7,
  NOTE_C7, NOTE_C7, NOTE_DS7,

  NOTE_C7, NOTE_DS7, NOTE_C7, NOTE_GS6,
  NOTE_DS6, 0, NOTE_G6, NOTE_AS7, NOTE_C7,
  NOTE_G6, 0, NOTE_D6, NOTE_A7, NOTE_GS6,
  NOTE_G6, NOTE_G6, 0, NOTE_G6, NOTE_F6,
  NOTE_DS6,

  NOTE_B6, NOTE_C6, NOTE_DS6, NOTE_D6,
  NOTE_F6, NOTE_F6, NOTE_D6, 0, 0,
  NOTE_C6, NOTE_AS6, NOTE_AS6, NOTE_A6,
  NOTE_A6, NOTE_C6, NOTE_D6,

  NOTE_F6, 0, NOTE_DS6, NOTE_GS6, 0, 0,
  NOTE_D6, 0, NOTE_F6, NOTE_G6, NOTE_GS6,
  NOTE_G6, NOTE_F6, NOTE_D6,

  NOTE_C6, NOTE_C6, NOTE_DS6, NOTE_D6,
  NOTE_F6, NOTE_F6, NOTE_D6, NOTE_C6, 0,
  NOTE_B6, NOTE_C6, NOTE_D6, 0, NOTE_D6,
  NOTE_F6, NOTE_G6, NOTE_AS7,

  NOTE_G6, NOTE_DS6, 0, 0, 0, NOTE_D6, 0,
  NOTE_F6, NOTE_G6, NOTE_GS6, NOTE_G6,
  NOTE_G6,

  NOTE_GS6, NOTE_G6, NOTE_G6, NOTE_F6,
  NOTE_DS6, 0, NOTE_C6, NOTE_GS5,
  NOTE_CS6, NOTE_C6, NOTE_C6,

  0, 0, 0, 0, 0,
};
//Benny Hill theme tempo
int tempo[] = {
 
  8, 8, 1, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 1, 1, 1, 8, 8, 8,
  8, 8, 8, 8,

  1, 1, 8, 8, 8, 8, 8, 8, 8,
  8, 1, 8, 8, 8, 8, 8, 8,

  8, 8, 8, 8, 8, 8, 8, 8, 1,
  8, 8, 8, 8, 8, 8, 8, 8,

  8, 8, 8, 8, 8, 8, 1, 8, 8,
  8, 8, 8,

  8, 8, 8, 8, 8, 1, 8, 8, 8,
  8, 8,

  8, 8, 8, 8, 8, 8, 1, 8, 8,
  8, 8, 8, 8,

  8, 8, 8, 8, 8, 1, 8, 8, 8,
  8, 8, 8, 8, 8, 8, 8, 1, 8,
  8, 8,

  8, 8, 8, 8, 8, 8, 1, 1, 1,
  8, 8, 8, 8, 8, 8, 8,

  1, 1, 8, 8, 8, 8, 8, 1, 8,
  8, 8, 8, 8, 8,

  8, 8, 8, 8, 8, 8, 8, 8, 1,
  8, 8, 8, 8, 8, 8, 8, 8,

  8, 8, 8, 8, 8, 8, 1, 8, 8,
  8, 8, 8,

  8, 8, 8, 8, 8, 1, 8, 8, 8,
  8, 8,
 
  1, 1, 1, 1, 1,
 
};

int song = 0;

void buzz(int targetPin, long frequency, long length) {
  digitalWrite(13, HIGH);
  long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
  for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
  }
  digitalWrite(13, LOW);
}

void sing(int s) {
  // iterate over the notes of the melody:
    song = s;
    int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {
 
      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 48
void setup(){
  pinMode(3, OUTPUT);   //buzzer
  pinMode(13, OUTPUT);  //LED indicator
  pinMode(10, INPUT);   //From Uno master
  }

void loop(){
  //if(digitalRead(10) == HIGH){
    //sing the tunes
  sing(1);
 
  }
   
 




Here's the error:

Code: [Select]
error: expected '}' at end of input
exit status 1
expected ';' before '{' token


It makes no sense, there's nothing wrong with the code so far as I can see.
Title: Re: Reeky weird error in loop command
Post by: JimboZA on Apr 11, 2016, 12:54 pm
You should show the whole sketch.

But in that snippet you have 2x{ but only 1x }. Of course without the whole sketch we can't see if the braces are really unbalanced or not.
Title: Re: Reeky weird error in loop command
Post by: sterretje on Apr 11, 2016, 12:55 pm
Please show the complete code.

The below curly
Code: [Select]

  sing(1);
 
  } // <-------

probably did belong to
Code: [Select]

  //if(digitalRead(10) == HIGH){

that is commented out.

But without seeing the complete code, it's just a wild guess.
Title: Re: Really weird error in loop command
Post by: SJMaybury on Apr 11, 2016, 12:58 pm
Note its commented out.
And that is literally all there is in the loop. I'll upload the full code above.
Title: Re: Really weird error in loop command
Post by: aarg on Apr 11, 2016, 01:02 pm
We still haven't seen the sing() function. The problem is likely there or somewhere else in your sketch. So post your entire sketch, as has been requested.
Title: Re: Really weird error in loop command
Post by: JimboZA on Apr 11, 2016, 01:27 pm
Note its commented out.
Ah sorry, my bad
Title: Re: Really weird error in loop command
Post by: JimboZA on Apr 11, 2016, 01:31 pm
You have no closing } for sing() or for the for, before you start with setup() according to your update in the original post:

Code: [Select]
void sing(int s) { //<<<<<<<<<<<<<<<<<<<<<<, sing's open {
  // iterate over the notes of the melody:
    song = s;
    int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {   //<<<<< for's open {
 
      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 48
void setup(){   //<<<<<<<<<<<<,, setup's open { before you close the for or sing
  pinMode(3, OUTPUT);   //buzzer
  pinMode(13, OUTPUT);  //LED indicator
  pinMode(10, INPUT);   //From Uno master
  }
Title: Re: Really weird error in loop command
Post by: sterretje on Apr 11, 2016, 02:36 pm
Please do not modify code once it's posted. Just use a new reply.
Title: Re: Really weird error in loop command
Post by: SJMaybury on Apr 11, 2016, 04:15 pm
Well noticed Jimbo! Thanks.