Pages: [1]   Go Down
Author Topic: programming of 4wd robot using ping sensor  (Read 911 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Where's your "setup"?
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post code.
All of it.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is the setup
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;
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See reply #4
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is the whole code
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();}  
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: