Skip to content

Instantly share code, notes, and snippets.

@shfitz
Last active February 25, 2021 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shfitz/8365207a15d254bc8e64c7aa07ae6b65 to your computer and use it in GitHub Desktop.
Save shfitz/8365207a15d254bc8e64c7aa07ae6b65 to your computer and use it in GitHub Desktop.
int enablePin= 5;
int ctrl1 = 3;
int ctrl2 = 2;
int button = 4;
int rotDirection = 1;
int buttState = false;
int preState = false;
void setup() {
pinMode(enablePin, OUTPUT);
pinMode(ctrl1, OUTPUT);
pinMode(ctrl2, OUTPUT);
pinMode(button, INPUT);
// Set initial rotation direction
digitalWrite(ctrl1, LOW);
digitalWrite(ctrl2, HIGH);
}
void loop() {
int potVal = analogRead(A0); // Read sensor value
analogWrite(enablePin, potVal/4); // Send PWM signal to H-Bridge
buttState = digitalRead(button);
// check if button has changed state
if (buttState == true && preState == false) {
rotDirection *= -1;
}
if (rotDirection == 1) {
digitalWrite(ctrl1, HIGH);
digitalWrite(ctrl2, LOW);
delay(20);
}
if (rotDirection == -1) {
digitalWrite(ctrl1, LOW);
digitalWrite(ctrl2, HIGH);
delay(20);
}
preState = buttState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment