// Stepper turn right and left
// For: https://forum.arduino.cc/t/stepper-w-2-buttons-pushed-and-potentiometer-got-stuck/1166311
// This Wokwi project: https://wokwi.com/projects/375260835477182465
// Not working code.
// I don't know what to do with the code.
// Defin pins
int button_left = 2; // Push button for CW
int button_right = 3; // Push button for CCW
int driverPUL = 7; // PUL- pin
int driverDIR = 6; // DIR- pin
int spd = A0; // Potentiometer
// Variables
int pd = 500; // Pulse Delay period
boolean setdir = HIGH; // Set Direction
// Interrupt Handler
void revmotor()
{
setdir = !setdir;
}
void setup()
{
Serial.begin(9600);
pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
pinMode (button_left, INPUT);
attachInterrupt(digitalPinToInterrupt(button_left), revmotor, FALLING);
}
void loop()
{
int pressed = digitalRead (button_left);
if (pressed == HIGH)
{
pd = map((analogRead(spd)), 0, 1023, 2000, 50);
Serial.println("Button left");
digitalWrite(driverDIR, LOW);
digitalWrite(driverPUL, LOW);
delayMicroseconds(pd);
digitalWrite(driverDIR, HIGH);
digitalWrite(driverPUL, HIGH);
delayMicroseconds(pd);
}
}