Skip to content

Instantly share code, notes, and snippets.

@mandy1243
Created April 11, 2018 20:37
Show Gist options
  • Save mandy1243/f1cde4628e0ac580b617a821cc73c41b to your computer and use it in GitHub Desktop.
Save mandy1243/f1cde4628e0ac580b617a821cc73c41b to your computer and use it in GitHub Desktop.
const int switchPin = 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(switchPin, INPUT_PULLUP);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
analogWrite(enablePin, 100);
Serial.begin(9600);
}
void loop() {
Serial.println(digitalRead(switchPin));
int switchVal = digitalRead(switchPin);
// if the switch is high, motor will turn on one direction
if(switchVal == HIGH){
// set leg 1 of the H-bridge high
digitalWrite(motor1Pin, HIGH);
// set leg 2 of the H-bridge low
digitalWrite(motor2Pin, LOW);
}
// else (which means the switch is low), motor will turn in the other direction
else if(switchVal == LOW){
// set leg 1 of the H-bridge high
digitalWrite(motor1Pin, LOW);
// set leg 2 of the H-bridge low
digitalWrite(motor2Pin, HIGH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment