Hi there
I use a stepper motor with a potentiometer for speed and a push button for direction.
the problem i have because it is a mechanical switch it switch the direction a few times if i press the button one can someone please help.
see code below
// Defin pins
int reverseSwitch = 2; // Push button for reverse
int driverPUL = 7; // PUL- pin
int driverDIR = 6; // DIR- pin
int spd = A0; // Potentiometer
// Variables
int pd = 500; // Pulse Delay period
boolean setdir = LOW; // Set Direction
// Interrupt Handler
void revmotor (){
setdir = !setdir;
}
void setup() {
pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
}
void loop() {
pd = map((analogRead(spd)),0,1023,2000,50);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
Hi There
Thank you for the information, but I am new with programming and I am not sure how to incorporate the code you send into my code.
Can you please help?
Regards
Johannes
Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.
Please no Fritzy diagrams.
In addition to the software debounce shown by @gcjr there is hardware debounce. Hardware debounce is a 0.1uF (or so) ceramic capacitor placed across the switch.
The best way to wire a switch with modern microcontrollers is from ground to an input that is set to pinMode INPUT_PULLUP.