Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * created by Rui Santos, https://randomnerdtutorials.com
- * Control 2 DC motors with Smartphone via bluetooth
- */
- int motor1Pin1 = 10; // pin 2 on L293D IC
- int motor1Pin2 = 11; // pin 7 on L293D IC
- int enable1Pin = 6; // pin 1 on L293D IC
- int motor2Pin1 = 12; // pin 10 on L293D IC
- int motor2Pin2 = 13; // pin 15 on L293D IC
- int enable2Pin = 11; // pin 9 on L293D IC
- 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);
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, LOW);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, LOW);
- // sets enable1Pin and enable2Pin 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 'F' the DC motor will go forward
- if (state == 'f') {
- digitalWrite(motor1Pin1, HIGH);
- digitalWrite(motor1Pin2, LOW);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, LOW);
- }
- else if (state == 'l') {
- digitalWrite(motor1Pin1, HIGH);
- digitalWrite(motor1Pin2, LOW);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, HIGH);
- }
- else if (state == 'r') {
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, HIGH);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, HIGH);
- }
- else if (state == 'a') {
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, HIGH);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, LOW);
- }
- else if (state == 's') {
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, LOW);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement