Skip to content

Instantly share code, notes, and snippets.

@jesu0404
Created March 23, 2017 21:26
Show Gist options
  • Save jesu0404/7bdaa592d9c5f798a7664479dc57b9ab to your computer and use it in GitHub Desktop.
Save jesu0404/7bdaa592d9c5f798a7664479dc57b9ab to your computer and use it in GitHub Desktop.
Uses a switch to turn a DC motor on and off
const int switchPin = 2;
const int motor1Pin = 3;
const int motor2Pin = 4;
const int enablePin = 9;
void setup() {
pinMode(switchPin, INPUT);
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
analogWrite(enablePin, 100);
}
void loop() {
int switchVal = digitalRead(switchPin);
if(switchVal == HIGH) {
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
}
else {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment