Go Down

Topic: programming of 4wd robot using ping sensor (Read 1 time) previous topic - next topic

victorfb

I am trying to make a 4wd robot using the ping sensor. I was able to get readings from the sensor and create functions for the movement of the robot, but when I try to combine both, the sensor stop the reading.
Code: [Select]

float ping(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  return distance
}
  void loop() {
  long duration, distance;
  distance = ping();
  duration = pulseIn (echoPin, HIGH);

  Serial.print(distance);
  Serial.print("cm, ");
  Serial.println();
 
  if (distance >= 20) {
    distance = ping();
    turnRight(); // function that turns the robot right
  }
 
   delay(500);
   }

if I upload this code the sensor returns the distance reading nicely. but when I put one more if there, like to move forward, the sensor does not return any reading. I also tried using 'while' but it gave me the same result

AWOL

What's the point of the pulseIn after the call to "ping"?

Where's your "setup"?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Code: [Select]
  if (distance >= 20) {
    distance = ping();
    turnRight(); // function that turns the robot right
  }

If distance is greater than or equal to 20, overwrite it, and then call turnRight(). Why overwrite it?

victorfb

I made the modifications and it works fine when I put just one condition, when I put a second condition the sensor stops reading.

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
I made the modifications

Oops, but I forget to post the new code.

Quote
it works fine when I put just one condition, when I put a second condition the sensor stops reading.

See hint above.

victorfb

this is the setup
Code: [Select]
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
#define trigPin 8
#define echoPin 9

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  motor1.setSpeed(200);         // Set the speed for the motors (255 is the maximum)     
  motor2.setSpeed(200);
  motor3.setSpeed(200);
  motor4.setSpeed(200);
}
void forward(){      // This function moves the wheels forward
    motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
 
}
void backward() {      // This function moves the wheels backward
    motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
void haltMotors()   // This function stops each motor (It is better to stop the motors before changing direction.)
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}
void turnRight(){    // This function turns the robot right.   
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(FORWARD);
}
void turnLeft(){
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}

float ping() {
  int duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  return distance;
}

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

I'm pretty sure that sketch needs a loop() function.

One or more calls to the ping() function probably need to be made.

victorfb

this is the whole code
Code: [Select]
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
#define trigPin 8
#define echoPin 9

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 motor1.setSpeed(200);         // Set the speed for the motors (255 is the maximum)      
 motor2.setSpeed(200);
 motor3.setSpeed(200);
 motor4.setSpeed(200);
}
void forward(){      // This function moves the wheels forward
   motor1.run(FORWARD);
 motor2.run(FORWARD);
 motor3.run(FORWARD);
 motor4.run(FORWARD);
 
}
void backward() {      // This function moves the wheels backward
   motor1.run(BACKWARD);
 motor2.run(BACKWARD);
 motor3.run(BACKWARD);
 motor4.run(BACKWARD);
}
void haltMotors()   // This function stops each motor (It is better to stop the motors before changing direction.)
{
 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
}
void turnRight(){    // This function turns the robot right.  
 motor1.run(FORWARD);
 motor2.run(BACKWARD);
 motor3.run(BACKWARD);
 motor4.run(FORWARD);
}
void turnLeft(){
 motor1.run(BACKWARD);
 motor2.run(FORWARD);
 motor3.run(FORWARD);
 motor4.run(BACKWARD);
}

float ping() {
 int duration, distance;
 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPin, HIGH);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 return distance;
}

void loop(){
 long time, distance;
 distance=ping();

 
 
 Serial.print(distance);
 Serial.print("cm");
 Serial.println();
 
if (distance >=10){
 turnRight();
 }
if (distance <10){
forward();}  
}

PaulS

Looking at the AFMotor source and header file, it appears that pins 3, 4, 5, 6, 7, 8, 11, and 12 are used by the shield.

It appears that you are trying to use pin 8 for your sensor, too.

victorfb

I already figure out, all I needed to do was to introduce a delay at the end of if. it is working fine now. Thank you

Go Up