Last active
January 20, 2020 07:14
-
-
Save ShawnHymel/ddf4ab49a7c69a530864f365fde689df to your computer and use it in GitHub Desktop.
Workshop - 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
// Pins | |
int switchPin = 7; | |
const int BIN1 = 8; | |
const int BIN2 = 9; | |
const int PWMB = 10; | |
const int PWMA = 11; | |
const int AIN2 = 12; | |
const int AIN1 = 13; | |
// Parameters | |
const int driveTime = 20; | |
const int turnTime = 8; | |
// Globals | |
String botDirection; | |
String distance; | |
void setup() | |
{ | |
// We use the switch to enable motors | |
pinMode(switchPin, INPUT_PULLUP); | |
// Set motor driver pins as output | |
pinMode(AIN1, OUTPUT); | |
pinMode(AIN2, OUTPUT); | |
pinMode(PWMA, OUTPUT); | |
pinMode(BIN1, OUTPUT); | |
pinMode(BIN2, OUTPUT); | |
pinMode(PWMB, OUTPUT); | |
} | |
void loop() | |
{ | |
if(digitalRead(switchPin) == LOW) { | |
//*** YOUR ROBOT CODE*** | |
// Turn right | |
leftMotor(255); | |
rightMotor(0); | |
delay(1000); | |
// Turn left | |
leftMotor(0); | |
rightMotor(255); | |
delay(1000); | |
// Go forward at half speed | |
leftMotor(100); | |
rightMotor(100); | |
delay(1000); | |
// Spin in a circle | |
leftMotor(-150); | |
rightMotor(150); | |
delay(1000); | |
// Stop | |
leftMotor(0); | |
rightMotor(0); | |
delay(1000); | |
//*** END YOUR ROBOT CODE *** | |
} | |
} | |
// Functions | |
void rightMotor(int motorSpeed) { | |
if (motorSpeed > 0) { //if the motor should drive forward (positive speed) | |
digitalWrite(AIN1, HIGH); //set pin 1 to high | |
digitalWrite(AIN2, LOW); //set pin 2 to low | |
} else if (motorSpeed < 0) { //if the motor should drive backward (negative speed) | |
digitalWrite(AIN1, LOW); //set pin 1 to low | |
digitalWrite(AIN2, HIGH); //set pin 2 to high | |
} else { //if the motor should stop | |
digitalWrite(AIN1, LOW); //set pin 1 to low | |
digitalWrite(AIN2, LOW); //set pin 2 to low | |
} | |
analogWrite(PWMA, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed | |
} | |
void leftMotor(int motorSpeed) { //function for driving the left motor | |
if (motorSpeed > 0) { //if the motor should drive forward (positive speed) | |
digitalWrite(BIN1, HIGH); //set pin 1 to high | |
digitalWrite(BIN2, LOW); //set pin 2 to low | |
} else if (motorSpeed < 0) { //if the motor should drive backward (negative speed) | |
digitalWrite(BIN1, LOW); //set pin 1 to low | |
digitalWrite(BIN2, HIGH); //set pin 2 to high | |
} else { //if the motor should stop | |
digitalWrite(BIN1, LOW); //set pin 1 to low | |
digitalWrite(BIN2, LOW); //set pin 2 to low | |
} | |
analogWrite(PWMB, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment