Go Down

Topic: "expected unqualified-id before 'else'" (Read 1 time) previous topic - next topic

aarong

What is the compiler trying to communicate when it indicates that "expected unqualified-id before 'else'"?

Attached is the full code. Everything up to the first else compiles. Not sure about the code thereafter.


Code: [Select]
int led = 13;                                           //initializes PIN variable for LED 13 is LED default
int count = 0;                                          //count variable for counting array placement
int cycleTally = 0;                                     //variable to track the number of times an array has been cycle through
int timeBlock = 250;                                    //discrete variable of 250 ms for pauses and led
int timeBlockArrayShort[] = {timeBlock, timeBlock};     //morse code short constant
int timeBlockArrayLong[] = {timeBlock, timeBlock * 3};  //morse code long constant
int timeBlockPause = timeBlock * 3;                     //morse code pause between words

void setup() {                                          //initializes LED    
   pinMode(led,OUTPUT);
}

void loop() {                                          //starts program
 if (cycleTally <= 3) {                               //establishes test that if cycle tally is less than or equal to 3, than the LED flash will loop 3 times
   for (count = 0, cycleTally = 0; count<3 && cycleTally==3;count++,cycleTally++) {   //simutaneously tracks the array count and times that the LED has flashed this is an 'S'
     digitalWrite(led, LOW);                          //LED is dim for 250 ms
     delay (timeBlockArrayShort[count]);              //count = 1 in the array, 250 ms
     digitalWrite(led, HIGH);                         //LED is bright for 250 ms
     delay (timeBlockArrayShort[count + 1]);          //count = 2 in the array, 250 ms
   }
   }
 }
 else                                               //once LED has flashed 3 times, then it continues to a Morse Code 'O' <<---where the error compiles
   if (cycleTally <= 6) {                            //establishes test that if cycle tally is less than or equal to 6, than the LED will flash 3 times
     for (count = 0, cycleTally = 4; count<3 && cycleTally==6;count++,cycleTally++) {
     digitalWrite(led, LOW);
     delay (timeBlockArrayLong[count]);
     digitalWrite(led, HIGH);
     delay (timeBlockArrayLong[count + 1]);
     }
     }
       
         else {                                              //once LED has flashed 3 times, then it continues to a Morse Code 'O'
   if (cycleTally <= 9) {                            //establishes test that if cycle tally is less than or equal to 6, than the LED will flash short burst 3 times
   for (count = 0, cycleTally = 0; count<3 && cycleTally==3;count++,cycleTally++) {   //simutaneously tracks the array count and times that the LED has flashed this is an 'S'
     digitalWrite(led, LOW);                          //LED is dim for 250 ms
     delay (timeBlockArrayShort[count]);              //count = 1 in the array, 250 ms
     digitalWrite(led, HIGH);                         //LED is bright for 250 ms
     delay (timeBlockArrayShort[count + 1]);          //count = 2 in the array, 250 ms
   }
   }  
     else {                                          //official pause between SOS's
             digitalWrite(led, LOW);
             delay (timeBlockPause);
     }
 }
}


PaulS

Put every { on a new line. Use Tools + Auto Format to properly format the code, and you'll see what the problem is.

How can people write code like that mess, anyway. It hurts my eyes to look at code running all over the place like that. I like neatness and order.
The art of getting good answers lies in asking good questions.

aarong

:P i didn't mind my mess. my level of thinking hasn't gotten to http://arduino.cc/en/Hacking/LibraryTutorial on the SOS.

Thanks for the quick fix in the program.

PeterH


:P i didn't mind my mess.



Yeah, but it's making it a lot harder than necessary to spot problems in your code. For example you weren't able to spot a structural problem which becomes obvious when your code is laid out properly. We don't lay the code out carefully because we're neat freaks - we do it because it makes software development easier and quicker.

aarong

#4
Sep 06, 2013, 09:23 pm Last Edit: Sep 06, 2013, 09:28 pm by CrossRoads Reason: 1
I cleaned it up quite a bit. I agree with you. This was my first time programming on Arduino, so I didn't mind my mess. However, I hated it after loading the sketch because the first attempt failed to light the LED. Laughing at the learning process.

Again Thanks.

Below is the cleaned up code which could be shortened. It works.  :smiley-surprise:
Code: [Select]

// Don't Forget Code Tags Next Post - Moderator!

int pin = 13;

void setup()
{
 pinMode(pin, OUTPUT);
}

void loop()
{
 s();
 o();
 s();
 longPause();
}

void s()
{
 dot();
 dot();
 dot();
}

void o()
{
 dash();
 dash();
 dash();
}

void longPause()
{
 int timeBlock = 250;
 delay(timeBlock*3);
}
void dot()
{
 int timeBlock = 250;
 digitalWrite(pin, 0);
 delay(timeBlock);
 digitalWrite(pin, HIGH);
 delay(timeBlock);
}

void dash()
{
 int timeBlock = 250;
 digitalWrite(pin, 0);
 delay(timeBlock);
 digitalWrite(pin, HIGH);
 delay(timeBlock*3);
}

PaulS

Code: [Select]
  digitalWrite(pin, 0);
  delay(timeBlock);
  digitalWrite(pin, HIGH);

You don't like LOW?

Code: [Select]
  int timeBlock = 250;
  int timeBlock = 250;
  int timeBlock = 250;

Suppose you want to change the time. This way, you have to make the change in three place. I'd miss one of them, for sure.
The art of getting good answers lies in asking good questions.

Go Up