quickfix code, pls help

hi, im trying to get this code working and its giving me an "unqualified id before “else” " error, and i’m not sure why, i tried fixing it and everything, but for now, ill keep fidgiting with it…
but if you can help me adn share your insights, that would be great!!

code:

int val = 0;
void setup()
{
 Serial.begin(9600);
  backlightOn();

}

void loop()
{
val = analogRead(0)/4.011764;
if (val > 252)  
 {
  selectLineOne();
  delay(50);
  Serial.print("It is very dark ");
  selectLineTwo();
  delay(50);
  Serial.print("in this room.");
  delay(100);
 }
 else if (val > 246 && val < 252)
 {
   selectLineOne();
   delay(50);
  Serial.print("It is pretty dark   ");
  selectLineTwo();
  delay(50);
  Serial.print("in this room.   ");
  delay(100);
 }
 else if (val > 230 && val < 246)
 {
   selectLineOne();
   delay(50);
  Serial.print("It is pretty   ");
  selectLineTwo();
  delay(50);
  Serial.print("bright here.   ");
  delay(100);
 }
else if (val < 230)
{
  selectLineOne();
   delay(50);
  Serial.print("It is bright     ");
  selectLineTwo();
  delay(50);
  Serial.print("in this room.   ");
  delay(100);
}
}

void selectLineOne(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(128, BYTE);    //position
}
void selectLineTwo(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(192, BYTE);    //position
}
void clearLCD(){
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(0x01, BYTE);   //clear command.
}
void backlightOn(){  //turns on the backlight
    Serial.print(0x7C, BYTE);   //command flag for backlight stuff
    Serial.print(157, BYTE);    //light level.
}
void backlightOff(){  //turns off the backlight
    Serial.print(0x7C, BYTE);   //command flag for backlight stuff
    Serial.print(128, BYTE);     //light level for off.
}
void serCommand(){   //a general function to call the command flag for issuing all other commands   
  Serial.print(0xFE, BYTE);
}

thanks in advance to any help!
-int big93 = love you;

:slight_smile:

I don’t think there is anything wrong with your code. I expect the error is the arduino parser getting confused. The code below compiles ok and the only difference is a few spaces were added in the last else if statment.

int val = 0;
void setup()
{
 Serial.begin(9600);
  backlightOn();

}

void loop()
{
val = analogRead(0)/4.011764;
if (val > 252)  
 {
  selectLineOne();
  delay(50);
  Serial.print("It is very dark ");
  selectLineTwo();
  delay(50);
  Serial.print("in this room.");
  delay(100);
 }
 else if (val > 246 && val < 252)
 {
   selectLineOne();
   delay(50);
  Serial.print("It is pretty dark   ");
  selectLineTwo();
  delay(50);
  Serial.print("in this room.   ");
  delay(100);
 }
 else if (val > 230 && val < 246)
 {
   selectLineOne();
   delay(50);
  Serial.print("It is pretty   ");
  selectLineTwo();
  delay(50);
  Serial.print("bright here.   ");
  delay(100);
}
 else if (val < 230)
 {
  selectLineOne();
   delay(50);
  Serial.print("It is bright     ");
  selectLineTwo();
  delay(50);
  Serial.print("in this room.   ");
  delay(100);
 }
}

void selectLineOne(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(128, BYTE);    //position
}
void selectLineTwo(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(192, BYTE);    //position
}
void clearLCD(){
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(0x01, BYTE);   //clear command.
}
void backlightOn(){  //turns on the backlight
    Serial.print(0x7C, BYTE);   //command flag for backlight stuff
    Serial.print(157, BYTE);    //light level.
}
void backlightOff(){  //turns off the backlight
    Serial.print(0x7C, BYTE);   //command flag for backlight stuff
    Serial.print(128, BYTE);     //light level for off.
}
void serCommand(){   //a general function to call the command flag for issuing all other commands   
  Serial.print(0xFE, BYTE);
}

Hi, the problem goes away when you move the loop() function right to the bottom of the sketch, so that all the methods called from loop() are above it. Might be a case of the way the arduino-ide creates prototypes for the functions automatically.

I had strange compiler messages like yours several times so I got into the habit of putting the loop()-function last in all my sketches.

Eberhard

Hi, just read the reply from "mem" and even a simple "Menu->Tools->Autoformat" make the error go away !?

Eberhard

thanks alot guys.... i got the code working strangly by simply removing the else commands, so they were all "if" commands insead of most of them being "else if".

this code works nicly, and i like it, it works well, ill post a vid later tonight when the sun has gone by by, and i can take a vid in my dining room where the light is dimmable....