Offline
Newbie
Karma: 1
Posts: 21
|
 |
« on: February 18, 2011, 11:26:27 pm » |
Hi, I am trying to get a robot to go forward until an object is detected within 5 inches. Then I want it to go backwards 10 inches from the object, then I want it to turn right. And finally, repeat the loop by going forward again and checking for an object within 5 inches. My robot just isn't moving in the correct manner. Is there an issue with my code? Here is my code: #include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ); AF_DCMotor motor2(2, MOTOR12_8KHZ); AF_DCMotor motor3(3, MOTOR12_1KHZ); AF_DCMotor motor4(4, MOTOR12_1KHZ); int ultraSoundSignal = 19; // assigning the PING sensor to Digitial Pin 19 unsigned long ultrasoundValue = 0; unsigned long echo = 0;
void setup() { Serial.begin(9600); // set up Serial library at 9600 bps pinMode(ultraSoundSignal,OUTPUT); motor1.setSpeed(200); motor2.setSpeed(200); motor3.setSpeed(200); motor4.setSpeed(200); }
unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output digitalWrite(ultraSoundSignal, LOW); // Send low pulse delayMicroseconds(2); // Wait for 2 microseconds digitalWrite(ultraSoundSignal, HIGH); // Send high pulse delayMicroseconds(5); // Wait for 5 microseconds digitalWrite(ultraSoundSignal, LOW); // Holdoff pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches return ultrasoundValue; }
void forward() // the forward function { motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(FORWARD); motor4.run(FORWARD); }
void backward() // the backward function { motor1.run(BACKWARD); motor2.run(BACKWARD); motor3.run(BACKWARD); motor4.run(BACKWARD); }
void haltMotors() //the functon to stop the motors before reversing direction { motor1.run(RELEASE); motor2.run(RELEASE); motor3.run(RELEASE); motor4.run(RELEASE); }
void turnRight() // the function to turn right { motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(BACKWARD); motor4.run(BACKWARD); }
void loop() { ping(); while (ultrasoundValue > 5) //while robot is 5 inches away from object { forward(); //go forward ping(); // update position } haltMotors(); //stop the motors before reversing direction delay(3000); while (ultrasoundValue < 10) //move robot back 10 inches before turning { backward(); ping(); //update position } turnRight(); delay(5000); }
|
|
|
|
« Last Edit: February 18, 2011, 11:28:41 pm by Brennn10 »
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #1 on: February 19, 2011, 06:17:19 am » |
My robot just isn't moving in the correct manner It's dancing the cha-cha? Why don't you tell us what it is doing, instead of what it is not doing? Some debug prints might point you in the right direction (no pun intended) Your ping function returns a value - you may want to check its value, and use it rather than relying on a single global.
|
|
|
|
« Last Edit: February 19, 2011, 06:41:50 am by Groove »
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35590
Seattle, WA USA
|
 |
« Reply #2 on: February 19, 2011, 07:14:39 am » |
While you are telling us what the robot actually does versus what you want it to do, please explain why echo, ultrasoundValue, and ultraSoundSignal are global variables. If ping() returns a value, the value that it returns should be used, not ignored in favor of a global variable.
Consistency in variable names is a good thing. ultrasoundValue and ultraSoundSignal are not consistent. One has sound with a lower case s, the other has sound with an upper case s. In addition, the ultraSoundSignal contains a pin number, and has nothing to do with any signal.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #3 on: February 19, 2011, 08:58:06 am » |
void loop() { forward (); while (ping () > 5); haltMotors(); //stop the motors before reversing direction delay(3000); backward (); while (ping () < 10) ; //move robot back 10 inches before turning turnRight(); }
|
|
|
|
« Last Edit: February 19, 2011, 08:59:56 am by Groove »
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 21
|
 |
« Reply #4 on: February 19, 2011, 11:02:01 am » |
I found the code for this sensor on this Arduino site: http://www.arduino.cc/playground/Main/UltrasonicSensorHow does Groove's response look? I'm away from the robot right now, but I'll try it out when I get back.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #5 on: February 19, 2011, 11:09:16 am » |
How does Groove's response look It looks the same as yours, but with superfluous stuff removed. I found the code for this sensor on this Arduino site It's pretty horrible (I would have parameterised the output pin number), and it's never really a good idea to return a global value, things could get confusing.
|
|
|
|
« Last Edit: February 19, 2011, 11:13:00 am by Groove »
|
Logged
|
Per Arduino ad Astra
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #6 on: February 19, 2011, 11:31:04 am » |
My take: (caveat: I don't have this sensor, so the code is untested) const int myPingPin = 9;
void setup() { Serial.begin(9600); }
unsigned long ping (const int pingPin) { pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); digitalWrite(pingPin, HIGH); // I don't think I've seen other Ping code do this - is it necessary? unsigned long echo = pulseIn(pingPin, HIGH); return (unsigned long) (echo / 58.138) * .39; }
void loop() { unsigned long x = ping(myPingPin); Serial.println(x); delay(250); }
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 21
|
 |
« Reply #7 on: February 19, 2011, 11:55:47 am » |
Ok Goove, thanks for your edit. Is my loop still correct? I'm not sure if I am updating the position in the correct spots. void loop(){ while (ultrasoundValue < 10) //move robot back 10 inches before turning { backward(); void loop() { while (ping() > 5) //while robot is 5 inches away from object { forward(); //go forward ping(); // update position } haltMotors(); //stop the motors before ping(); //update position } turnRight(); delay(5000); }
|
|
|
|
« Last Edit: February 19, 2011, 11:57:24 am by Brennn10 »
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #8 on: February 19, 2011, 12:01:26 pm » |
while (ultrasoundValue < 10) //move robot The first time through loop, you haven't called ping, so ultrasoundValue is zero, so it is < 10. Why don't you try the version I wrote for you? backward(); void loop()
Have you actually compiled this? What happened?
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 21
|
 |
« Reply #9 on: February 19, 2011, 12:12:15 pm » |
I'll be working on it this afternoon, so I'll let you know how it goes.
|
|
|
|
|
Logged
|
|
|
|
|
|