code question

I'm trying to write code for a robot to stop when it comes close to a wall. I've read and asked for a bunch of help and as a result simplified my code to the following. Now I get a compile issue I can't figure out. Any help would be great.

// names the LED and pin number
int led = 10;
int led2 = 6;


// this constant won't change.  It's the pin number for the sensor
// of the sensor's output:
const int pingPin = 7;

//sets the interger for the pulse sensor
long distance;
unsigned long pulseDuration=0;

void setup() {

 //Setup MOTOR RIGHT
 pinMode(12, OUTPUT); //Initiates Motor Channel A pin
 pinMode(9, OUTPUT); //Initiates Brake Channel A pin

 //Setup MOTOR LEFT
 pinMode(13, OUTPUT); //Initiates Motor Channel A pin
 pinMode(8, OUTPUT);  //Initiates Brake Channel A pin

 //SETUP LED HEADLIGHTS
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);
  pinMode (led2, OUTPUT);

  // initialize serial communication: SENSOR
 Serial.begin(9600);
}

 void loop (){
   distance = pingsensor();
   motion ();
 }

   
 long pingsensor(){
 // 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();

  return inches;
 delay(100);
}

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 motion (); 

// distance at which the robot will stop
 if (distance > 5)

 //Motor A forward @ full speed (left motor)
 //full speed of motor is 255
   digitalWrite(12, LOW);  //Establishes forward direction of Channel A  left motor
   digitalWrite(9, LOW);   //Disengage the Brake for Channel A
   analogWrite(3, 239);    //Spins the motor on Channel A

 //motor B forward @ full speed (right motor)
   digitalWrite (13, HIGH); //Establishes forward motion of Channel B right motor
   digitalWrite (8, LOW); //Disengages the brake for Channel B
   analogWrite (11, 254);  //Spins the motor on Channel B 
 }

 else {
 // stop motors
//engages the brake for left motor
   digitalWrite(9, HIGH);
   //engages brake for right motorch
   digitalWrite (8, HIGH);
}

So what is the compiler error message do you get?

That would helps...sorry.

it says the following:

Motion is not declared in this scope new_robot_sketch.ino: In function 'void loop()': new_robot_sketch:35: error: 'motion' was not declared in this scope new_robot_sketch.ino: At global scope: new_robot_sketch:93: error: expected unqualified-id before 'if' new_robot_sketch:98: error: expected constructor, destructor, or type conversion before '(' token new_robot_sketch:99: error: expected constructor, destructor, or type conversion before '(' token new_robot_sketch:102: error: expected constructor, destructor, or type conversion before '(' token new_robot_sketch:103: error: expected constructor, destructor, or type conversion before '(' token new_robot_sketch:104: error: expected constructor, destructor, or type conversion before '(' token new_robot_sketch:105: error: expected declaration before '}' token

void motion ();

That's probably causing a bunch of those last errors. The semi-colon ends a statement and it looks like you were declaring a function. You have no curly braces just a semi-colon. This incorrectly declared function is also the apparent cause of the first (not declared in this scope) error.

And when you've fixed that one, there's a curly brace missing after the if in the motion function.

Thanks..that fixed most of it. Now I have an error with the else statement

expected unqualified-id before 'else' new_robot_sketch:107: error: expected unqualified-id before 'else'

That worked! The robot moves and then stops before it hits the wall....thanks a lot

If I want the robot to backup (in a straight line) after it stops, where would the code go for that? Attached is the working code that stops the robot when it comes close to a wall.

// names the LED and pin number
int led = 10;
int led2 = 6;


// this constant won't change.  It's the pin number for the sensor
// of the sensor's output:
const int pingPin = 7;

//sets the interger for the pulse sensor
long distance;
unsigned long pulseDuration=0;

void setup() {

 //Setup MOTOR RIGHT
 pinMode(12, OUTPUT); //Initiates Motor Channel A pin
 pinMode(9, OUTPUT); //Initiates Brake Channel A pin

 //Setup MOTOR LEFT
 pinMode(13, OUTPUT); //Initiates Motor Channel A pin
 pinMode(8, OUTPUT);  //Initiates Brake Channel A pin

 //SETUP LED HEADLIGHTS
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);
  pinMode (led2, OUTPUT);

  // initialize serial communication: SENSOR
 Serial.begin(9600);
}

 void loop (){
   distance = pingsensor();
   motion ();
   turn ();
 }

   
 long pingsensor(){
 // 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();

  return inches;
 delay(100);
}

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 motion (){ 

// distance IN INCHES at which the robot will stop
 if (distance > 5){

 //Motor A forward @ full speed (left motor)
 //full speed of motor is 255
   digitalWrite(12, LOW);  //Establishes forward direction of Channel A  left motor
   digitalWrite(9, LOW);   //Disengage the Brake for Channel A
   analogWrite(3, 239);    //Spins the motor on Channel A

 //motor B forward @ full speed (right motor)
   digitalWrite (13, HIGH); //Establishes forward motion of Channel B right motor
   digitalWrite (8, LOW); //Disengages the brake for Channel B
   analogWrite (11, 254);  //Spins the motor on Channel B 
 }

 else {
 // stop motors
//engages the brake for left motor
   digitalWrite(9, HIGH);
   //engages brake for right motorch
   digitalWrite (8, HIGH);
 }
}

Your code would be far easier to read if you put each { on a new line and used Tools + Auto Format to fix the indenting.

If I want the robot to backup (in a straight line) after it stops, where would the code go for that?

Look at the code in motion(). Some of it makes the robot go forward. Figure out what that part is. and why it goes forward, instead of reverse. Then, move the forward stuff into a new function, forward(). Copy that function as backward() and change the stuff that makes it go forward to make it go backward, instead. (Hint: It has to do with what you are doing to pins 12 and 13).

I'm sorry, I don't think I made myself clear. I know how to get the wheels backwards, just set the pins to the opposite value. Ultimately what I want it do is after the robot stops, I want it to move backwards. I'm not sure where that code goes. Is it part of the "else" statement or is it an entirely different function.

gizahuna: Ultimately what I want it do is after the robot stops, I want it to move backwards. I'm not sure where that code goes. Is it part of the "else" statement or is it an entirely different function.

If you just want the robot to slam into reverse instead of stopping, then replace the 'stop' code with the 'reverse' code. Note that as soon as the robot moved far enough away from the wall, it would switch back into 'forwards' again so it would eventually just keep switching between forwards and reverse. Doesn't seem like a very useful thing to do. If that isn't the behaviour you want them you need to decide exactly what you do want to happen (Reverse forever? Reverse a set distance? A set time? Something else?) and then write the code to make that happen.

really what I want is for the robot to turn as it goes back. As I go through all this I'm trying understand the code as I go through it (walking before running, if you will). Here is my current code but I get the following error

new_robot_sketch.ino: In function 'void motion()': new_robot_sketch:116: error: expected `;' before 'digitalWrite'

// names the LED and pin number
int led = 10;
int led2 = 6;


// this constant won't change.  It's the pin number for the sensor
// of the sensor's output:
const int pingPin = 7;

//sets the interger for the pulse sensor
long distance;
unsigned long pulseDuration=0;

void setup() {

 //Setup MOTOR RIGHT
 pinMode(12, OUTPUT); //Initiates Motor Channel A pin
 pinMode(9, OUTPUT); //Initiates Brake Channel A pin

 //Setup MOTOR LEFT
 pinMode(13, OUTPUT); //Initiates Motor Channel A pin
 pinMode(8, OUTPUT);  //Initiates Brake Channel A pin

 //SETUP LED HEADLIGHTS
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);
  pinMode (led2, OUTPUT);

  // initialize serial communication: SENSOR
 Serial.begin(9600);
}

 void loop (){
   distance = pingsensor();
   motion ();
 }

   
 long pingsensor(){
 // 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();

  return inches;
 //delay(100);
}

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 motion (){ 

// distance IN INCHES at which the robot will stop
 if (distance > 5){

 //Motor A forward @ full speed (left motor)
 //full speed of motor is 255
   digitalWrite(12, LOW);  //Establishes forward direction of Channel A  left motor
   digitalWrite(9, LOW);   //Disengage the Brake for Channel A
   analogWrite(3, 239);    //Spins the motor on Channel A

 //motor B forward @ full speed (right motor)
   digitalWrite (13, HIGH); //Establishes forward motion of Channel B right motor
   digitalWrite (8, LOW); //Disengages the brake for Channel B
   analogWrite (11, 254);  //Spins the motor on Channel B 
 }

 else {
 // stop motors
//engages the brake for left motor
   digitalWrite(9, HIGH);
//engages brake for right motorch
   digitalWrite (8, HIGH);
   
   delay (5000)
   
   digitalWrite (12, HIGH);  //establishes a backwards motion
   digitalWrite (9, LOW);  //disengages brake for channel A
   analogWrite (3, 239);  //establishes the speed for channel A
   
   digitalWrite (13, LOW);  //establishes a backwards motion
   digitalWrite (8, LOW);  //disengages brake for channel B
   analogWrite (11, 100);  //establishes speed of channel B
   
 }
}
}
 delay (5000)

That line is missing the semi-colon.

You know that in the second line of the errors you posted that 116 is a line number. It's a good place to start looking. Usually the error is before the line where the compiler starts puking.

gizahuna: really what I want is for the robot to turn as it goes back.

Assuming you get the code to compile, it looks as if the 'bot will stop, wait five seconds and then start reversing and turning, but immediately resume checking the distance (so it would start forwards again as soon as it got five inches away from the wall).

A crude but simple way to arrange for the 'bot to reverse and turn through a specified angle before it resumes the normal forwards movement would be to put a delay after the code to initiate the reverse+turn.

Just don't tell PaulS I advised you to use delay().