Go Down

Topic: (HELP) error: expected unqualified-id (Read 914 times) previous topic - next topic

tlavite

When running the code below i keep getting this error


" error: expected unqualified-id before '{' token arduino "



/*

OBJECT AVOIDING ROBOT CODE FOR ARDUINO - ADAFRUIT MOTORSHIELD - PARALLAX PING )) - TAMIYA DUAL MOTOR GEARBOX

CODE ADAPTED, COPIED, MODIFIED, AND STOLEN IN GENERAL FROM VARIOUS PLACES AROUND THE INTERNET BY GRINAN BARRETT JULY 4TH/5TH 2008

THIS CODE IS FREE TO ANYONE THAT WISHES TO USE IT, MODIFY IT, OR STEAL IT IN GENERAL.. HAVE FUN WITH IT!

This code will let you program a bot that will roam around looking for things to avoid running into.  The code is pretty simple, but gave me major fits figuring out.  Espeically the interaction with the
the parallax ping )) ultrasonic range finder.  The main thing to remember is the intialization of the ping unit.. it has to be exact.. once i saw i was running to tight a loop on it.. things got much easier.. ( thanks collin)
if nothing else, this code will let you have the basics of a roving bot with ping capability, it's up to you to do cool stuff with it.

*/

//    GETTING EVERYTHING SETUP AND READY
#include <AFMotor.h>
AF_DCMotor motor(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
unsigned long echo = 0;  
unsigned long ultrasoundValue = 0;
int ultraSoundSignal = 14;    // sets enable to pin 14
float dist = 0;      // variable to store the converted distance in inches

// ***************************   MAIN STRAIGHT ROUTINE  *********************************** \\
void straight()
{
   motor.run(FORWARD);
   motor2.run(FORWARD);
   Serial.print("Forward Hoooo!");  // ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
}
// ***************************   MAIN TURNING ROUTINE  *********************************** \\
void turnRight()
{
    motor.run(BACKWARD);
    motor2.run(FORWARD);
    Serial.print("Turning Right!!");// ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
  }

// ***************************   PING FUNCTIONS TO GET DISTANCES  *********************************** \\
float distCalc() // distance calculating function converts analog input to inches
{
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
 
 echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
 dist = (echo / 58.138) * .39; //convert to CM then to inches
 
return dist;

}

// ***************************   THE AVOIDING OBJECTS ROUTINE  *********************************** \\
void AvoidObjects()
{
   if(dist < 8)                            // if the distance is less than 8 inches
 {
   Serial.print("object detected closer than allowed!!!");   // ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
   turnRight();                            // turns right using turnRight function
   delay(100);
 }

  else                                      // otherwise
 {
    straight();                            // go straight using the straight function
    Serial.print(" continuing forward ");  //  ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
 }
}

// ***************************   SETTING UP THE PROGRAM  *********************************** \\
void setup()
{
 
 motor.setSpeed(200);          // sets motor 1 speed to 200
 motor2.setSpeed(200);          // sets motor 2 speed to 200
 
 pinMode(ultraSoundSignal, OUTPUT);  // sets enable as output

 straight();                    // initializes go straight
}

// ***************************   MAIN PROGRAM LOOPS  *********************************** \\
void loop() {
 Serial.begin(9600);
 distCalc();                                
 AvoidObjects();                            
 delay(20);                                
 int x = 0;
 x = dist;
 Serial.println(x);  //     ADDED TO APPEND DISTANCE TO SERIAL MONITOR SO I COULD SEE PROBLEMS..
 delay(250);
}

Tyler LaVite
tlavite@gmail.com

AlphaBeta

After some time with banging my head against the closest wall, I found that the error was with the \\ in the comments. I do not know why, but without them it seems to work.
I've done some other minor changes as well;
Quote


/*
OBJECT AVOIDING ROBOT CODE FOR ARDUINO - ADAFRUIT MOTORSHIELD - PARALLAX PING )) - TAMIYA DUAL MOTOR GEARBOX

CODE ADAPTED, COPIED, MODIFIED, AND STOLEN IN GENERAL FROM VARIOUS PLACES AROUND THE INTERNET BY GRINAN BARRETT JULY 4TH/5TH 2008

THIS CODE IS FREE TO ANYONE THAT WISHES TO USE IT, MODIFY IT, OR STEAL IT IN GENERAL.. HAVE FUN WITH IT!

This code will let you program a bot that will roam around looking for things to avoid running into.  The code is pretty simple, but gave me major fits figuring out.  Espeically the interaction with the
the parallax ping )) ultrasonic range finder.  The main thing to remember is the intialization of the ping unit.. it has to be exact.. once i saw i was running to tight a loop on it.. things got much easier.. ( thanks collin)
if nothing else, this code will let you have the basics of a roving bot with ping capability, it's up to you to do cool stuff with it.

*/

byte ultraSoundSignal = 14;
unsigned long dist = 0;

// ***************************   SETTING UP THE PROGRAM  ***********************************
void setup()
{
 Serial.begin(9600);
 //motor.setSpeed(200);          // sets motor 1 speed to 200
 //motor2.setSpeed(200);          // sets motor 2 speed to 200

 pinMode(ultraSoundSignal, OUTPUT);  // sets enable as output

 straight();                    // initializes go straight
}

// ***************************   MAIN PROGRAM LOOPS  ***********************************
void loop() {
distCalc();                                
avoidObjects();                            
delay(20);    
Serial.println(dist);  //     ADDED TO APPEND DISTANCE TO SERIAL MONITOR SO I COULD SEE PROBLEMS..
delay(250);
}
// ***************************   MAIN STRAIGHT ROUTINE  ***********************************
void straight(){
  //motor.run(FORWARD);
  //motor2.run(FORWARD);
  Serial.print("Forward Hoooo!");  // ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
}
// ***************************   MAIN TURNING ROUTINE  ***********************************
void turnRight()
{
   //motor.run(BACKWARD);
   //motor2.run(FORWARD);
   Serial.print("Turning Right!!");// ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
}

// ***************************   PING FUNCTIONS TO GET DISTANCES  ***********************************
float distCalc() // distance calculating function converts analog input to inches
{
 pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
 digitalWrite(ultraSoundSignal, LOW); // Send low pulse
 delayMicroseconds(2); // Wait for 2 microseconds
 digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
 delayMicroseconds(5); // Wait for 5 microseconds
 digitalWrite(ultraSoundSignal, LOW); // Holdoff
 pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
 digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor

 unsigned long echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
 dist = (echo / 58.138) * 0.39; //convert to CM then to inches

 return dist;
}

// ***************************   THE AVOIDING OBJECTS ROUTINE  ***********************************
void avoidObjects()
{
 if(dist < 8)                            // if the distance is less than 8 inches
 {
   Serial.print("object detected closer than allowed!!!");   // ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
   turnRight();                            // turns right using turnRight function
   delay(100);
 }
 else                                      // otherwise
 {
   straight();                            // go straight using the straight function
   Serial.print(" continuing forward ");  //  ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
 }
}

tlavite

Thank you i compiles... now to change the motors to 3 and 4 instead of 1 and 2 i would just change it to 3 and 4 right?
Tyler LaVite
tlavite@gmail.com

tlavite

the code is running my motors but is jerking with the ping sensor. everything is running on its own power supply. i am going to switch the ping to pin 14 as the code says.
Tyler LaVite
tlavite@gmail.com

tlavite

I put the Ping sensor on pin 13 the code works it avoids objects but it just jerks terribly as it moves. could that have something to do with the power? the motor shield has a separate power supply but i noticed when the Arduino is on the motor shield's LED is dimly lighted as if its also receiving some but not quite all power from the Arduino's PS
Tyler LaVite
tlavite@gmail.com

Go Up