Newbe to Arduino. I need help.

I am trying to make a object avoider for a friend who built a bot. I was successful with using bumpers but when I added the ping sensor nothing seems to work. Please check out my code and tell me what I am doing wrong. It seem logical to me.

[quote] 
#include <AFMotor.h>
[color=#CC6600]AF_DCMotor[/color] motor1(1, MOTOR12_64KHZ); 
[color=#CC6600]AF_DCMotor[/color] motor2(2, MOTOR12_64KHZ);
const [color=#996600]int[/color] buttonPin = 2;
const [color=#996600]int[/color] pingPin = 7;
[color=#996600]int[/color] buttonState;

[color=#CC6600]void[/color] [color=#993300][b]setup[/b][/color]() {
  [color=#996600]Serial[/color].[color=#996600]begin[/color](9600);           
  [color=#996600]pinMode[/color](buttonPin, [color=#CC0000]INPUT[/color]);   
  motor1.[color=#996600]setSpeed[/color](200);         
  motor2.[color=#996600]setSpeed[/color](200);        
}

[color=#CC6600]void[/color] [color=#993300][b]loop[/b][/color]() 
{
  [color=#996600]long[/color] duration;
  [color=#996600]pinMode[/color](pingPin, [color=#CC0000]OUTPUT[/color]);
  [color=#996600]digitalWrite[/color](pingPin, [color=#CC0000]LOW[/color]);
  [color=#996600]delayMicroseconds[/color](2);
  [color=#996600]digitalWrite[/color](pingPin, [color=#CC0000]HIGH[/color]);
  [color=#996600]delayMicroseconds[/color](5);
  [color=#996600]digitalWrite[/color](pingPin, [color=#CC0000]LOW[/color]);
  [color=#996600]pinMode[/color](pingPin, [color=#CC0000]INPUT[/color]);
  duration = [color=#996600]pulseIn[/color](pingPin, [color=#CC0000]HIGH[/color]);

  [color=#996600]digitalWrite[/color](buttonPin, [color=#CC0000]HIGH[/color]);
  buttonState=[color=#996600]digitalRead[/color](buttonPin);
  
  [color=#CC6600]if[/color](buttonState == [color=#CC0000]LOW[/color] || duration < 800)
  {
    motor1.[color=#996600]run[/color]([color=#CC0000]BACKWARD[/color]);
    motor2.[color=#996600]run[/color]([color=#CC0000]BACKWARD[/color]);
    [color=#996600]delay[/color](1000);
    motor1.[color=#996600]run[/color]([color=#CC0000]FORWARD[/color]);
    motor2.[color=#996600]run[/color]([color=#CC0000]BACKWARD[/color]);
    [color=#996600]delay[/color](1000);
  }
  [color=#CC6600]else[/color]
  {
    motor1.[color=#996600]run[/color]([color=#CC0000]FORWARD[/color]);
    motor2.[color=#996600]run[/color]([color=#CC0000]FORWARD[/color]);
  }
}

[/quote][tt]
digitalWrite(buttonPin, HIGH);
buttonState=digitalRead(buttonPin);

Are you sure about these two? Appears that you write to the button pin, and then read from it next command.

I don't think there's anything wrong with enabling the pull-ups and then reading back, though you'd normally do the enable just once, in "setup ()". (assuming that's how the bumper switch is wired)

According to the info I have read for the Ping sensor you need to do the input/output switching to initialize the Ping sensor.

According to the info I have read for the Ping sensor you need to do the input/output switching to initialize the Ping sensor

But that’s done by controlling “pingPin”.
No need to switch the button pin, surely?

 pinMode(pingPin, OUTPUT); // <<< HERE
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);
 pinMode(pingPin, INPUT);  // <<< AND HERE

Oh I see what you mean. I put in the write statement for the buttonPin to enable the pull up resistor. My bumper switches are connected to GND pin. If I just use my switches the program works fine. When I added the code for the Ping sensor is when all goes wrong.

Opps hit send too soon - I will move write bumperPin line to the setup area. I agree it only needs to run once. I will try it when I get home.

I move the bumpPin line to the setup area but the bot is still dead. Any other ideas?

Sorry - had to reread that code a couple times.

You're not using the Ping data yet, correct? Just reading it but not processing it?

The pulsein command has a total wait time of 3 minutes if it does not receive data properly. That could be either sensor line never transitions high, or transitions high and stays there.

So check the common issues that Ping is wired to power, sig is connected to Arduino D7. If all is good, try and scope it to see if it responds to your commands at all. Maybe dead sensor or pulled wire?

Check to make sure you have it wired as Ground, +5, Sig

The only thing that I can see at a quick scan that will cause the bot to hang is this:

pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);

What happens if you comment it out?
If the bot still hangs with this not present, then your problem probably isn’t anything to do with the Ping.

[EDIT] OOps, don’t forget to set “duration” to some sensible value, or even read it off a pot.

I will try to comment it out and see if the bot moves again. I will also check the signals with a scope. I am looking for the ping to turn around the robot at about 12 inches. Is the duration variable set about right for this? Can I use a integer instead of a long variable to simplify the use of duration?

Thanks for all the help so far!!!

Cheers, Mike

long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the PING))), there are // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per // second). This gives the distance travelled by the ping, outbound // and return, so we divide by 2 to get the distance of the obstacle. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf return microseconds / 74 / 2; }

Based on this, I'd say 12" X 2 x 74 = 1776

I commented out both lines and the robot started moving again. It acted like my if statement is always true and the light on the ping was flashing. I believe this is what should of happened. Next I commented out just the Duration = line and the bot stopped working. Is there something wrong with my pinmode line or????

I would say that for whatever reason, it's not able to read the pulse from the Ping, so it just sits there waiting for it to respond.

Need to watch on scope what happens during entire process.

I will do that.

Thanks for your help everyone!! I will let you all know what I find.

Cheers,

Mike

Ok I put the Ping to a scope and all looks good. I changed the program so I could see what the ping is doing. I have found that the ping seems to work. I see the logic change the direction. What does not work is the motors do not work. Sometimes the motors will move super slow and you can sometimes here the motors trying to move but still no go. So what I am thinking now is I have some kind of power problem. I am using the motor shield with a separate power supply of 4 AA batteries to give me 6 volts. Is this not enough? Has any one else had this problem? I have included my current code just in cas some one sees something else I missed.

Thanks

#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ); 
AF_DCMotor motor2(2, MOTOR12_64KHZ);
const int pingPin = 7;

 void setup() {
   // initialize serial communication:
   Serial.begin(9600);
   motor1.setSpeed(250);         
   motor2.setSpeed(250);
 }

 void loop()
 {
   // establish variables for duration of the ping, 
   // and the distance result in inches and centimeters:
   long duration, inches, cm;

   // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
   // 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 of an object.
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);

   // convert the time into a distance
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);

   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   if(inches < 18)
  {
    Serial.print("Back ");
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    //delay(1000);
    motor1.run(FORWARD);
    motor2.run(BACKWARD);
    //delay(1000);
  }
  else
  {
    Serial.print("Forward ");
    motor1.run(FORWARD);
    motor2.run(FORWARD);
  }
  delay(100);
 }
 long microsecondsToInches(long microseconds)
 {
   // According to Parallax's datasheet for the PING))), there are
   // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
   // second).  This gives the distance travelled by the ping, outbound
   // and return, so we divide by 2 to get the distance of the obstacle.
   // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
   return microseconds / 74 / 2;
 }

 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 travelled.
   return microseconds / 29 / 2;
 }