How to correct "expected unqualified-id before 'if' and else

Hi .
This is my first Arduino project, it is an insect robot from the book "make Arduino bots and gadgets and i am relatively new to raw code. I completed it and wanted to add a feature so the bot light flashed on and off and stopped if the battery was low but if the battery was ok it would just continue to run. The code that i have added for this feature is below.

// walkerForwardComplete.pde - Two servo walker. 
// Complete code with obstacle avoidance
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
// Updated by Joe Saavedra, 2010
#include <Servo.h> 
#define LED 13
#define batteryLow 500

int batteryVoltage;



if (batteryVoltage < batteryLow)// the if error
  {
    stopMoving();
    digitalWrite(LED, HIGH);
    delay(50);
    digitalWrite(LED, LOW);
    delay(50); 
  
  }
  else // else error
  {continue};

 
Servo frontServo;  
Servo rearServo;  
/* Servo motors - global variables */
int centerPos = 90; 
int frontRightUp = 72;
int frontLeftUp = 108;
int backRightForward = 75;
int backLeftForward = 105;
int walkSpeed = 150; // How long to wait between steps in milliseconds
int centerTurnPos = 81; 
int frontTurnRightUp = 63;
int frontTurnLeftUp = 117;
int backTurnRightForward = 66;
int backTurnLeftForward = 96;

/* Ping distance measurement - global variables */
int pingPin = 4;
long int duration, distanceInches;
long distanceFront=0; //cm
int startAvoidanceDistance=20; //cm

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

long distanceCm(){
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  distanceInches = microsecondsToInches(duration);
  return microsecondsToCentimeters(duration);
}

void center()
{
  frontServo.write(centerPos);
  rearServo.write(centerPos);  
}

void moveForward()
{
  frontServo.write(frontRightUp);
  rearServo.write(backLeftForward);
  delay(125);
  frontServo.write(centerPos);
  rearServo.write(centerPos);
  delay(65);
  frontServo.write(frontLeftUp);
  rearServo.write(backRightForward);
  delay(125);
  
  frontServo.write(centerPos);
  rearServo.write(centerPos);
  delay(65);
}

void moveBackRight()
{
  frontServo.write(frontRightUp);
  rearServo.write(backRightForward-6);
  delay(125);
  frontServo.write(centerPos);
  rearServo.write(centerPos-6);
  delay(65);
  frontServo.write(frontLeftUp+9);
  rearServo.write(backLeftForward-6);
  delay(125);
  
  frontServo.write(centerPos);
  rearServo.write(centerPos);
  delay(65);
}

void moveTurnLeft()
{
  frontServo.write(frontTurnRightUp);
  rearServo.write(backTurnLeftForward);
  delay(125);
  frontServo.write(centerTurnPos);
  rearServo.write(centerTurnPos);
  delay(65);
  frontServo.write(frontTurnLeftUp);
  rearServo.write(backTurnRightForward);
  delay(125);
  
  frontServo.write(centerTurnPos);
  rearServo.write(centerTurnPos);
  delay(65);
}

void setup()
{
  frontServo.attach(2);
  rearServo.attach(3);
  pinMode(pingPin, OUTPUT);
}  

void loop()
{
  distanceFront=distanceCm();
  if (distanceFront > 1){ // Filters out any stray 0.00 error readings
    if (distanceFront<startAvoidanceDistance) {
      for(int i=0; i<=8; i++) {
        moveBackRight();
        delay(walkSpeed);
      }
      for(int i=0; i<=10; i++) {
        moveTurnLeft();
        delay(walkSpeed);
      }
    } else {
      moveForward(); 
      delay(walkSpeed);
    }
  }
}

the debugger says
insect_robot:13: error: expected unqualified-id before ‘if’
insect_robot:22: error: expected unqualified-id before ‘else’
so how do i fix it

If you have any question feel free to ask.

Thanks for your help in advance
daniel

Please post all of your code. The culprit is probably several lines before that point.

Hi, could you post the complete sketch, but please read http://forum.arduino.cc/index.php/topic,148850.0.html before you do, it will show you how to post a sketch using #code tags.

The complete code will let us see how you have declared your variables and assigned them values.

Tom....... :)

The code you have posted is not inside a function. It won't compile unless it is inside a function.

So how do i put it in a function

The code of a function needs to be enclosed in braces. However, that will not solve your problem as the batteryVoltage variable is never updated in the code that you posted.

Have you posted your whole program ?

I copy and pated some of it . So do i need to define it first with a hash tag

You have a number of functions defined in your code already, such as moveBackRight. You need to put your battery code in a new function using the same syntax, perhaps called CheckBattery and call it from somewhere - loop most likely.

Then it will compile at least.

I think i have done what you have said. But it is coming up with
insect_robot:31: error: expected initializer before ‘if’
insect_robot:55: error: expected unqualified-id before ‘else’.
How do i fix it and what is an initialiser.
The adjusted code is below

// walkerForwardComplete.pde - Two servo walker. 
// Complete code with obstacle avoidance
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
// Updated by Joe Saavedra, 2010
#include <Servo.h> 
#define LED 13
#define batteryLow 500
#define batteryCHECK batteryVoltage < batteryLow

Servo frontServo;  
Servo rearServo;  
/* Servo motors - global variables */
int centerPos = 90; 
int frontRightUp = 72;
int frontLeftUp = 108;
int backRightForward = 75;
int backLeftForward = 105;
int walkSpeed = 150; // How long to wait between steps in milliseconds
int centerTurnPos = 81; 
int frontTurnRightUp = 63;
int frontTurnLeftUp = 117;
int backTurnRightForward = 66;
int backTurnLeftForward = 96;
int BatteryCHECK 

if (batteryVoltage < batteryLow)
  {
    stopMoving();
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); 
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(50); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); 
  }
  else 
  {continue};

Thanks
d

Something is missing before the if.

int BatteryCHECK change to int BatteryCHECK;

Here is one of your functions, defined properly:

void center()
{
  frontServo.write(centerPos);
  rearServo.write(centerPos);  
}

Here is your new battery code (I think):

int BatteryCHECK 

if (batteryVoltage < batteryLow)
  {
    stopMoving();
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); 
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(50); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); 
  }
  else 
  {continue};

Attention to detail is important when writing code. Compare the two - there are significant important differences at the top and bottom of those snippets.

Post the entire sketch(*) please, its hopeless trying to guess what's up from incomplete snippets.

You probably don't understand the continue statement. Suggest you don't attempt to use a statement before you understand its purpose.

(*) An entire sketch will have a setup() and loop() function at a very minimum.

The whole program is below

// walkerForwardComplete.pde - Two servo walker. 
// Complete code with obstacle avoidance
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
// Updated by Joe Saavedra, 2010
#include <Servo.h> 
#define LED 13
#define batteryLow 500
#define batteryCHECK batteryVoltage < batteryLow

Servo frontServo;  
Servo rearServo;  
/* Servo motors - global variables */
int centerPos = 90; 
int frontRightUp = 72;
int frontLeftUp = 108;
int backRightForward = 75;
int backLeftForward = 105;
int walkSpeed = 150; // How long to wait between steps in milliseconds
int centerTurnPos = 81; 
int frontTurnRightUp = 63;
int frontTurnLeftUp = 117;
int backTurnRightForward = 66;
int backTurnLeftForward = 96;
int BatteryCHECK 

if (batteryVoltage < batteryLow)
  {
    stopMoving();
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); 
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(50); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500); 
  }
  else 
  {continue};




/* Ping distance measurement - global variables */
int pingPin = 4;
long int duration, distanceInches;
long distanceFront=0; //cm
int startAvoidanceDistance=20; //cm

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

long distanceCm(){
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  distanceInches = microsecondsToInches(duration);
  return microsecondsToCentimeters(duration);
}

void center()
{
  frontServo.write(centerPos);
  rearServo.write(centerPos);  
}

void moveForward()
{
  frontServo.write(frontRightUp);
  rearServo.write(backLeftForward);
  delay(125);
  frontServo.write(centerPos);
  rearServo.write(centerPos);
  delay(65);
  frontServo.write(frontLeftUp);
  rearServo.write(backRightForward);
  delay(125);
  
  frontServo.write(centerPos);
  rearServo.write(centerPos);
  delay(65);
}

void moveBackRight()
{
  frontServo.write(frontRightUp);
  rearServo.write(backRightForward-6);
  delay(125);
  frontServo.write(centerPos);
  rearServo.write(centerPos-6);
  delay(65);
  frontServo.write(frontLeftUp+9);
  rearServo.write(backLeftForward-6);
  delay(125);
  
  frontServo.write(centerPos);
  rearServo.write(centerPos);
  delay(65);
}

void moveTurnLeft()
{
  frontServo.write(frontTurnRightUp);
  rearServo.write(backTurnLeftForward);
  delay(125);
  frontServo.write(centerTurnPos);
  rearServo.write(centerTurnPos);
  delay(65);
  frontServo.write(frontTurnLeftUp);
  rearServo.write(backTurnRightForward);
  delay(125);
  
  frontServo.write(centerTurnPos);
  rearServo.write(centerTurnPos);
  delay(65);
}

void setup()
{
  frontServo.attach(2);
  rearServo.attach(3);
  pinMode(pingPin, OUTPUT);
}  

void loop()
{
  distanceFront=distanceCm();
  if (distanceFront > 1){ // Filters out any stray 0.00 error readings
    if (distanceFront<startAvoidanceDistance) {
      for(int i=0; i<=8; i++) {
        moveBackRight();
        delay(walkSpeed);
      }
      for(int i=0; i<=10; i++) {
        moveTurnLeft();
        delay(walkSpeed);
      }
    } else {
      moveForward(); 
      delay(walkSpeed);
    }
  }
}

I foresee this getting very confusing between this and your other thread on functions.

To answer your question, this code block

if (batteryVoltage < batteryLow)
{
  stopMoving();
  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(500); 
  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(500); 
  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(50); 
  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(500); 
  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(500); 
  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(500); 
}
else 
{
  continue};

is not in a function. It does not have a name or a return type and the code is not enclosed in braces. Under what circumstances do you expect or want the code to run ?

Contrast it with functions in your program such as

void center()
{
  frontServo.write(centerPos);
  rearServo.write(centerPos);  
}

However, as I said previously, as you never update the batteryVoltage variable, even if this code were put in a function it would achieve nothing.

Do you want to continue this discussion in this thread or the other one about functions ?

You have a lot of code not in a function. This is simply never going to compile. Until you recognise and fix that, you’re wasting your time. Look at the example sketches that come with the Arduino IDE to see examples of functions. Here is one example. In this case the function is named setup, and the function body contains a single call to Serial.begin():

void setup()
{
    Serial.begin(9600);
}

If that doesn’t make it clear then Google C++ functions and make sure you understand what a function is before you go any further.

Once you understand that, I suggest you fix the code here:

#define batteryCHECK batteryVoltage < batteryLow

In my opinion this macro is considerably less clear than simply putting the corresponding condition directly in your code.

int BatteryCHECK 
if (batteryVoltage < batteryLow)

The variable definition needs to be terminated with a semicolon. Also, having a variable name and a macro name which only differ in capitalisation is not a good idea as it will be easy to muddle them up and any mistakes will not be obvious.

Hi, danield123, have you done any of the example sketches in the IDE.

As you have said, the sketch you are using is by another programmer, do you understand how it is constructed? It doesn't look as though you do, and need us to do it for you.

Can I suggest you start from scratch, and write sections of your own program a bit at a time. For example. Get the motors to run, then get them to turn, then reverse. Next the obstacle avoidance, get it working and signalling when it detects objects too close. Combining the two and debugging any problems.

This way you will now how the sketch works, learn how to program Arduino and have a project that will give you the satisfaction of saying I developed that, (with some forum help)

I have no problem with using other peoples code, but it won't necessarily teach you anything.

Hope to help mate, but it needs a fair bit of work from your end.

Tom...... :)