Wall sensing robot code help

I am very new to arduino and code.

I’m trying to help my students create a wall sensing robot. They have a two wheel robot chassis and an ultrasonic ping sensor. We have it all coded to have the robot move forward and have the ping sensor wired and running. My question is writing the if statement so when the sensor senses an obstacle it will stop the wheels, turn a specific amount, and move forward again. I want to learn how to code and not just copy and paste others although I don’t mind repurposing code, I just want to learn. Can someone help with step by step how to write the code to do the above?
thanks

My question is writing the if statement so when the sensor senses an obstacle it will stop the wheels, turn a specific amount, and move forward again.

This seems like such a simple thing. What have you tried? What were the results?

Using an if test:
if(dist < 10)
{
// do stuff here
}

Stop:
// depends on your motor shield,
// the connections and how many
// motors there are and whether
// braking is needed

Turn:
// depends on your motor shield,
// the connections

Go:
// depends on your motor shield,
// the connections

I really haven't tried yet. I didn't know how to start.

Have you got code that turns the motors on and off to move forwards, backwards, turn, stop?

Have you got code that obtains a distance reading from the ping sensor?

void pingsensor()
{
<snip> 
  delay(100); // Why?
}

gizahuna:
This is what I have for code so far:

Please enclose code in [ code ] [ / code ] tags when posting in the forum. Just click on the little # button when editing your post.

I’d prefer to see the motor pin numbers defined as constants rather than have the pin numbers scattered through the code.

I noticed that ‘forward’ sets the two motors to different speeds. Any reason for that?

Do all the motor control functions and ping sensor reading actually do what you intend? In other words, does the ping sensor return a credible distance result depending on what is in front of the sensor, and do the motors both go in the intended direction+speed for each type of movement?

PeterH:

gizahuna: This is what I have for code so far:

I noticed that 'forward' sets the two motors to different speeds. Any reason for that?

Do all the motor control functions and ping sensor reading actually do what you intend? In other words, does the ping sensor return a credible distance result depending on what is in front of the sensor, and do the motors both go in the intended direction+speed for each type of movement?

The motors don't seem to be the same strength at full power so the kids adjusted their speeds in order to get the bot to go straight. Everything seems to go fine and the sensor shows that it is sensing (they will run a basic program to test it tomorrow). I'm trying to stay a step ahead and and figure out the "if" statement to get it to turn when it comes near a wall.

Suggest you change pingsensor() to return the distance (in whatever units you prefer) rather than just print it.

In loop() when you call pingsensor(), assign the return value to a variable. Then use an IF statement to compare that value against a constant threshold to see whether it is short enough that you need to make a turn. If it is, you make a turn. If it isn't, you carry on.

PeterH: Suggest you change pingsensor() to return the distance (in whatever units you prefer) rather than just print it.

In loop() when you call pingsensor(), assign the return value to a variable. Then use an IF statement to compare that value against a constant threshold to see whether it is short enough that you need to make a turn. If it is, you make a turn. If it isn't, you carry on.

That is exactly what I want to do, but I don't know how to start it

Do you understand what it means for a function to return a value? You need to change pingsensor() so it returns a distance value.

Topics merged. Please use code tags when posting code.

Nary a Serial.print() in sight. How do you know what is happening?

Sorry…I’m catching on slowly. Where am I going wrong with the if statement.

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

// 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
int 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(){
  lightson();
  moveforward();
  pingsensor();
  //turn
  //blinkers();
}
  
 void lightson(){ 
  // the loop for the LED to run as a headlight
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(led2, HIGH); //TURN THE LED2 ON (HIGH IS THE VOLTAGE LEVEL)
 }

 void moveforward(){ 
  //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
 }
 
 void 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();
  
  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.
  // 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;
}

void turn (){
  if (pulseDuration < 10 & pulseDuration > 5)
{
  // stop motors
  //engages the brake for left motor
  digitalWrite(9, HIGH);
  //engages brake for right motorch
  digitalWrite (8, HIGH);
  
  delay (5000);
  
  //turns
   //full speed of motor is 255
  //digitalWrite(12, HIGH);  //Establishes REVERSE 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, LOW); //Establishes REVERSE motion of Channel B right motor
  //digitalWrite (8, LOW); //Disengages the brake for Channel B
  //analogWrite (11, 100);  //Spins the motor on Channel B SLOWLY
}
else
{
  work = work + 1;
}
}

I don't think I see anything wrong with it, but I can't see how pulseDuration has a value other than zero. (But that may just be the effects of too much egg nog)

AWOL: I don't think I see anything wrong with it, but I can't see how pulseDuration has a value other than zero. (But that may just be the effects of too much egg nog)

What I can't figure out is making the sensor sense the wall and just stop. I think once I get it to stop, I can figure out how to do the rest. Right now with the above code, it will just run straight into a wall.

In "pingsensor" you have a variable called "duration", but it is local, so is unavailable to the rest of the sketch. In fact, it seems that "pingsensor" serves no purpose other than reporting a range to the serial monitor.

AWOL: In "pingsensor" you have a variable called "duration", but it is local, so is unavailable to the rest of the sketch. In fact, it seems that "pingsensor" serves no purpose other than reporting a range to the serial monitor.

Isn't the duration the value the says how close the wall the robot is? I get a lot of the basic programming and get the sensor to an extent. I built the hot/cold sensor from Make Magazine, but I just can't figure out how to code this?

Isn't the duration the value the says how close the wall the robot is?

It represents the time the echo takes to reach the sensor, so yes, it may be proportional to the distance to a wall, but all you do with it is to convert it to centimetres and inches, and report the value to the serial monitor.

Based on that...what is the "if" in the if statement. I guess the variable, correct?

I don't understand the question. Your problem seems to be that you're expecting a variable to have a value between six and nine inclusive, but you never set the value of the variable, so it is always zero.