int motorLpin1 = 2;
int motorLpin2 = 3;
int motorRpin1 = 4;
int motorRpin2 = 5;
int rightMotorENA = 6;
int leftMotorENB = 9;
int motorSpeed = 255;
int turn=50;
void setup()
{
Serial.begin(9600);
Serial.flush();
pinMode(motorLpin1,OUTPUT);
pinMode(motorLpin2,OUTPUT);
pinMode(motorRpin1,OUTPUT);
pinMode(motorRpin2,OUTPUT);
pinMode(rightMotorENA,OUTPUT);
pinMode(leftMotorENB,OUTPUT);
}
void loop(){
String input="";
while(Serial.available()){
input+=(char)Serial.read();
delay(5);
}
if(input=="n"){
MotorStop();
}
else if(input=="F"){
MotorForward();
}
else if(input=="R"){
MotorBackward();
}
else if(input=="TL"){
MotorLeft();
}
else if(input=="TR"){
MotorRight();
}
else if(input=="RR"){
rightRight();
}
else if(input=="M"){
StraightStraight();
}
}
void MotorForward(void)
{
digitalWrite(motorLpin1,HIGH);
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
}
void MotorBackward(void)
{
digitalWrite(motorLpin1,LOW);
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,LOW);
digitalWrite(motorRpin2,HIGH);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
}
void MotorLeft(void)
{
digitalWrite(motorLpin1,HIGH);
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,LOW);
digitalWrite(motorRpin2,HIGH);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
}
void MotorRight(void)
{
digitalWrite(motorLpin1,LOW);
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
}
void MotorStop(void)
{
digitalWrite(motorLpin1,LOW);
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,LOW);
digitalWrite(motorRpin2,LOW);
digitalWrite(leftMotorENB,LOW);
digitalWrite(rightMotorENA,LOW);
}
void rightRight(void){
digitalWrite(motorLpin1,LOW); //turn right
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(600);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(600);
}
void StraightStraight (void){
digitalWrite(motorLpin1,HIGH); //staright
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(4000);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,LOW); //turn right
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(100);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,HIGH); //straight
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(4000);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,LOW); //turn right
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(100);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,HIGH); //straight
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(4000);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,LOW); //turn right
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(100);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,HIGH); //straight
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(4000);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,LOW); //turn right
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(100);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,HIGH); //straight
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(4000);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,LOW); //turn right
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(100);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,HIGH); //straight
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(4000);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,LOW); //turn right
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(100);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,HIGH); //straight
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(4000);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,LOW); //turn right
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(100);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,HIGH); //straight
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(4000);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,LOW); //turn right
digitalWrite(motorLpin2,HIGH);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(100);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
digitalWrite(motorLpin1,HIGH); //straight
digitalWrite(motorLpin2,LOW);
digitalWrite(motorRpin1,HIGH);
digitalWrite(motorRpin2,LOW);
analogWrite(leftMotorENB,255);
analogWrite(rightMotorENA,255);
delay(4000);
analogWrite(leftMotorENB,LOW);
analogWrite(rightMotorENA,LOW);
delay(500);
}
Comments