top of page

RC Car code

Layout:

Code:

/*

 * created by The Arduinos, Micah, Leo, James

 * Control 2 DC motors with Smartphone via bluetooth

 * 2015

 */

int motor1Pin1 = 3; // pin 2 on L293D IC

int motor1Pin2 = 4; // pin 7 on L293D IC

int enable1Pin = 6; // pin 1 on L293D IC

int motor2Pin1 = 8; // pin 10 on L293D IC

int motor2Pin2 = 9; // pin 15 on L293D IC

int enable2Pin = 11; // pin 9 on L293D IC

int led1 = 12;

int led2 = 13;

int state;

int flag=0;        //makes sure that the serial only prints once the state

int stateStop=0;

void setup() {

    // sets the pins as outputs:

    pinMode(motor1Pin1, OUTPUT);

    pinMode(motor1Pin2, OUTPUT);

    pinMode(enable1Pin, OUTPUT);

    pinMode(motor2Pin1, OUTPUT);

    pinMode(motor2Pin2, OUTPUT);

    pinMode(enable2Pin, OUTPUT);

    pinMode(led1, OUTPUT);

    pinMode(led2, OUTPUT);

    // sets enable1Pin and enable2Pin high so that motor can turn on:

    digitalWrite(enable1Pin, HIGH);

    digitalWrite(enable2Pin, HIGH);

    // initialize serial communication at 9600 bits per second:

    Serial.begin(9600);

}

 

void loop() {

    //if some date is sent, reads it and saves in state

    if(Serial.available() > 0){     

      state = Serial.read();   

      flag=0;

    }   

    // if the state is '1' the DC motor will go forward

    if (state == '1') {

        digitalWrite(enable1Pin, 255);

        digitalWrite(enable2Pin, 255);

        digitalWrite(motor1Pin1, LOW);

        digitalWrite(motor1Pin2, HIGH); 

        digitalWrite(motor2Pin1, LOW);

        digitalWrite(motor2Pin2, LOW);

        if(flag == 0){

          Serial.println("Go Forward!");

          flag=1;

        }

    }

    

    // if the state is '2' the motor will turn left

    else if (state == '2') {

        digitalWrite(enable1Pin, 255);

        digitalWrite(enable2Pin, 255);

        digitalWrite(motor1Pin1, LOW);

        digitalWrite(motor1Pin2, HIGH); 

        digitalWrite(motor2Pin1, HIGH);

        digitalWrite(motor2Pin2, LOW);

        if(flag == 0){

          Serial.println("Turn LEFT");

          flag=1;

        }

    }

    // if the state is '3' the motor will Stop

    else if (state == '3' || stateStop == 1) {

        digitalWrite(motor1Pin1, LOW); 

        digitalWrite(motor1Pin2, LOW); 

        digitalWrite(motor2Pin1, LOW);

        digitalWrite(motor2Pin2, LOW);

        if(flag == 0){

          Serial.println("STOP!");

          flag=1;

        }

        stateStop=0;

    }

    // if the state is '4' the motor will turn right

    else if (state == '4') {

        digitalWrite(enable1Pin, 255);

        digitalWrite(enable2Pin, 255);

        digitalWrite(motor1Pin1, LOW);

        digitalWrite(motor1Pin2, HIGH); 

        digitalWrite(motor2Pin1, LOW);

        digitalWrite(motor2Pin2, HIGH);

        if(flag == 0){

          Serial.println("Turn RIGHT");

          flag=1;

        }

    }

    // if the state is '5' the motor will Reverse

    else if (state == '5') {

        digitalWrite(enable1Pin, 255);

        digitalWrite(enable2Pin, 255);

        digitalWrite(motor1Pin1, HIGH); 

        digitalWrite(motor1Pin2, LOW);

        digitalWrite(motor2Pin1, LOW);

        digitalWrite(motor2Pin2, LOW);

        if(flag == 0){

          Serial.println("Reverse!");

          flag=1;

        }

    }

    else if (state == '6') {

        digitalWrite(enable1Pin, 255);

        digitalWrite(enable2Pin, 255);

        digitalWrite(motor1Pin1, HIGH); 

        digitalWrite(motor1Pin2, LOW);

        digitalWrite(motor2Pin1, HIGH);

        digitalWrite(motor2Pin2, LOW);

        if(flag == 0){

          Serial.println("Reverse Right!");

          flag=1;

        }

    }

    else if (state == '7') {

        digitalWrite(enable1Pin, 255);

        digitalWrite(enable2Pin, 255);

        digitalWrite(motor1Pin1, HIGH); 

        digitalWrite(motor1Pin2, LOW);

        digitalWrite(motor2Pin1, LOW);

        digitalWrite(motor2Pin2, HIGH);

        if(flag == 0){

          Serial.println("Reverse Left!");

          flag=1;

        }

    }

    else if (state == '8') {

      digitalWrite(led1, HIGH);

      digitalWrite(led2, HIGH);

      if(flag == 0){

        Serial.println("Lights On!");

        flag=1;

      }

    }

    else if (state == '9') {

    digitalWrite(led1, LOW);

    digitalWrite(led2, LOW);

    if(flag == 0){

      Serial.println("Lights Off!");

      flag=1;

      }

    }

    else if (state == 'a') {

      digitalWrite(enable2Pin, 255);

      digitalWrite(enable1pin, 255);

      digitalWrite(motor1Pin1, LOW); 

      digitalWrite(motor1Pin2, HIGH);

      digitalWrite(motor2Pin1, LOW);

      digitalWrite(motor2Pin2, LOW);

      delay(7500);

      digitalWrite(enable1Pin, 255);

      digitalWrite(enable2Pin, 255);

      digitalWrite(motor1Pin1, LOW); 

      digitalWrite(motor1Pin2, HIGH);

      digitalWrite(motor2Pin1, LOW);

      digitalWrite(motor2Pin2, HIGH);

      delay(10); 

      digitalWrite(enable1Pin, 255);

      digitalWrite(enable2Pin, 255);

      digitalWrite(motor1Pin1, LOW); 

      digitalWrite(motor1Pin2, HIGH);

      digitalWrite(motor2Pin1, HIGH);

      digitalWrite(motor2Pin2, LOW);

      delay(20000);

      if(flag == 0){

        Serial.println("Patrol!");

        flag=1;

      }

    }

    //For debugging purpose

    //Serial.println(state);

}

 

bottom of page