Go Down

Topic: Error: expected unqualified-id before 'if' (Read 2489 times) previous topic - next topic

Dustin Romero

Jan 25, 2010, 12:50 am Last Edit: Jan 25, 2010, 01:14 am by jenius8477 Reason: 1
Well i am new to programming in C. But I am trying to learn as much as i can. What i am trying to do is run a ping distance sensor to judge distance to nearest object and if the distance is < 40 cm, output to servos. the actual servo values are bogus and just used in a testing rig to watch for movement. here's the code


Code: [Select]

/* Ping))) Sensor
 
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
     
   The circuit:
      * +V connection of the PING))) attached to +5V
      * GND connection of the PING))) attached to ground
      * SIG connection of the PING))) attached to digital pin 7

 
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Jun 2009
   by Tom Igoe

 */

// this constant won't change.  It's the pin number
// of the sensor's output:

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
Servo myservo3;  // create servo object to control a servo
const int pingPin = 53;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
   
{myservo1.attach(31);
myservo2.attach(32);
myservo3.attach(33);
}
}
void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(1000);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
 
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}





void loop()
{
if (cm > 15 && cm < 40 )

{                                  
  myservo3.write(90);
  myservo2.write(90);                                  
  myservo1.write(90);              
  delay(1000);                      

}  
{
  myservo3.write(190);
  myservo2.write(175);              
  myservo1.write(175);            
  delay(1000);                      

}
 {
  myservo3.write(10);
  myservo2.write(5);              
  myservo1.write(5);            
  delay(1000);                      

}
else
 myservo3.write(90);
  myservo2.write(90);                                  
  myservo1.write(90);              
  delay(1000);
}
}







Any suggestions would be greatly appriciated :-?

AlphaBeta

Hello!

Try using thw
  • button when posting code.


    It looks like you're trying to have two loop()s, that won't work.

    Please edit your post, highlight all code and press
  • . :)

Dustin Romero

thanks for that catch, any suggestion on what to do?

AlphaBeta

Code: [Select]
/* Ping))) Sensor
 
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.

   The circuit:
      * +V connection of the PING))) attached to +5V
      * GND connection of the PING))) attached to ground
      * SIG connection of the PING))) attached to digital pin 7

 
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Jun 2009
   by Tom Igoe

 */

// this constant won't change.  It's the pin number
// of the sensor's output:

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
Servo myservo3;  // create servo object to control a servo
const int pingPin = 53;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
   
{myservo1.attach(31);
myservo2.attach(32);
myservo3.attach(33);
}
}
void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(1000);if (cm > 15 && cm < 40 )

{                                  
  myservo3.write(90);
  myservo2.write(90);                                  
  myservo1.write(90);              
  delay(1000);                      

}  
{
  myservo3.write(190);
  myservo2.write(175);              
  myservo1.write(175);
  delay(1000);                      

}
 {
  myservo3.write(10);
  myservo2.write(5);              
  myservo1.write(5);
  delay(1000);                      

}
else
 myservo3.write(90);
  myservo2.write(90);                                  
  myservo1.write(90);              
  delay(1000);
}
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
 
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


You can try this.  :)

What I did:
Copy all code from the below loop, paste it in at the bottom of the above loop.

Dustin Romero

well i tried the code you posted and it gave the error that there was no if before else. after removing the else statement. code compiled. but i would like to use the else statement later on. any help?

AlphaBeta

you need an explicit if() before all your {} for it to work like you expext

it is no compile error to omit them (if you have no else) but since you had an else you need something like:
Code: [Select]

if(){
} else if(){
} else {
}

not
Code: [Select]
if(){
}{
}{
}else{
}

:)

Dustin Romero

you are a lifesaver.........pineapple flavor haha. code is working fine, and i have a better understanding of how the code works. thank you . any other suggestions you may have. lay them on

Dustin Romero

how can i impliment a counter so that the sensor will scan say, 250 times in between the servo movement. the code has developed a little bit so here it is

Code: [Select]
/* Ping))) Sensor
 
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
     
   The circuit:
      * +V connection of the PING))) attached to +5V
      * GND connection of the PING))) attached to ground
      * SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Jun 2009
   by Tom Igoe

 */

// this constant won't change.  It's the pin number
// of the sensor's output:

 #include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
Servo myservo3;
const int pingPin = 53;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
   myservo1.attach(31);
myservo2.attach(32);
myservo3.attach(33);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   myservo3.write(10);
  pinMode(pingPin, OUTPUT);
 
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(10);
  myservo3.write(190);
     pinMode(pingPin, OUTPUT);
 
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(10);
    myservo3.write(90);
   
          pinMode(pingPin, OUTPUT);
 
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(10);
 
 if (cm < 40){
   
  myservo2.write(90);                                  
  myservo1.write(90);              
  delay(1000);                      
 
  myservo2.write(175);              
  myservo1.write(175);            
  delay(1000);                      
 
  myservo2.write(50);              
  myservo1.write(50);            
  delay(1000);                      

}


 
 
 
 
 
 
 
 
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}




here it is

Dustin Romero


digimike

yes.

you can do a for() inside a for() or if(). You can use any number of control structure commands inside of each other as you need. Just be sure to have the syntax correct. Remember you need the colosing curly bracket for each one you start.

Dustin Romero

Thank you very much, im pluging along and now i have a working code that needs a few tweaks. here is the code i am working on right now

Code: [Select]
 #include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
Servo myservo3;
const int pingPin = 53;

void setup() {
  Serial.begin(9600);
   myservo1.attach(31);
myservo2.attach(32);
myservo3.attach(33);
}
void loop()
{
  long duration, inches, cm, inches1, cm1, inches2, cm2, inches3, cm3;
  myservo3.write(90);
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(10);
 
 if (cm < 40){
  myservo3.write(10);
  delay(500);
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches1 = microsecondsToInches(duration);
  cm1 = microsecondsToCentimeters(duration);
  Serial.print(inches1);
  Serial.print("in, ");
  Serial.print(cm1);
  Serial.print("cm");
  Serial.println();
  delay(10);
  myservo3.write(180);
  delay(500);
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches2 = microsecondsToInches(duration);
  cm2 = microsecondsToCentimeters(duration);
  Serial.print(inches2);
  Serial.print("in, ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
  delay(10);
  myservo.write(90);
  delay(500)
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches3 = microsecondsToInches(duration);
  cm3 = microsecondsToCentimeters(duration);
  Serial.print(inches3);
  Serial.print("in, ");
  Serial.print(cm3);
  Serial.print("cm");
  Serial.println();
  delay(10);
 
  if (cm1<cm2) {                    
  myservo2.write(10);              
  myservo1.write(175);    
  delay(500);
  }
  if (cm1>cm2){  
  myservo2.write(10);              
  myservo1.write(10);            
  delay(1000);  
  }

}
}

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

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

Go Up