//motor A connected between A01 and A02 //motor B connected between B01 and B02 int x=0; int sensorIR = 0; // analog pin for reading the IR sensor int PWM1=8; int led=53; float sensorValue, inches, cm; //Must be of type float for pow() //Motor A int AIN1 = 22; //Direction int AIN2 = 23; //Direction int AIN3 = 26; int AIN4 = 27; //Motor B int BIN1 = 24; //Direction int BIN2 = 25; //Direction int BIN3 = 28; //Direction int BIN4 = 29; //Direction void setup(){ Serial.begin(9600); //set serial to 9600 baud rate pinMode(led, OUTPUT); pinMode(PWM1, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(AIN3, OUTPUT); pinMode(AIN4, OUTPUT); pinMode(BIN1, OUTPUT); pinMode(BIN2, OUTPUT); pinMode(BIN3, OUTPUT); pinMode(BIN4, OUTPUT); } void loop(){ if (Serial.available()) { // Check if there is a new message x = Serial.read(); // Put the serial input into the message switch (x) { // Motors directions case 'g': // rigth analogWrite(PWM1,255); //RIGH 100% digitalWrite(AIN1, LOW); //MOTOR1 digitalWrite(AIN2, HIGH); //MOTOR1 digitalWrite(BIN1, HIGH); //MOTOR2 digitalWrite(BIN2, LOW); //MOTOR2 digitalWrite(BIN3, LOW) ; //MOTOR3 digitalWrite(BIN4, HIGH); //MOTOR3 digitalWrite(AIN3, LOW); //MOTOR4 digitalWrite(AIN4, HIGH); //MOTOR4 break; case 'h': // rigth analogWrite(PWM1,170); //RIGH 100% digitalWrite(AIN1, LOW); //MOTOR1 digitalWrite(AIN2, HIGH); //MOTOR1 digitalWrite(BIN1, HIGH); //MOTOR2 digitalWrite(BIN2, LOW); //MOTOR2 digitalWrite(BIN3, LOW) ; //MOTOR3 digitalWrite(BIN4, HIGH); //MOTOR3 digitalWrite(AIN3, LOW); //MOTOR4 digitalWrite(AIN4, HIGH); //MOTOR4 break; case 'j': // rigth analogWrite(PWM1,85); //RIGH 100% digitalWrite(AIN1, LOW); //MOTOR1 digitalWrite(AIN2, HIGH); //MOTOR1 digitalWrite(BIN1, HIGH); //MOTOR2 digitalWrite(BIN2, LOW); //MOTOR2 digitalWrite(BIN3, LOW) ; //MOTOR3 digitalWrite(BIN4, HIGH); //MOTOR3 digitalWrite(AIN3, LOW); //MOTOR4 digitalWrite(AIN4, HIGH); //MOTOR4 break; case 's': // stop analogWrite(PWM1,0); digitalWrite(AIN1, LOW); digitalWrite(AIN2, LOW); digitalWrite(BIN1, LOW); digitalWrite(BIN2, LOW); digitalWrite(AIN3, LOW); digitalWrite(AIN4, LOW); digitalWrite(BIN3, LOW); digitalWrite(BIN4, LOW); break; case 'k': // left analogWrite(PWM1,85); //LEFT 100% digitalWrite(AIN1, HIGH); //MOTOR1 digitalWrite(AIN2, LOW); //MOTOR1 digitalWrite(BIN1, LOW); //MOTOR2 digitalWrite(BIN2, HIGH); //MOTOR2 digitalWrite(BIN3, HIGH) ; //MOTOR3 digitalWrite(BIN4, LOW); //MOTOR3 digitalWrite(AIN3, HIGH); //MOTOR4 digitalWrite(AIN4, LOW); //MOTOR4 break; case 'l': // left analogWrite(PWM1,170); //LEFT 100% digitalWrite(AIN1, HIGH); //MOTOR1 digitalWrite(AIN2, LOW); //MOTOR1 digitalWrite(BIN1, LOW); //MOTOR2 digitalWrite(BIN2, HIGH); //MOTOR2 digitalWrite(BIN3, HIGH) ; //MOTOR3 digitalWrite(BIN4, LOW); //MOTOR3 digitalWrite(AIN3, HIGH); //MOTOR4 digitalWrite(AIN4, LOW); //MOTOR4 break; case 'q': // left analogWrite(PWM1,255); //LEFT 100% digitalWrite(AIN1, HIGH); //MOTOR1 digitalWrite(AIN2, LOW); //MOTOR1 digitalWrite(BIN1, LOW); //MOTOR2 digitalWrite(BIN2, HIGH); //MOTOR2 digitalWrite(BIN3, HIGH) ; //MOTOR3 digitalWrite(BIN4, LOW); //MOTOR3 digitalWrite(AIN3, HIGH); //MOTOR4 digitalWrite(AIN4, LOW); //MOTOR4 break; case 'b': // forward analogWrite(PWM1,255); //FORWARD 100% digitalWrite(AIN1, HIGH); //MOTOR1 digitalWrite(AIN2, LOW); //MOTOR1 digitalWrite(BIN1, HIGH); //MOTOR2 digitalWrite(BIN2, LOW); //MOTOR2 digitalWrite(BIN3, HIGH); //MOTOR3 digitalWrite(BIN4, LOW); //MOTOR3 digitalWrite(AIN3, LOW); //MOTOR4 digitalWrite(AIN4, HIGH); //MOTOR4 break; case 'z': // forward analogWrite(PWM1,170); //FORWARD 100% digitalWrite(AIN1, HIGH); //MOTOR1 digitalWrite(AIN2, LOW); //MOTOR1 digitalWrite(BIN1, HIGH); //MOTOR2 digitalWrite(BIN2, LOW); //MOTOR2 digitalWrite(BIN3, HIGH); //MOTOR3 digitalWrite(BIN4, LOW); //MOTOR3 digitalWrite(AIN3, LOW); //MOTOR4 digitalWrite(AIN4, HIGH); //MOTOR4 break; case 'x': // forward analogWrite(PWM1,85); //FORWARD 100% digitalWrite(AIN1, HIGH); //MOTOR1 digitalWrite(AIN2, LOW); //MOTOR1 digitalWrite(BIN1, HIGH); //MOTOR2 digitalWrite(BIN2, LOW); //MOTOR2 digitalWrite(BIN3, HIGH); //MOTOR3 digitalWrite(BIN4, LOW); //MOTOR3 digitalWrite(AIN3, LOW); //MOTOR4 digitalWrite(AIN4, HIGH); //MOTOR4 break; case 'a': // backward analogWrite(PWM1,85); //BACKWARD 100% digitalWrite(AIN1, LOW); //MOTOR1 digitalWrite(AIN2, HIGH); //MOTOR1 digitalWrite(BIN1, LOW); //MOTOR2 digitalWrite(BIN2, HIGH); //MOTOR2 digitalWrite(BIN3, LOW); //MOTOR3 digitalWrite(BIN4, HIGH); //MOTOR3 digitalWrite(AIN3, HIGH); //MOTOR4 digitalWrite(AIN4, LOW); //MOTOR4 break; case 'd': // backward analogWrite(PWM1,170); //BACKWARD 100% digitalWrite(AIN1, LOW); //MOTOR1 digitalWrite(AIN2, HIGH); //MOTOR1 digitalWrite(BIN1, LOW); //MOTOR2 digitalWrite(BIN2, HIGH); //MOTOR2 digitalWrite(BIN3, LOW); //MOTOR3 digitalWrite(BIN4, HIGH); //MOTOR3 digitalWrite(AIN3, HIGH); //MOTOR4 digitalWrite(AIN4, LOW); //MOTOR4 break; case 'f': // backward analogWrite(PWM1,255); //BACKWARD 100% digitalWrite(AIN1, LOW); //MOTOR1 digitalWrite(AIN2, HIGH); //MOTOR1 digitalWrite(BIN1, LOW); //MOTOR2 digitalWrite(BIN2, HIGH); //MOTOR2 digitalWrite(BIN3, LOW); //MOTOR3 digitalWrite(BIN4, HIGH); //MOTOR3 digitalWrite(AIN3, HIGH); //MOTOR4 digitalWrite(AIN4, LOW); //MOTOR4 break; } } }