AFTER I BURN THIS CODE THE WHEELS MOVE FARWARD ONLY IT DOESNT TURN RIGHT OR LEFT,
S1,s2......are IR sensors.
int s1,s2,s3,s4,s5;
int ss1,ss2,ss3,ss4,ss5;
char P0_0,P0_1,P0_2,P0_3,P0_4,P0_5;
/*
P0_0 --------> Enable pin of the left half of the H-bridge
P0_1 --------> will drive the left motor in forward direction
P0_2 --------> will drive the left motor in reverse direction
P0_3 --------> will drive the right motor in forward direction
P0_4 --------> Enable pin of the right half of the H-bridge
P0_5 --------> will drive the right motor in reverse direction
*/
void Forward()
{
P0_1=HIGH;
P0_2=LOW;
P0_3=HIGH;
P0_5=LOW;
}
/*Generally for turning we use a pulsated wave so the bOt doesnt get out of
control i.e. we run the motor for sometime then again stop it and this is done
very quickly to create an effective pulse. See the function below.*/
void TurnLeft()
{
P0_1=LOW; /*Left motor is not running in any direction.*/
P0_2=LOW;
P0_3=HIGH; /*Right motor is running in forward direction. bOt will eventually turn left*/
P0_5=LOW;
delay(50); /* Wait for 50 ms*/
P0_1=LOW;/*Motors are not running*/
P0_2=LOW;
P0_3=LOW;
P0_5=LOW;
delay(50); /*Delay of another 50 ms*/
}
/*So in the above program we have effectively created a pulse of 100ms which
is on for 50ms and off for another 50ms. You can change this value to suit
your needs*/
/*Similarly we can write a function to turn right*/
void TurnRight()
{
P0_1=HIGH; /*Left motor running in forward direction.*/
P0_2=LOW;
P0_3=LOW; /*Right motor is not running.*/
P0_5=LOW;
delay(50); /*50ms time delay*/
P0_1=LOW; /*Motors not running in any direction*/
P0_2=LOW;
P0_3=LOW;
P0_5=LOW;
delay(50); /*50ms time delay*/
}
void setup()
{
Serial.begin(9600);
ss1=analogRead(0);
ss2=analogRead(1);
ss3=analogRead(2);
ss4=analogRead(3);
ss5=analogRead(4);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
}
void loop()
{
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
digitalWrite(2,P0_1);
digitalWrite(3,P0_2);
digitalWrite(4,P0_3);
digitalWrite(5,P0_5);
/*
//main loop of the program
*/
if (ss1>996)
s1=1;
else
s1=0;
if (ss2>996)
s2=1;
else
s2=0;
if (ss3>996)
s3=1;
else
s3=0;
if (ss4>996)
s4=1;
else
s4=0;
if (ss5>996)
s5=1;
else
s5=0;
/*STEERING */
if (s3==1)
Forward();
if(s2==1||s1==1)
TurnLeft();
else
if(s4==1||s5==1)
TurnRight();
if(s1==1&&s5==1&&s3==1);
{
TurnRight();
}
}
Moderator edit: [code] ...
[/code] tags added. Also reformatted.
(Nick Gammon)