/* * created by Rui Santos, http://randomnerdtutorials.wordpress.com * Control DC motor with Smartphone via bluetooth * 2013 */ int motor1 = 6;// pin 1 on L293D IC int motor2 = 7; int motor3 = 8; int motor4 = 9; int state; int flag=0; //makes sure that the serial only prints once the state void setup() { // sets the pins as outputs: pinMode(motor1, OUTPUT); pinMode(motor2, OUTPUT); pinMode(motor3, OUTPUT); pinMode(motor4, OUTPUT); // sets enablePin high so that motor can turn on: // 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 '0' the DC motor will turn off if (state == '1') { digitalWrite(motor1, HIGH);// set pin 7 on L293D low digitalWrite(motor3, HIGH); digitalWrite(motor2, LOW); digitalWrite(motor4, LOW); if(flag == 0){ Serial.println("Motor: off"); flag=1; } } // if the state is '1' the motor will turn right if (state == '2') { digitalWrite(motor1, LOW); digitalWrite(motor2, HIGH); digitalWrite(motor3, LOW); digitalWrite(motor4, HIGH); // set pin 7 on L293D high flag=1; } if (state =='3') { digitalWrite(motor1, HIGH); digitalWrite(motor2, LOW); digitalWrite(motor3, LOW); digitalWrite(motor4, HIGH); } if (state =='4') { digitalWrite(motor1, LOW); digitalWrite(motor2, HIGH); digitalWrite(motor3, HIGH); digitalWrite(motor4, LOW); } }