The problem I am having right now is that when i run the code below, i will get one ping, the motors will start, and then everything blanks out. This problem is almost exactly what is described in this post.. http://forums.ladyada.net/viewtopic.php?t=6346. Although the poster said he found an answer by adding a release for both motors at the end of the loop().
I attempted to do the same... with less than desirable effects. First i couldn't decide where in my loop() to place the release. No where in my loop() did it seem to make a difference, had the same effects.
I eventually added the release statement to all of my movements.. and that fix just isn't going to work. While it did finally get the bot to move, pan and ping.. it would only roll forward for a second.. then there was a delay.. then it would move.. and a delay.. and move.. etc.. etc.. etc..
Bot's don't look cool when they roll, stop, roll, stop.. etc.
Background on my hardware..
Arduino diecimilla, two small dc motors, which have been used previously with no issues, on a boe bot conversion.
This is a parallax ping))) unit.. with standard futaba unmodified servo for pan.
Running two power supplies.. one for motors, one for arduino.
Have caps installed across my motors at the base.. so interference should not be an issue.
Here is the code that i am currently running:
- Code: Select all
#include <ServoTimer1.h>
#include <AFMotor.h>
/*
* My First Robot
*
* This robot drive around and avoids objects with the Ping sonar.
*
*/
AF_DCMotor motor(2, MOTOR12_8KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(4, MOTOR12_8KHZ); // create motor #2, 64KHz pwm
int pingPin = 14;
int servoPin = 9;
int servoRange[] = {550, 2450}; // The min/max pulse lengths to send to the servo
int servoRotateTime = 600; // How long it takes to turn the servo 180 degrees
int dangerDist = 100; // How far should something be before we react?
int servoTime = 700; // How long it takes to turn the servo
int turnTime = 250; // How long it takes to turn 90 degrees with the motors
ServoTimer1 servo1;
// This is called when the Arduino starts up
void setup(){
Serial.begin(9600);
printString("Start\n");
motor.setSpeed(230);
motor2.setSpeed(230);
// Servo
servo1.attach(servoPin);
servo1.setMinimumPulse(500);
servo1.setMaximumPulse(2500);
turn_servo(100);
}
// Arduino calls this infinite loop after setup()
void loop(){
int dist = read_ping(); // Read distance ahead
// There's nothing right in front of us
if(dist > dangerDist){
Serial.println("All Good\n");
drive_straight();
delay(50);
}
// Object ahead, find a better route
else{
Serial.println("Object Ahead\n");
change_direction();
delay(50);
}
}
// Something is in front of us, so find the best alternate route.
// Look left and right and decide which has more distance to drive.
void change_direction(){
motor_stop();
// Look left
turn_servo(180);
int left = read_ping_avg();
// Look right
turn_servo(0);
int right = read_ping_avg();
// Neither are good options, turn around
if(left < dangerDist && right < dangerDist){
turn_right();
delay(turnTime * 2);
drive_straight();
}
// Right is better
else if(left < right){
turn_right();
delay(turnTime);
drive_straight();
}
// Left is better
else {
turn_left();
delay(turnTime);
drive_straight();
}
}
// Read the distance from the Ping sensor
int read_ping(){
int timecount = 0;
/*
* Send trigger pulse to ping to tell it to start
*/
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(pingPin, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(pingPin, LOW); // Holdoff
/*
* Listen for echo pulses
*/
// Wait for input
int val = digitalRead(pingPin); // Append signal value to val
pinMode(pingPin, INPUT); // Switch signalpin to input
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(pingPin);
}
// Count pulses
while(val == HIGH) { // Loop until pin reads a low value
val = digitalRead(pingPin);
timecount = timecount +1;
}
delay(10);
return timecount;
}
// Get the average distance from 3 pings
int read_ping_avg(){
int total = 0;
total += read_ping();
total += read_ping();
total += read_ping();
int avg = total/3;
return avg;
}
// Turn the servo to a degree between 1 - 180.
void turn_servo(int degree){
int servoStopAt = 0;
// Set stop time
if(servoStopAt == 0){
servoStopAt = millis() + servoRotateTime;
}
servo1.write(degree);
// Refresh until the servo is done moving
// (using approximate timing set by servoRotateTime)
while(servoStopAt > millis()){
delay(20);
}
}
/*
* Motor movements
*/
void motor_stop(){
motor.run(RELEASE);
motor2.run(RELEASE);
}
void drive_straight(){
turn_servo(90); // Look straight ahead
motor.run(FORWARD);
motor2.run(FORWARD);
delay(100);
motor.run(RELEASE);
motor2.run(RELEASE);
}
void turn_left(){
motor.run(FORWARD);
motor2.run(BACKWARD);
delay(100);
motor.run(RELEASE);
motor2.run(RELEASE);
}
void turn_right(){
motor.run(BACKWARD);
motor2.run(FORWARD);
delay(100);
motor.run(RELEASE);
motor2.run(RELEASE);
}
Any help at all would be much appreciate.. if the original poster that had issues such as this would post his fix, that would be appreciated.. ladyada.. if you have ears on and are listening.. can you throw me a bone? Point a lost person in the right direction?
It seems to me that it's some kind of interrupt problem.. but what do i know..

