Last active
March 2, 2019 18:39
-
-
Save samilkorkmaz/98bab8a285f7615e17a11e31735c8ac9 to your computer and use it in GitHub Desktop.
Four DC toy motors controlled with Arduino Pro Micro (similar to Leonardo), HC-06 Bluetooth module and L293B motor driver.
This file contains 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
//Four DC toy motors controlled with Arduino Pro Micro (similar to Leonardo), HC-06 Bluetooth module and L293B motor driver. | |
//Motor settings: | |
const int pwmMotorLeft = 6; | |
const int in1MotorLeft = 14; | |
const int in2MotorLeft = 15; | |
const int pwmMotorRight = 10; | |
const int in1MotorRight = 16; | |
const int in2MotorRight = 9; | |
const int pwmMotorTower = 3; | |
const int in1MotorTower = 8; | |
const int in2MotorTower = 2; | |
const int pwmMotorArm = 5; | |
const int in1MotorArm = 7; | |
const int in2MotorArm = 4; | |
void setup() { | |
Serial.begin(9600); | |
Serial1.begin(9600);//Serial1 is required to use RX pin of pro micro to receive data from HC-06 TX pin. | |
delay(1000); //For the following println to work | |
Serial.println("Setup finished."); | |
} | |
void stopMotors() { | |
digitalWrite(in1MotorLeft, LOW); | |
digitalWrite(in2MotorLeft, LOW); | |
digitalWrite(in1MotorRight, LOW); | |
digitalWrite(in2MotorRight, LOW); | |
digitalWrite(in1MotorTower, LOW); | |
digitalWrite(in2MotorTower, LOW); | |
digitalWrite(in1MotorArm, LOW); | |
digitalWrite(in2MotorArm, LOW); | |
} | |
void goForward() { | |
digitalWrite(in1MotorLeft, LOW); | |
digitalWrite(in2MotorLeft, HIGH); | |
digitalWrite(in1MotorRight, LOW); | |
digitalWrite(in2MotorRight, HIGH); | |
} | |
void goBackward() { | |
digitalWrite(in1MotorLeft, HIGH); | |
digitalWrite(in2MotorLeft, LOW); | |
digitalWrite(in1MotorRight, HIGH); | |
digitalWrite(in2MotorRight, LOW); | |
} | |
void turnRight() { | |
digitalWrite(in1MotorLeft, HIGH); | |
digitalWrite(in2MotorLeft, LOW); | |
digitalWrite(in1MotorRight, LOW); | |
digitalWrite(in2MotorRight, HIGH); | |
} | |
void turnLeft() { | |
digitalWrite(in1MotorLeft, LOW); | |
digitalWrite(in2MotorLeft, HIGH); | |
digitalWrite(in1MotorRight, HIGH); | |
digitalWrite(in2MotorRight, LOW); | |
} | |
void turnTowerRight() { | |
digitalWrite(in1MotorTower, LOW); | |
digitalWrite(in2MotorTower, HIGH); | |
} | |
void turnTowerLeft() { | |
digitalWrite(in1MotorTower, HIGH); | |
digitalWrite(in2MotorTower, LOW); | |
} | |
void lowerArm() { | |
digitalWrite(in1MotorArm, LOW); | |
digitalWrite(in2MotorArm, HIGH); | |
} | |
void raiseArm() { | |
digitalWrite(in1MotorArm, HIGH); | |
digitalWrite(in2MotorArm, LOW); | |
} | |
void loop() { | |
int delay_ms = 5; | |
int avgSpeed = 8*30; | |
if (Serial1.available()) { | |
Serial.print("Serial1.available(). data: "); | |
char data = Serial1.read(); | |
Serial.println(data); | |
if(data == '1') raiseArm(); | |
if(data == '0') lowerArm(); | |
if(data == 'F') goForward(); | |
if(data == 'B') goBackward(); | |
if(data == 'L') turnLeft(); | |
if(data == 'R') turnRight(); | |
if(data == 'A') turnTowerLeft(); | |
if(data == 'D') turnTowerRight(); | |
if(data == 'S') stopMotors(); | |
} | |
analogWrite(pwmMotorLeft, avgSpeed); | |
analogWrite(pwmMotorRight, avgSpeed); | |
analogWrite(pwmMotorTower, avgSpeed); | |
analogWrite(pwmMotorArm, avgSpeed); | |
delay(delay_ms); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment