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.
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
system
September 28, 2012, 10:20pm
2
What's the point of the pulseIn after the call to "ping"?
Where's your "setup"?
system
September 29, 2012, 12:11pm
3
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?
I made the modifications and it works fine when I put just one condition, when I put a second condition the sensor stops reading.
system
October 2, 2012, 5:13pm
6
I made the modifications
Oops, but I forget to post the new code.
it works fine when I put just one condition, when I put a second condition the sensor stops reading.
See hint above.
this is the setup
#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;
}
system
October 2, 2012, 5:24pm
9
I'm pretty sure that sketch needs a loop() function.
One or more calls to the ping() function probably need to be made.
this is the whole code
#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();}
}
system
October 2, 2012, 5:56pm
11
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.
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