I’m trying to do my first script.. ever. So been googleing alot.Trying to learn. So here we go.
I’m trying to do 2 stepper motors to do different stuff with one button. Short and long Presses.
I have got it going, but there is 3 buggs i can’t solve. So hope someone can help me solve the puzzle.
How i’t works. When i power it on i’t will index First motor. I’t will go slowly until it retcess the Switch.
And then it will change direction even slower untill the switch is UnSwitched. It’s now index. The secound
stepper will do the same thing.
Now when i short press the button the both stepper will go to there pos. (200 and 400). If i short press
again then both stepper will get back to zero.
If i long press the both will move one to zero and the other to (600). In this state the stepper that has
stopped at 600 will stay there. And now i can short press to change the position of the other stepper back
and fort (0-200). But here is the bugg.
The Bugg: When i go from state one to state to state “(600)” i need some code that can feel if i do a short or
a long press. Becouse both stepper starts to move when i press the button. Even if i do a short or a long press. This problem are at LINE 190 there should also be the samt at LINE 218. So the stepper woun’t move untill it know if it was a short or a long press.
Bugg 2: If i do a long press to get to state 2 “(600)” and the stepper haven’t reaced the destination before
i press short again. The stepper motor will stop until i short press again. Here i just whant it to continue
until it reaches the destination. Or if i do a long press again. This is at LINE 207.
Hope someone can help me. And remember this is my firstever code so it is what it is.
The code:
#include "AccelStepper.h"
#define home_switch1 8 // Pin 8 connected to Home Switch (MicroSwitch)
#define home_switch2 7
#define LEDPin 13
int buttonPin = 6;
int state = LOW;
int state2 = LOW;
int btnState = 0;
int btn;
int motor1;
int motor2;
int btnStatePrevious = LOW;
bool btnStateLongPress = false;
unsigned long btnLongPressMillis;
unsigned long previousBtnMillis;
unsigned long btnPressDuration;
unsigned long currentMillis;
unsigned long time = millis();
unsigned long minBtnLongPressDuration = 3000;
const int intervalBtn = 50;
#define HOMEING 100 //Speed
#define HOMEING_RETURN 20 //Return Speed
#define HOMEING_ACCELERATION 200
#define STEPS1 400 // Right Joystick pos 1
#define STEPS2 200 // Left Joystick pos 1
#define STEPS3 600 // Right Joystick pos 2
#define STEPS4 0 // Left Joystick pos 2
#define MAXSPEED 1000
#define ACCELERATION 800
#define MICROSTEP1 8 * STEPS1 // Full, Halv, 1/4, 1/8, 1/16
#define MICROSTEP2 8 * STEPS2
#define MICROSTEP3 8 * STEPS3
#define MICROSTEP4 8 * STEPS4
AccelStepper stepper1(1, 5, 2); // 1 = Easy Driver, 5 Step Pin, 2 Dir Pin.
AccelStepper stepper2(1, 4, 3);
long initial_homing1 = -1; // Used to Home Stepper at startup
long initial_homing2 = -1;
void setup() {
Serial.begin(9600);
pinMode(home_switch1, INPUT);
pinMode(home_switch2, INPUT);
pinMode(buttonPin, INPUT);
pinMode(LEDPin, OUTPUT);
delay(5); // Wait for EasyDriver wake up
stepper1.setMaxSpeed(HOMEING); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper1.setAcceleration(HOMEING_ACCELERATION); // Set Acceleration of Stepper
stepper2.setMaxSpeed(HOMEING);
stepper2.setAcceleration(HOMEING_ACCELERATION);
// Start Homing procedure of Stepper Motor at startup
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (digitalRead(home_switch1)) { // Make the Stepper move CCW until the switch is activated
stepper1.moveTo(initial_homing1); // Set the position to move to
initial_homing1--; // Decrease by 1 for next move if needed
stepper1.run(); // Start moving the stepper
delay(5);
}
stepper1.setCurrentPosition(0); // Set the current position as zero for now
stepper1.setMaxSpeed(HOMEING_RETURN); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper1.setAcceleration(HOMEING_ACCELERATION); // Set Acceleration of Stepper
initial_homing1 = 1;
while (!digitalRead(home_switch1)) { // Make the Stepper move CW until the switch is deactivated
stepper1.moveTo(initial_homing1);
initial_homing1++;
stepper1.run();
delay(5);
}
stepper1.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
stepper1.setMaxSpeed(MAXSPEED); // Set Max Speed of Stepper (Faster for regular movements)
stepper1.setAcceleration(ACCELERATION); // Set Acceleration of Stepper
while (digitalRead(home_switch2)) { // Make the Stepper move CCW until the switch is activated
stepper2.moveTo(initial_homing2); // Set the position to move to
initial_homing2--; // Decrease by 1 for next move if needed
stepper2.run(); // Start moving the stepper
delay(5);
}
stepper2.setCurrentPosition(0); // Set the current position as zero for now
stepper2.setMaxSpeed(HOMEING_RETURN); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper2.setAcceleration(HOMEING_ACCELERATION); // Set Acceleration of Stepper
initial_homing2 = 1;
while (!digitalRead(home_switch2)) { // Make the Stepper move CW until the switch is deactivated
stepper2.moveTo(initial_homing2);
stepper2.run();
initial_homing2++;
delay(5);
}
stepper2.setCurrentPosition(0);
stepper2.setMaxSpeed(MAXSPEED);// Set Max Speed of Stepper (Faster for regular movements)
stepper2.setAcceleration(ACCELERATION);// Set Acceleration of Stepper
}
void readBtnState()
{
if (currentMillis - previousBtnMillis > intervalBtn)
{
int btn = digitalRead(buttonPin);
{
if (btn == HIGH && btnStatePrevious == LOW && !btnStateLongPress)
{
btnLongPressMillis = currentMillis;
btnStatePrevious = HIGH;
Serial.println("Button pressed");
if (state == HIGH)
{
motor1 = 0;
state = LOW;
}
else
{
motor1 = 1;
state = HIGH;
}
}
digitalWrite(LEDPin, state);
}
btnPressDuration = currentMillis - btnLongPressMillis;
if (btn == HIGH && !btnStateLongPress && btnPressDuration >= minBtnLongPressDuration)
{
btnStateLongPress = true;
if (state2 == HIGH)
{
motor2 = 0;
state2 = LOW;
}
else
{
motor2 = 1;
state2 = HIGH;
}
Serial.println("Button long pressed");
}
if (btn == LOW && btnStatePrevious == HIGH)
{
btnStatePrevious = LOW;
btnStateLongPress = false;
Serial.println("Button released");
if (btnPressDuration < minBtnLongPressDuration)
{
Serial.println("Button pressed shortly");
}
}
previousBtnMillis = currentMillis;
}
}
void loop()
{
currentMillis = millis();
readBtnState();
if (motor1 == 0 && motor2 == 0)
{
stepper1.moveTo(0);
stepper2.moveTo(0);
stepper1.run();
stepper2.run();
}
if (motor1 == 1 && motor2 == 0)
{
stepper2.moveTo(MICROSTEP2); // Left stepper to 200
stepper2.run();
if (motor1 == 1 && motor2 == 0)
{
stepper1.moveTo(MICROSTEP1); //Right stepper to 400
stepper1.run();
}
}
else if (motor1 == 1 && motor2 == 1)
{
stepper1.moveTo(MICROSTEP3); //Right stepper to 600
stepper1.run();
if (motor1 == 1 && motor2 == 1) // Left stepper to home position
{
stepper2.moveTo(MICROSTEP4);
stepper2.run();
}
}
else if (motor1 == 0 && motor2 == 1) // Left stepper to 200
{
stepper2.moveTo(MICROSTEP2);
stepper2.run();
if (motor1 == 1 && motor2 == 1) //Left stepper to home position
{
stepper2.moveTo(MICROSTEP4);
stepper2.run();
}
}
}
Well i think i have explanie the best i could. Codeing is hard!
link to the simulator where i have been tinkering.
https://wokwi.com/projects/398318638460529665
Peter Höjerslev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.