Hello everyone , merry christmas and happy new year.
I'm working on a very little project with little knowledge and now I'm stuck.
I want to control a stepper motor within two limit switches and get start command by a button. I've found a example code from here (Using BIG Stepper Motors with Arduino | DroneBot Workshop) and do the wiring and changed the code a bit.
Here is the code I'm using
/*
Stepper Motor Test
stepper-test01.ino
Uses MA860H or similar Stepper Driver Unit
Has speed control & reverse switch
DroneBot Workshop 2019
https://dronebotworkshop.com
*/
// Defin pins
int reverseSwitch = 2; // Limitswitch Home
int reverseSwitch2 = 3; // Limitswitch END
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() {
if (digitalRead(reverseSwitch) == LOW && digitalRead(reverseSwitch2) == HIGH)
{
action1();
}
if (digitalRead(reverseSwitch) == HIGH && digitalRead(reverseSwitch2) == LOW)
{
digitalWrite(reverseSwitch, HIGH);
action2();
}
else
{
digitalWrite(driverPUL, LOW); // stop the motor
}
}
void action1() {
pd = map((analogRead(spd)),0,1023,2000,50);
digitalWrite(driverDIR,HIGH);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
void action2() {
digitalWrite(driverDIR,LOW);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(500);
digitalWrite(driverPUL,LOW);
delayMicroseconds(500);
}
The code works but not the way I want. When I press one switch the motor rotates one way while I keep pressing. If I let go the switch It stops.
I want my code to work like this
-Press start button
-Check limit switch, if machine at the end switch, move to home switch at a constant speed and stop when hit at home switch. Wait for button press to start again
-If machine at home switch, move to end switch at speed from potentiometer and stop when hit at home switch. Wait for button press to start again
Thanks for all the help in advance.