int Left_motor_go=8;
int Left_motor_back=9;
int Right_motor_go=10;
int Right_motor_back=11;
int key=A2;
int beep=A3;
int LED=7;
const int SensorRight_2 = 5;
const int SensorLeft_2 = 6;
int SR_2;
int SL_2;
void setup()
{
pinMode(Left_motor_go,OUTPUT);
pinMode(Left_motor_back,OUTPUT);
pinMode(Right_motor_go,OUTPUT);
pinMode(Right_motor_back,OUTPUT);
pinMode(key,INPUT);
pinMode(beep,OUTPUT);
pinMode(LED,OUTPUT);
pinMode(SensorRight_2, INPUT);
}
void run()
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,160);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,160);
}
void brake()
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
}
void left()
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,0);
}
void spin_left( )
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);
analogWrite(Left_motor_back,0);
}
void right()
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,200);
}
void spin_right()
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,200);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,200);
}
void back()
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,150);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,150);
analogWrite(Left_motor_back,0);
}
void keysacn()
{
int val;
val=digitalRead(key);
while(!digitalRead(key))
{
val=digitalRead(key);
}
while(digitalRead(key))
{
delay(10);
val=digitalRead(key);
if(val==HIGH)
{
digitalWrite(beep,HIGH);
while(!digitalRead(key))
digitalWrite(beep,LOW);
}
else
digitalWrite(beep,LOW);
}
}
void loop()
{
keysacn();
while(1)
{
SR_2 = digitalRead(SensorRight_2);
SL_2 = digitalRead(SensorLeft_2);
if (SL_2 == HIGH&&SR_2==HIGH)
{
run();
digitalWrite(beep,LOW);
}
else if (SL_2 == HIGH & SR_2 == LOW)
left();
else if (SR_2 == HIGH & SL_2 == LOW)
spin_right();
else
{
digitalWrite(beep,HIGH);
brake();
delay(300);
back();
delay(400);
left();
delay(500);
}
}
}