Skip to content

Instantly share code, notes, and snippets.

@kmicheli
Last active April 12, 2018 04:34
Show Gist options
  • Save kmicheli/0807a3eadfa8fd41a5ff5161ccf90d42 to your computer and use it in GitHub Desktop.
Save kmicheli/0807a3eadfa8fd41a5ff5161ccf90d42 to your computer and use it in GitHub Desktop.
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