Created
May 17, 2019 03:27
-
-
Save e-Gizmo/b66fa3f44acc177fc40c3841492a8377 to your computer and use it in GitHub Desktop.
This is an example code for Hulkster Motor Driver.
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
#define pwm 9 | |
#define in1 6 | |
#define in2 7 | |
#define button 4 | |
int rotDirection = 0; | |
int pressed = false; | |
void setup() { | |
pinMode(enA, OUTPUT); | |
pinMode(in1, OUTPUT); | |
pinMode(in2, OUTPUT); | |
pinMode(button, INPUT); | |
// Set initial rotation direction | |
digitalWrite(in1, LOW); | |
digitalWrite(in2, HIGH); | |
} | |
void loop() { | |
int potValue = analogRead(A0); // Read potentiometer value | |
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255 | |
analogWrite(pwm, pwmOutput); // Send PWM signal | |
// Read button - Debounce | |
if (digitalRead(button) == true) { | |
pressed = !pressed; | |
} | |
while (digitalRead(button) == true); | |
delay(20); | |
// If button is pressed - change rotation direction | |
if (pressed == true & rotDirection == 0) { | |
digitalWrite(in1, HIGH); | |
digitalWrite(in2, LOW); | |
rotDirection = 1; | |
delay(20); | |
} | |
// If button is pressed - change rotation direction | |
if (pressed == false & rotDirection == 1) { | |
digitalWrite(in1, LOW); | |
digitalWrite(in2, HIGH); | |
rotDirection = 0; | |
delay(20); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment