expected unqualified-id before 'else'

//*** 1- Documentation
//This program is used to control a robot car using a app that communicates with Arduino trough a bluetooth module
//L293 Connection
const int motorA1 = 5; // Pin 2 of L293
const int motorA2 = 6; // Pin 7 of L293
const int motorB1 = 10; // Pin 10 of L293
const int motorB2 = 9; // Pin 14 of L293
//Leds connected to Arduino UNO Pin 12
const int lights = 12;
//Buzzer / Speaker to Arduino UNO Pin 3
const int buzzer = 3 ;
//Calculate Battery Level
const float maxBattery = 8.0;// Change value to your max battery voltage level!
int perVolt; // Percentage variable
float voltage = 0.0; // Read battery voltage
int level;
#include <AFMotor.h>

//creates two objects to control the terminal 3 and 4 of motor shield
AF_DCMotor motor1(3);
AF_DCMotor motor2(4);

char command;

void setup()
{
Serial.begin(9600); //Set the baud rate to your Bluetooth module.
}

void loop(){
if(Serial.available() > 0){
command = Serial.read();
Stop(); //initialize with motors stoped
//Change pin mode only if new command is different from previous.
//Serial.println(command);
switch(command){
case ‘F’:
forward();
break;
case ‘B’:
back();
break;
case ‘L’:
left();
break;
case ‘R’:
right();
break;
}
}
}

void forward()
{
motor1.setSpeed(255); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
}

void back()
{
motor1.setSpeed(255);
motor1.run(BACKWARD); //rotate the motor counterclockwise
motor2.setSpeed(255);
motor2.run(BACKWARD); //rotate the motor counterclockwise
}

void left()
{
motor1.setSpeed(255); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(0);
motor2.run(RELEASE); //turn motor2 off
}

void right()
{
motor1.setSpeed(0);
motor1.run(RELEASE); //turn motor1 off
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
}

void Stop()
{
motor1.setSpeed(0);
motor2.run(RELEASE); //turn motor1 off
motor2.setSpeed(0);
motor2.run(RELEASE); //turn motor2 off
}
/Lights*****/
//If state is equal with letter ‘W’, turn leds on or of off
else if (state == ‘W’) {
if (i==0){
digitalWrite(lights, HIGH);
i=1;
}
else if (i==1){
digitalWrite(lights, LOW);
i=0;
}
state=‘n’;
}
/Horn sound*****/
//If state is equal with letter ‘V’, play (or stop) horn sound
else if (state == ‘V’){
if (j==0){
tone(buzzer, 1000);//Speaker on
j=1;
}
else if (j==1){
noTone(buzzer); //Speaker off
j=0;
}
state=‘n’;
}
/Battery******/
//Read battery voltage every 10sec.
currentMillis = millis();
if(currentMillis - (previousMillis) > (interval)) {
previousMillis = currentMillis;
//Read voltage from analog pin A0 and make calibration:
voltage = (analogRead(A0)*5.015 / 1024.0)11.132;
//Calculate percentage…
perVolt = (voltage
100)/ maxBattery;
if (perVolt<=75) { level=0; }
else if (perVolt>75 && perVolt<=80) { level=1; } // Battery level
else if (perVolt>80 && perVolt<=85) { level=2; } //Min ------------------------ Max
else if (perVolt>85 && perVolt<=90) { level=3; } // | 0 | 1 | 2 | 3 | 4 | 5 | >
else if (perVolt>90 && perVolt<=95) { level=4; } // ------------------------
else if (perVolt>95) { level=5; }
Serial.println(level);
}

}

After verifying I got an error 'expected unqualified-id before ‘else’ on the line
‘/Lights*****/
//If state is equal with letter ‘W’, turn leds on or of off
else if (state == ‘W’) {’
pls help… :frowning:

KannanProgram.ino (3.86 KB)

Next time, please use code tags as requested for this Forum. It makes it easier to assist you.

Take a look at the following excerpt from your sketch...

void Stop() { motor1.setSpeed(0); motor2.run(RELEASE); //turn motor1 off motor2.setSpeed(0); motor2.run(RELEASE); //turn motor2 off } /*********************Lights**************************/ //If state is equal with letter 'W', turn leds on or of off else if (state == 'W') {

The function Stop() ends. Then comes a long comment starting with /*** Then another commend starting with //If state...

What comes then? The exact line giving you the error! It does NOT belong to the stop function because the stop function has ended! In fact, it belongs to NOTHING. Thus, the Compiler is asking informing you that something is wrong here...

Thank You for the advice.So could you please tell me what I can do?

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Tom... :)

//*** 1- Documentation
//This program is used to control a robot car using a app that communicates with Arduino trough a bluetooth module
//L293 Connection   
  const int motorA1  = 5;  // Pin  2 of L293
  const int motorA2  = 6;  // Pin  7 of L293
  const int motorB1  = 10; // Pin 10 of L293
  const int motorB2  = 9;  // Pin 14 of L293
//Leds connected to Arduino UNO Pin 12
  const int lights  = 12;
//Buzzer / Speaker to Arduino UNO Pin 3
  const int buzzer = 3 ;   
//Calculate Battery Level
  const float maxBattery = 8.0;// Change value to your max battery voltage level!
  int perVolt;                 // Percentage variable
  float voltage = 0.0;         // Read battery voltage
  int level;
  #include <AFMotor.h>

//creates two objects to control the terminal 3 and 4 of motor shield
AF_DCMotor motor1(3);
AF_DCMotor motor2(4);

char command;

void setup()
{       
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
}

void loop(){
  if(Serial.available() > 0){
    command = Serial.read();
    Stop(); //initialize with motors stoped
    //Change pin mode only if new command is different from previous.   
    //Serial.println(command);
    switch(command){
    case 'F': 
      forward();
      break;
    case 'B': 
       back();
      break;
    case 'L': 
      left();
      break;
    case 'R':
      right();
      break;
    }
  }
}

void forward()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
}

void back()
{
  motor1.setSpeed(255);
  motor1.run(BACKWARD); //rotate the motor counterclockwise
  motor2.setSpeed(255);
  motor2.run(BACKWARD); //rotate the motor counterclockwise
}

void left()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

void right()
{
  motor1.setSpeed(0);
  motor1.run(RELEASE); //turn motor1 off
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
}

void Stop()
{
  motor1.setSpeed(0);
  motor2.run(RELEASE); //turn motor1 off
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

#if 0
/************************Lights*****************************/
  //If state is equal with letter 'W', turn leds on or of off
    else if (state == 'W') {
      if (i==0){ 
         digitalWrite(lights, HIGH);
         i=1;
      }
      else if (i==1){
         digitalWrite(lights, LOW);
         i=0;
      }
      state='n';
    }
    /**********************Horn sound***************************/
  //If state is equal with letter 'V', play (or stop) horn sound
    else if (state == 'V'){
      if (j==0){ 
         tone(buzzer, 1000);//Speaker on
         j=1;
      }
      else if (j==1){
         noTone(buzzer);    //Speaker off
         j=0;
      }
      state='n'; 
    }
     /***********************Battery*****************************/
  //Read battery voltage every 10sec.
    currentMillis = millis();
    if(currentMillis - (previousMillis) > (interval)) {
       previousMillis = currentMillis;
       //Read voltage from analog pin A0 and make calibration:
       voltage = (analogRead(A0)*5.015 / 1024.0)*11.132;
       //Calculate percentage...
       perVolt = (voltage*100)/ maxBattery;
       if      (perVolt<=75)               { level=0; }
       else if (perVolt>75 && perVolt<=80) { level=1; }    //        Battery level
       else if (perVolt>80 && perVolt<=85) { level=2; }    //Min ------------------------   Max
       else if (perVolt>85 && perVolt<=90) { level=3; }    //    | 0 | 1 | 2 | 3 | 4 | 5 | >
       else if (perVolt>90 && perVolt<=95) { level=4; }    //    ------------------------
       else if (perVolt>95)                { level=5; }   
       Serial.println(level);   
    }
   
}
#endif

THANKS A LOT..

I HAVE A SMALL PROBLEM...THE LED IS ON WHEN I CONNECT IT TO ARDUINO AND I CAN'T TURN IT OFF USING BLUETOOTH APP..I CONNECTED ONE TERMINAL OF LED TO GND AND THE OTHER TO ARDUINO PIN 12..PLS HELP....

You did remember a current-limiting resistor, didn't you?

Please turn off your caps lock key.

so what can I do?

so what can I do?

Fix the code, so that it turns off the LED.

We have no clue what your "bluetooth app" (whatever the hell that is) is sending to the Arduino. We have no idea whether you've moved the else block into loop() in the proper place (although I'm guessing that you have not).

My apps sends the following commands to Arduino: Front lights on : W (upper case) front lights off : w (lower case) back lights on : U (upper case) back lights off : u (lower case) horn on : V (upper case) Horn off : v (lower case)

I used the programme given by 'Awol'.

I used the programme given by 'Awol'.

Did you bother looking at the code? Did you see how AWOL structured the code SO IT WOULD COMPILE?

AWOL did NOT fix the issue where YOU put code in the wrong place. All AWOL did was to stop the compiler from compiling the code that is in the wrong place.

When YOU fix the code properly, proper functioning will be possible.

i need ur help with my code. I don't no what I did wrong pls answer...I am not perfect at programming.Could you pretty please modify AWOL code?

what the below code is doing? to which function this code belongs?

#if 0[color=#222222][/color]
/************************Lights*****************************/[color=#222222][/color]
  //If state is equal with letter 'W', turn leds on or of off[color=#222222][/color]
    else if (state == 'W') {[color=#222222][/color]
      if (i==0){ [color=#222222][/color]
         digitalWrite(lights, HIGH);[color=#222222][/color]
         i=1;[color=#222222][/color]
      }[color=#222222][/color]
      else if (i==1){[color=#222222][/color]
         digitalWrite(lights, LOW);[color=#222222][/color]
         i=0;[color=#222222][/color]
      }[color=#222222][/color]
      state='n';[color=#222222][/color]
    }[color=#222222][/color]
    /**********************Horn sound***************************/[color=#222222][/color]
  //If state is equal with letter 'V', play (or stop) horn sound[color=#222222][/color]
    else if (state == 'V'){[color=#222222][/color]
      if (j==0){ [color=#222222][/color]
         tone(buzzer, 1000);//Speaker on[color=#222222][/color]
         j=1;[color=#222222][/color]
      }[color=#222222][/color]
      else if (j==1){[color=#222222][/color]
         noTone(buzzer);    //Speaker off[color=#222222][/color]
         j=0;[color=#222222][/color]
      }[color=#222222][/color]
      state='n'; [color=#222222][/color]
    }[color=#222222][/color]
     /***********************Battery*****************************/[color=#222222][/color]
  //Read battery voltage every 10sec.[color=#222222][/color]
    currentMillis = millis();[color=#222222][/color]
    if(currentMillis - (previousMillis) > (interval)) {[color=#222222][/color]
       previousMillis = currentMillis;[color=#222222][/color]
       //Read voltage from analog pin A0 and make calibration:[color=#222222][/color]
       voltage = (analogRead(A0)*5.015 / 1024.0)*11.132;[color=#222222][/color]
       //Calculate percentage...[color=#222222][/color]
       perVolt = (voltage*100)/ maxBattery;[color=#222222][/color]
       if      (perVolt<=75)               { level=0; }[color=#222222][/color]
       else if (perVolt>75 && perVolt<=80) { level=1; }    //        Battery level[color=#222222][/color]
       else if (perVolt>80 && perVolt<=85) { level=2; }    //Min ------------------------   Max[color=#222222][/color]
       else if (perVolt>85 && perVolt<=90) { level=3; }    //    | 0 | 1 | 2 | 3 | 4 | 5 | >[color=#222222][/color]
       else if (perVolt>90 && perVolt<=95) { level=4; }    //    ------------------------[color=#222222][/color]
       else if (perVolt>95)                { level=5; }   [color=#222222][/color]
       Serial.println(level);   [color=#222222][/color]
    }[color=#222222][/color]
   [color=#222222][/color]
}

This was (by the looks of it) the last piece of your code that is valid.

void Stop()
{
  motor1.setSpeed(0);
  motor2.run(RELEASE); //turn motor1 off
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

After that you had code that is not placed in a function and AWOL fixed that by removing it in a non-destructive way.

How about adding cases in loop() to switch the lights on and off and writing an appropriate function for it?

Coul you please provide me the entire modified code? i'm getting nuts over here looking and solving the code....

i'm getting nuts over here looking and solving the code....

You are not even trying. You have been told, several times, which code is in the wrong place. It is not at all difficult to determine where it really belongs. Try moving it, all by your lonesome self, and, if you get it wrong, post the code and error messages again.

This is NOT the homework hotline.

We all go nuts at occasion if we can't find a bug. Nothing new.

void loop()
{
  if (Serial.available() > 0)
  {
    command = Serial.read();
    Stop(); //initialize with motors stoped
    //Change pin mode only if new command is different from previous.
    //Serial.println(command);
    switch (command)
    {
      case 'F':
        forward();
        break;
      case 'B':
        back();
        break;
      case 'L':
        left();
        break;
      case 'R':
        right();
        break;
      case 'W':
        lightsOn();
        break;
      case 'w':
        lightsOff();
        break;
    }
  }
}

Write the functions that you added. Be aware that the Stop() is always executed, something you might not want (motor will stop if you switch lights on or off). Stop() needs to move to each of the original cases if you want to prevent that.

The below code (from AWOL's code) can be removed as AWOL simply used a #if to 'disable' it

#if 0
...
...
#endif

See, I don't know programming. I just know how things work.I got this entire code from my friend,but it showed some error.So I seeked your help . please don't consider this as a 'homework hotline'.

sterretje: We all go nuts at occasion if we can't find a bug. Nothing new.

void loop()
{
  if (Serial.available() > 0)
  {
    command = Serial.read();
    Stop(); //initialize with motors stoped
    //Change pin mode only if new command is different from previous.
    //Serial.println(command);
    switch (command)
    {
      case 'F':
        forward();
        break;
      case 'B':
        back();
        break;
      case 'L':
        left();
        break;
      case 'R':
        right();
        break;
      case 'W':
        lightsOn();
        break;
      case 'w':
        lightsOff();
        break;
    }
  }
}

Write the functions that you added. Be aware that the Stop() is always executed, something you might not want (motor will stop if you switch lights on or off). Stop() needs to move to each of the original cases if you want to prevent that.

The below code (from AWOL's code) can be removed as AWOL simply used a #if to 'disable' it

#if 0
...
...
#endif

Its showing, 'error: 'lightson' was not declared in this scope