Skip to content

Instantly share code, notes, and snippets.

@LucasDachman
Created November 15, 2018 22:52
Show Gist options
  • Save LucasDachman/4056944027cf1d621a912cc7a7829b95 to your computer and use it in GitHub Desktop.
Save LucasDachman/4056944027cf1d621a912cc7a7829b95 to your computer and use it in GitHub Desktop.
/*
* Lucas Dachman
* Nov 15 2018
*/
const int SWITCH = 10;
const int ON = 0;
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
//In the setup(), set all the pins for the H-bridge as outputs, and the pin for the switch as an input. Then set the enable pin high so the H-bridge can turn the motor on.
void setup() {
// set the switch as an input:
Serial.begin(9600);
pinMode(SWITCH, INPUT_PULLUP);
// set all the other pins you're using as outputs:
pinMode(3, OUTPUT);
pinMode(9, OUTPUT);
pinMode(4, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
}
//In the main loop() read the switch. If it’s high, turn the motor one way by taking one H-bridge pin high and the other low. If the switch is low, reverse the direction by reversing the states of the two H-bridge pins.
void loop() {
// if the switch is high, motor will turn on one direction
if (digitalRead(SWITCH) == ON) {
Serial.println("ON");
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
} else {
Serial.println("OFF");
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment