Let's Make Robots!

Arduino car

Ok I have this arduino powered autonomis car. It uses a Sharp proximity sensor. It runs well when it does not sense anyting but when i put my hand near it sometimes is turns but other times it stops and the motor moves a tiny bit and then it stops. 

Here is a video ..... http://www.youtube.com/watch?v=qrxh59XDGT8&feature=youtu.be

Side comments

when it stops the tx and rx lights on the arduino are flashing (simmilar to when it finds an object). After a while (a minute or two... or sometimes never - have to restart it) when it continues going strait it stops blinking.


this is my code


int d1a=13;

int d2a= 12;

int s1= 10;

int d1b=11;

int d2b=8;

int sb=9;

int sensorPin = 5;

void setup () {  


  pinMode (d1a, OUTPUT);

  pinMode (d2a, OUTPUT);

  pinMode (s1, OUTPUT);

  pinMode (d1b, OUTPUT);

  pinMode (d2b, OUTPUT);

  pinMode (sb, OUTPUT);


void loop () {

  int val= analogRead (sensorPin);


    if (val > 520) {

     analogWrite (s1, 255);

digitalWrite (d1a , HIGH);

digitalWrite (d2a, LOW);

analogWrite (sb, 255);

digitalWrite (d1b , HIGH);

digitalWrite (d2b, LOW);

delay (1500);

      analogWrite (s1, 190);

digitalWrite (d1a , HIGH);

digitalWrite (d2a, LOW);

analogWrite (sb, 190);

digitalWrite (d1b , LOW);

digitalWrite (d2b, HIGH);

  delay (1000);



    else {

        analogWrite (s1, 200);

digitalWrite (d1a ,LOW);

digitalWrite (d2a, HIGH);

analogWrite (sb, 200);

digitalWrite (d1b , LOW);

digitalWrite (d2b, HIGH);



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

As Bird says, functions rock.  

The video doesn't give much more information.  Some still photos of the wire-up would do wonders here.  For instance, is your sensor wired to Digital 5 or A5?  I'm guessing you're in Digital 5, which doesn't have an ADC on it so analogRead of the SharpIR compiles but makes no sense to the microcontroller (the value will always be 5 or 6.)  Also make sure it has power to it correctly-Gnd is center pin on Sharps where on most standard sensor harnesses Vcc is center.  Out of curiosity which is it-the 2YOA21 or the 2D120X?

One other thing that may or may not be giving you troubles here is that you don't give it the go ahead to go forward unless it gets to the "else."  Theoretically that should do it, but that's not always the way it goes.  Try putting it in gear in your setup or before your interrupt condition and see what happens.

it is connected to analog 

and it is the sharp 2yoa21

what do you mean by 

.  Try putting it in gear in your setup or before your interrupt condition and see what happens.

You have to define it with an "A" then.

int sensorPin=A5;

We could still use some pictures here.

He means, in your setup () function, make it go forward, so it is already moving before it gets to your loop() function.

btw funny subject...

If you would put your motor commands in functions, you code would be easier to read.

Instead of your if (val > 520) { ... }

You could:

if (val > 520) {

Now outside of the closed brace '}' of the loop() function you add:

void reverse(int speed) {
    analogWrite (s1, speed);
    digitalWrite (d1a , HIGH);
    digitalWrite (d2a, LOW);
    analogWrite (sb, speed);
    digitalWrite (d1b , HIGH);
    digitalWrite (d2b, LOW);

make functions for forward, right, and, left. Now when anyone looks at your code, they can immediately see what is supposed to be happening instead of getting bogged down in the low level pin read and writes. :) 

That is cool tanks bird

Without knowing how you've got this wired we can only really guess, but just looking at the code and not knowing anything about the motor interface (shield? breadboard?) or seeing if you've got the sensor in the right rail, it's probably not going to be enough to go on.  Any pics if it's breadboarded or custom shielded?  Also, your variable conventions are a little confusing.  Here's what I see though: You declare your pins and then use pin numbers instead of those declarations to do your writes.  You have no delay in your turning condition, which may not allow the car enough time to back up enough to get out of the condition loop.  Then there's no real reason to have a serial line/window (unless you just like dragging a USB umbilical cable around behind the car ;-)

Ok i edidted my code.. and posted a video. What do you mean i have no delay in my turning condition? i though it did, What do you mean. 

Btw i had the serial for something before when i was making sure the proximity sensor works.