This is the current code that I have to make my robot turn right when the PING sensor detects an object within 5 inches. How do you think this code works? Here is some pseudo-code: Go Forward until an object is detected within 5 inches Turn Right if an object is detected Move forward until an object is detected within 5 inches
Does my code do this well? Could it be refined?
#include <AFMotor.h>
// create motor #2, 64KHz pwm
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
const int pingPin = 19;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
motor1.setSpeed(200); // set the speed to 200/255
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
}
void loop() {
long duration, inches;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondstoInches(duration);
if (inches > 5)
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
else
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
}
long microsecondstoInches (long microseconds)
{
return microseconds / 74 / 2;
}
A quick view at the code shows no real problems, think it should work. But (there is allways a but) it can be improved in several ways. That was what AWOL meant by his remark about the comments. In the end the code is what counts, not the comments ...
I've tinkered with your code to show you some ideas, keep loop() as simple as possible by adding functions, and merging other ones. Changed the distance to float to get a bit more precission. Also added some questions to the code... (did not try to compile it BTW)
Hopes this helps ..
Rob
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ); // Position of this wheel front left ??
AF_DCMotor motor2(2, MOTOR12_8KHZ); // Why
AF_DCMotor motor3(3, MOTOR12_1KHZ); // WHY 1Khz?
AF_DCMotor motor4(4, MOTOR12_1KHZ);
void setup()
{
Serial.begin(9600); // is it used?
setSpeed(200);
}
void loop()
{
float inches = getDistance();
if (inches > 5.0)
{
goForward();
}
else
{
goRight();
}
// emergency break :)
if (inches < 1.0) setSpeed(0);
}
/////////////////////////////////////
void setSpeed(uint8_t speed)
{
motor1.setSpeed(speed); // set the speed to 200/255
motor2.setSpeed(speed);
motor3.setSpeed(speed);
motor4.setSpeed(speed);
}
void forward()
{
setSpeed(200);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void turnRight()
{
setSpeed(100); // turning slow is safer :)
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
float getDistance()
{
const int pingPin = 19; // only relevant here
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
return pulseIn(pingPin, HIGH) / 148.0;
}
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ); // Position of this wheel front left ??
AF_DCMotor motor2(2, MOTOR12_8KHZ); // Why
AF_DCMotor motor3(3, MOTOR12_1KHZ); // WHY 1Khz?
AF_DCMotor motor4(4, MOTOR12_1KHZ);
The reason why he's got motors 3&4 declared as 1KHz is because according to the Adafruit website, motors 1 and 2 can be set to 1,2,8, or 64KHz, while motors 3 and 4 can only be set at 1KHz. I never could get the speed definitions to work, so I just left them out (for some reason it just wouldn't compile). Much less of a headache that way.
Thanks for your help, guys. How should I make sure that the robot turns right at 90 degrees? Is it possible to set a time for how long the robot turns? Say I wanted to make it go backward a few inches before turning right at a 90 degrees angle. What would be the best way to accomplish this?
What do you mean by using the rangefinder? Do I utilize values from the sensor to make it turn right 90 degrees? Also, how do I make it go backward for a certain amount of time (or distance). Right now, once it detects an object, it moves backward until it is out of range, and then moves forward, and then moves backward again. So it is basically just lurching back and forth. I want it to move backwards about 6 inches before it starts to turn right again.
Also, how do I make it go backward for a certain amount of time (or distance
Distance is easy - use the rangefinder.
Time is easy - use the built-in timers.
Turns can use the timers, but the actual degree of turn can usually only be found by experiment.
Measuring an angle with the rangefinder isn't feasible, but I don't think that is what AWOL meant.
To be able to turn exactly 90 degrees you must have referencepoints, four. Two points define your "old direction" and two for the current direction. This is can be quite complex to build up these references and keep them up to date. Fortunately mother nature provided us with an free reference framework called the magnetic field. You could use a compass module e.g. - http://www.adafruit.com/index.php?main_page=product_info&cPath=35&products_id=244 - to continuously monitor your direction. When you rotate you just rotate until you have reached direction +90.
I tried uploading the motor code to my arduino but i keep getting this error message:
sketch_jan21b:4: error: 'AF_DCMotor' does not name a type
sketch_jan21b:5: error: 'AF_DCMotor' does not name a type
sketch_jan21b:6: error: 'AF_DCMotor' does not name a type
sketch_jan21b:7: error: 'AF_DCMotor' does not name a type
sketch_jan21b.ino: In function 'void setup()':
sketch_jan21b:12: error: 'motor1' was not declared in this scope
sketch_jan21b:13: error: 'motor2' was not declared in this scope
sketch_jan21b:14: error: 'motor3' was not declared in this scope
sketch_jan21b:15: error: 'motor4' was not declared in this scope
sketch_jan21b.ino: In function 'void loop()':
sketch_jan21b:35: error: 'motor1' was not declared in this scope
sketch_jan21b:35: error: 'FORWARD' was not declared in this scope
sketch_jan21b:36: error: 'motor2' was not declared in this scope
sketch_jan21b:37: error: 'motor3' was not declared in this scope
sketch_jan21b:38: error: 'motor4' was not declared in this scope
sketch_jan21b:42: error: 'motor1' was not declared in this scope
sketch_jan21b:42: error: 'FORWARD' was not declared in this scope
sketch_jan21b:43: error: 'motor2' was not declared in this scope
sketch_jan21b:44: error: 'motor3' was not declared in this scope
sketch_jan21b:44: error: 'BACKWARD' was not declared in this scope
sketch_jan21b:45: error: 'motor4' was not declared in this scope
sketch_jan21b.ino: In function 'long int microsecondstoInches(long int)':
sketch_jan21b:49: error: expected `}' at end of input