PROGRAM
#define ir_right_sensor A5
#define ir_left_sensor A4
#define left11 6
#define left12 5
#define right11 8
#define right12 7
int ir_left,ir_right;
void setup()
{
pinMode(ir_left_sensor,INPUT);
pinMode(ir_right_sensor,INPUT);
Serial.begin(9600);
Serial.println(“haiu”);
motorinit();
forward();
}
void loop()
{
//do
{
ir_left = digitalRead(ir_left_sensor);
ir_right = digitalRead(ir_right_sensor);
//delay(300);
Serial.println(“ok”);
}
//while((ir_left & ir_right));
Serial.println(“crossing”);
//stop1();
if(!ir_left )
{
Serial.print(“ir_left = “);Serial.println(ir_left);
right();delay_for_motor_operation();while(!digitalRead(ir_left_sensor));
}
else if(!ir_right)
{
Serial.print(“ir_right = “);Serial.println(ir_right);
left();delay_for_motor_operation();while(!digitalRead(ir_right_sensor));
}
else
{
forward();Serial.println(“forward”);
}
}
void forward(void)
{
digitalWrite(left11,HIGH);digitalWrite(left12, LOW);digitalWrite(right11, HIGH);digitalWrite(right12, LOW);//FRD
}
void back(void)
{
//BACK
digitalWrite(left11, LOW); digitalWrite(left12,HIGH);digitalWrite(right11, LOW); digitalWrite(right12,HIGH);
}
void right(void)
{
digitalWrite(left11,HIGH);digitalWrite(left12, LOW);digitalWrite(right11, LOW);digitalWrite(right12, LOW);//LEFT
}
void left(void)
{
digitalWrite(left11, LOW);digitalWrite(left12, LOW);digitalWrite(right11, HIGH);digitalWrite(right12, LOW);//RIGHT
}
void stop1(void)
{
digitalWrite(left11, LOW);digitalWrite(left12, LOW);digitalWrite(right11, LOW);digitalWrite(right12, LOW); //STOP
}
void motorinit(void)
{
pinMode(left11,OUTPUT); pinMode(left12, OUTPUT); pinMode(right11,OUTPUT); pinMode(right12, OUTPUT);
}
void delay_for_motor_operation (void )
{
delay(500);
}