Last active
February 25, 2021 21:03
-
-
Save shfitz/8365207a15d254bc8e64c7aa07ae6b65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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