Need help with my code. Can't figure it out

Hi

I'm trying to do my first code.. 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 1 to state 2 "(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.
Also this is the simulator i'm tinker in.

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();
    }
  }
}

seems like you tried doing a lot. even experienced people start simple and add features after they get each new feature working

  • buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW. a button press can be recognized by detecting a change in state and becoming LOW and may need to be debounced (e.g. delay (20))

  • don't understand why you would do something until the "switch is UnSwitched". when "homing", the motor is typically advanced one step at a time until the switch input pin goes LOW when closed. Reversing one setp should open the switch

  • looks like your readBtnState() does more that read a switch. looks like each press toggles the state of motor and motor2.

  • a button can be debounced by simply delaying for 20 msec after recognizing a state change

  • recognizing short long presses can be done by capturing a timestamp when a button is pressed and recognizing that it is released before some long-period as a short press and recognizing that the long-period has expired as a long press.

haved looks at the state machine in loop(). i think you need to sort out the issues with detecting button presses which are the stimuli to your state maching

1 Like
#include "AccelStepper.h"

#define  home_switch1  8 // Pin 8 connected to Home Switch (MicroSwitch)
#define  home_switch2  7
#define  LEDPin  13
#define  buttonPin 6

#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);

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 . . . . . . . . . . . ");

  long initial_homing1 = -1; // Used to Home Stepper at startup
  long initial_homing2 = -1;

  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);
  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
  Serial.println("Homing Completed");
  Serial.println();
}

void loop() {
  static bool motor1 = false ;
  static bool motor2 = false ;
  static bool btnStatePrevious = false ;
  static unsigned long previousBtnMillis = 0 ;
  static unsigned long btnPressDuration = 0 ;
  static unsigned long minBtnLongPressDuration = 3000;
  static unsigned int intervalBtn = 50;

  unsigned long currentMillis = millis();
  bool btn = digitalRead(buttonPin);

  if (btn ) {
    if (!btnStatePrevious)    {
      delay(intervalBtn);
      btnStatePrevious = HIGH;
      previousBtnMillis = currentMillis;
      Serial.println("Button pressed");
    }
  }
  else {
    if ( btnStatePrevious)    {
      btnStatePrevious = LOW;
      Serial.println("Button released");
      Serial.println(currentMillis - previousBtnMillis);
      if ( currentMillis - previousBtnMillis >= minBtnLongPressDuration)      {
        motor2 = !motor2;
        Serial.println("Button long pressed");
      }
      else      {
        motor1 = !motor1;
        Serial.println("Button pressed shortly");
      }
    }
  }
  digitalWrite(LEDPin, motor1);

  if (  motor2)  {
    stepper1.moveTo(MICROSTEP3); //Right stepper to 600
    stepper1.run();
    if (motor1) { // Left stepper to home position
      stepper2.moveTo(MICROSTEP4);
      stepper2.run();
    }
    else   {  // Left stepper to 200
      stepper2.moveTo(MICROSTEP2);
      stepper2.run();
    }
  }
  else  {
    if (motor1 )    {
      stepper2.moveTo(MICROSTEP2); // Left stepper to 200
      stepper2.run();
      stepper1.moveTo(MICROSTEP1); //Right stepper to 400
      stepper1.run();
    }
    else    {
      stepper1.moveTo(0);
      stepper2.moveTo(0);
      stepper1.run();
      stepper2.run();
    }
  }
}
1 Like

Thank you so much. This is exacly how i wanted it to work! I'm super thankful! :grinning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.