Ping and motors

Hello all.

I am using the makey code (will post below) and am having some issues. Specs:

Arduino duemilanove

Parallax ping sensor

9g mini servo

random rc tank base

homemade l293 H bridge

4.5v to the bridge

 

Now here's my problem. The bot starts up and appears to begin roaming around avoiding obstacles, but then suddenly gets stuck in this little loop where he backs up, goes forward at a slight angle, then backs up again. This makes him do a little star looking pattern indefinitely. I noticed that as soon as he starts going, the light on my ping sensor no longer lights up. My servo doesnt work at all, of course because there is no code in there to account for that.

I am assuming that it has to be some sort of wiring issue, because i have a switch in line from my battery to my h bridge. when i shut the power off there, the sensor works normall (can pass hands in front of it and blinking light shuts off, etc.)

Any idea of what im missing?

 

 

/*

 * 06_Object_Avoidance

 * 

 * Makey robot roams around, while avoiding objects

 * sensed with the Ping ultrasonic rangefinder.

 *

 * Makey Robot, MAKE Magazine, Volume 19, p. 77

 * Created: June 2009 Kris Magri

 * Modified:

 * 

 */

 

  #define pingPin       9      // Ping ultrasonic sensor on pin D9.

  #define leftDir1      11      // Left motor direction 1, to AIn1.

  #define leftDir2      10     // Left motor direction 2, to AIn2.

  #define rightDir1     6      // Right motor direction 1, to BIn1.

  #define rightDir2     5      // Right motor direction 2, to BIn2.

 

  #define BOUNDARY     20      // (cm) Avoid objects closer than 20cm.

  #define INTERVAL     25      // (ms) Interval between distance readings.

 

// setup

// Set motor pins as OUTPUTS, initialize Serial

 

void setup()                                                 

{

  pinMode(leftDir1, OUTPUT);        // Set motor direction pins as outputs.

  pinMode(leftDir2, OUTPUT);      

  pinMode(rightDir1, OUTPUT);      

  pinMode(rightDir2, OUTPUT); 

  Serial.begin(9600);

}

 

 

// Main program

// Roam around while avoiding objects.

// 

// Set motors to move forward,

// Take distance readings over and over, 

// as long as no objects are too close (determined by BOUNDARY).

// If object is too close, avoid it -- back up and turn.

// Repeat.

 

void loop()                     

{

  long distance;                    // Distance reading from rangefinder.

 

  forward();                        // Robot moves forward continuously.

  do 

  {

    distance = readDistance();      // Take a distance reading.

    Serial.println(distance);       // Print it out.             

    delay(INTERVAL);                // Delay between readings.

  }

  while(distance >= BOUNDARY);      // Loop while no objects close-by.

 

  // Robot has sensed a nearby object and exited the while loop.

  // Take evasive action to avoid object.          

  backward();                       // Move backward 500ms.

  delay(500);               

  rightTurn(300);                   // Turn right 300ms.

 

 } // end Main program

 

 

// forward

//

// Move robot forward by setting both wheels forward.

// Will persist until something else changes the

// motors' directions.

 

void forward()

{

  digitalWrite(leftDir1, LOW);      // Left motor forward.             

  digitalWrite(leftDir2, HIGH);

  digitalWrite(rightDir1, LOW);     // Right motor forward.

  digitalWrite(rightDir2, HIGH);

}  

 

 

// backward

//

// Move robot backward by setting both wheels backward.

// Will persist until something else changes the

// motors' directions.

 

void backward()

{

  digitalWrite(leftDir1, HIGH);     // Left motor backward.

  digitalWrite(leftDir2, LOW);

  digitalWrite(rightDir1, HIGH);    // Right motor backward.

  digitalWrite(rightDir2, LOW);

}

 

 

// rightTurn

//

// Turn robot to right by moving wheels in opposite directions.

// Amount of turning is determined by duration argument (ms).

 

void rightTurn(int duration)

{

  digitalWrite(leftDir1, HIGH);     // Left motor backward.

  digitalWrite(leftDir2, LOW);

  digitalWrite(rightDir1, LOW);     // Right motor forward.

  digitalWrite(rightDir2, HIGH);

  delay(duration);                  // Turning time (ms).

}

 

 

// readDistance

// Take a distance reading from Ping ultrasonic rangefinder.

// from http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

 

long readDistance()

{

  long duration, inches, cm;

 

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.

  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.

  pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW);

  delayMicroseconds(2);

  digitalWrite(pingPin, HIGH);

  delayMicroseconds(5);

  digitalWrite(pingPin, LOW);

 

  // The same pin is used to read the signal from the Ping: a HIGH

  // pulse whose duration is the time (in microseconds) from the sending

  // of the ping to the reception of its echo off an object.

  pinMode(pingPin, INPUT);

  duration = pulseIn(pingPin, HIGH);

 

  // Convert the time into a distance.

  cm = microsecondsToCentimeters(duration);

  return(cm);

}

 

long microsecondsToCentimeters(long microseconds)

{

  // The speed of sound is 340 m/s or 29 microseconds per centimeter.

  // The ping travels out and back, so to find the distance of the

  // object we take half of the distance traveled.

  return microseconds / 29 / 2;

}

This is a power issue

If it works at all, then does weird stuff, it is probably a power issue --what are you running this with? What kind of battery?

Code

Nice with comments in your code.

9v for the arduino4 - AA’s

9v for the arduino

4 - AA’s for the h-bridge

the ping is running off of the 5v on the arduino.

 

I dont know the actual specs on the motors themselves, but the chassis i am using was once a skid loader toy that ran on 3 AA’s.

Caps… you need some

Add some .1uf tantulum caps to your motors.

You have the grounds(-) tied

You have the grounds(-) tied together, correct?

**Yes. **

Yes. 

int motor1Pin1 =

Hope it helps. Use the H bridge from Luckylarry

 

int motor1Pin1 = 3;                             // pin 2 on L293D
int motor1Pin2 = 4;                             // pin 7 on L293D
int enable1Pin = 9;                             // pin 1 on L293D
int motor2Pin1 = 5;                             // pin 10 on L293D
int motor2Pin2 = 6;                             // pin  15 on L293D
int enable2Pin = 10;                            // pin 9 on L293D
const int pingPin = 7;  // pin 7 on arduino

void setup() {
 pinMode(motor1Pin1, OUTPUT);
 pinMode(motor1Pin2, OUTPUT);
 pinMode(enable1Pin, OUTPUT);
 pinMode(motor2Pin1, OUTPUT);
 pinMode(motor2Pin2, OUTPUT);
 pinMode(enable2Pin, OUTPUT);
 // set enablePins high so that motor can turn on:
 digitalWrite(enable1Pin, HIGH);
 digitalWrite(enable2Pin, HIGH);
}

float ping () // This is the code that runs the PING)) Sensor
{
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);
  delay(100);
}

void forward() // This function moves the wheels forward
{
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}
void backward() // This function moves the wheels backward
{
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
}
void  haltMotors()
{
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
}
void  turnRight(){// This function turns the robot right.
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}
void loop() {
long duration, inches; // Declare the variable, duration and inches.
duration = pulseIn(pingPin, HIGH);
inches = ping(); // Set the inches variable to the float returned by the ping() function.

while (inches >8){ // While the robot is 8 inches away from an object.
inches = ping();
forward(); // Move the robot forward.
}
haltMotors(); // Stop the motors for 2 seconds, before changing direction.
delay(2000);

while (inches < 10){ // Until the robot is 10 inches away from the object, go backward.
inches = ping();
backward(); // Move the robot backward.
}
turnRight();
delay(5000);

haltMotors();
delay(2000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

I built a similar bot to Makey.

      It might not help but one incredibly weird thing i discovered was that the ping sensor goes nuts in an empty room.    I had just painted my living room and wanted to let G.E.A.R. rome free with no furniture carpets or curtains.    Just hard floors and walls.  It went berserk.   I think old pings where still bouncing around when i took the next reading.   So if its a hard surfaced environment that might be an issue.