-
-
Save Suleman-Elahi/160315129421f2efddc5282da72be427 to your computer and use it in GitHub Desktop.
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
const int button = 2; // switch input | |
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A) | |
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A) | |
const int enablePin = 9; // H-bridge enable pin | |
void setup() { | |
// set the switch as an input | |
pinMode(button, INPUT); | |
// set all the other pins as outputs | |
pinMode(motor1Pin, OUTPUT); | |
pinMode(motor2Pin, OUTPUT); | |
pinMode(enablePin, OUTPUT); | |
//make it spin | |
digitalWrite(enablePin, 100); | |
Serial.begin(9600); | |
} | |
void loop() { | |
//reads the switch | |
int buttonState = digitalRead(button); | |
// if the switch is high, motor will turn on one direction | |
if(buttonState == HIGH){ | |
// set leg 1 of the H-bridge low | |
digitalWrite(motor1Pin, LOW); | |
// set leg 2 of the H-bridge high | |
digitalWrite(motor2Pin, HIGH); | |
} | |
// if the switch is low, motor will turn in the other direction | |
else if(buttonState == LOW){ | |
// set leg 1 of the H-bridge high | |
digitalWrite(motor1Pin, HIGH); | |
// set leg 2 of the H-bridge low | |
digitalWrite(motor2Pin, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment