Last active
March 2, 2022 09:57
-
-
Save EinsteinUnicorn/dcb707d95b556929bb61 to your computer and use it in GitHub Desktop.
Still a work in progress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Becky Button. This code is made such that the motor 1 (m1p1 m1p2) is on the left and Motor 2 (m2p1, m2p2) is on the right. | |
| I will be posting an instructables soon on how to make this work, with all of the schematics and such.*/ | |
| int m1Pin1 = 3; // pin 2 on L293D IC | |
| int m1Pin2 = 4; // pin 7 on L293D IC | |
| int enablePinm1 = 5; // pin 1 on L293D IC | |
| int m2Pin1 = 6; | |
| int m2Pin2 = 7; | |
| int enablePinm2; | |
| int state; | |
| int flag=0; //makes sure that the serial only prints once the state | |
| void setup() | |
| { | |
| // sets the pins as outputs: | |
| pinMode(m1Pin1, OUTPUT); | |
| pinMode(m1Pin2, OUTPUT); | |
| pinMode(m2Pin1, OUTPUT); | |
| pinMode(m2Pin2, OUTPUT); | |
| pinMode(enablePinm1, OUTPUT); | |
| pinMode(enablePinm1, OUTPUT); | |
| // sets enablePin high so that motor can turn on: | |
| digitalWrite(enablePin, HIGH); | |
| digitalWrite(enablePin, 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 '0' the DC motor will turn off | |
| if (state == '0') { | |
| digitalWrite(m1Pin1, LOW); // set pin 2 on L293D low | |
| digitalWrite(m1Pin2, LOW); // set pin 7 on L293D low | |
| digitalWrite(m2Pin1, LOW); | |
| digitalWrite(m2Pin2, LOW); | |
| if(flag == 0) | |
| { | |
| Serial.println("off"); | |
| flag=1; | |
| } | |
| } | |
| // if the state is '1' the bot will turn right | |
| else if (state == '1') { | |
| digitalWrite(m1Pin1, LOW); // set pin 2 on L293D low | |
| digitalWrite(m1Pin2, HIGH); // set pin 7 on L293D high | |
| if(flag == 0){ | |
| Serial.println("Motor: right"); | |
| flag=1; | |
| } | |
| } | |
| // if the state is '2' the bot will turn left | |
| else if (state == '2') { | |
| digitalWrite(m1Pin1, HIGH); // set pin 2 on L293D high | |
| digitalWrite(m1Pin2, LOW); // set pin 7 on L293D low | |
| if(flag == 0){ | |
| Serial.println("Motor: left"); | |
| flag=1; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment