Converting X axis of joystick to back&forth buttons

Hi,

I found this great example of stepper motor control with 'homing' function for extra safety.

I'm trying to convert it for my own project but with different hardware, I'd like to use 2 buttons instead of a joystick for moving the stepper motor back&forth a single axis. The homing function works but communicating with the buttons doesn't work and I can't seem to understand why.

This is my attempt so far, could you give me some pointers on how to proceed with this code?

#define RPMS                250.0
#define STEP_PIN                9
#define DIRECTION_PIN           8
#define CW_PIN                  3
#define CCW_PIN                 2

#define STEPS_PER_REV         200
#define MICROSTEPS_PER_STEP     8
#define MICROSECONDS_PER_MICROSTEP   (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 40))
#define home_switch 10 // microswitch voor home position

uint32_t LastStepTime = 0;
uint32_t CurrentTime = 0;
int steps;

void setup() {  
  Serial.begin(9600);              
  pinMode(STEP_PIN, OUTPUT);     
  pinMode(DIRECTION_PIN, OUTPUT);
  digitalWrite(STEP_PIN, LOW);
  digitalWrite(DIRECTION_PIN, LOW);
  pinMode(home_switch, INPUT_PULLUP);
  pinMode(CW_PIN,INPUT);    // button 1
  pinMode(CCW_PIN, INPUT);  // button 2
//  stepper.setAcceleration(100);

// Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(DIRECTION_PIN, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(STEP_PIN, HIGH);
    delay(5);                       // Delay to slow down speed of Stepper
    digitalWrite(STEP_PIN, LOW);
    delay(5);   
}

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(DIRECTION_PIN, LOW); 
    digitalWrite(STEP_PIN, HIGH);
    delay(10);                       // More delay to slow even more while moving away from switch
    digitalWrite(STEP_PIN, LOW);
    delay(10);
  }

  steps=0;  // Reset position variable to zero
  Serial.println(steps);
   
} ///// End of homing procedure which is executed before pressing buttons for control.. /////


void loop() {
//  if (digitalRead(CW_PIN) == LOW)
//  {
//    CurrentTime = micros();
//    if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
//    {
//      LastStepTime = CurrentTime;
//      digitalWrite(STEP_PIN, HIGH);
//      digitalWrite(DIRECTION_PIN, LOW);
//      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
//     // Serial.println(CurrentTime);
//      digitalWrite(STEP_PIN, LOW); 
//      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
//     // Serial.println(CurrentTime);
//    }
//  }
//
//  if (digitalRead(CCW_PIN) == LOW)
//  {
//    CurrentTime = micros();
//    if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
//    {
//      LastStepTime = CurrentTime;
//      digitalWrite(STEP_PIN, LOW);
//      digitalWrite(DIRECTION_PIN, HIGH);
//      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
//     // Serial.println(CurrentTime);
//      digitalWrite(STEP_PIN, HIGH); 
//      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
//    }
//  }
//}
  while (digitalRead(CW_PIN) >= 0 && digitalRead(CW_PIN) <= 100) {
    if (steps > 0) {  //  To make sure the Stepper doesn't go beyond the Home Position
      digitalWrite(DIRECTION_PIN, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(STEP_PIN, HIGH);
      delay(1);
      digitalWrite(STEP_PIN, LOW);
      delay(1);
      steps--;   // Decrease the number of steps taken
    }      
  }
   
    while (digitalRead(CCW_PIN) > 900 && digitalRead(CCW_PIN) <= 1024) {
      if (steps < 1000) {      // Maximum steps the stepper can move away from the Home Position
        digitalWrite(DIRECTION_PIN, LOW);
        digitalWrite(STEP_PIN, HIGH);
        delay(1);
         digitalWrite(STEP_PIN, LOW);
        delay(1);
        steps++;  // Increase the number of steps taken
      }
    }
}
  while (digitalRead(CW_PIN) >= 0 && digitalRead(CW_PIN) <= 100)
  while (digitalRead(CCW_PIN) > 900 && digitalRead(CCW_PIN) <= 1024)

digitalRead() will return either LOW or HIGH (0 or 1) so why the range testing ?

The original code used analogRead() to get the value of pots, which can be 0 - 1023. For buttons you are using digitalRead(), which is either HIGH or LOW. You should replace your while loops with if statements, checking whether the button reading is HIGH or LOW depending on how your project is wired.

I advise you to use print statements so you see what's going on. You can read the buttons like this

  byte cwVal = digitalRead(CW_PIN);
  byte ccwVal = digitalRead(CCW_PIN);

  Serial.print("CW = ");
  Serial.println(cwVal == HIGH ? "HIGH" : "LOW");

  Serial.print("CCW = ");
  Serial.println(ccwVal == HIGH ? "HIGH" : "LOW");

Then check the buttons like this

  if( cwVal == HIGH ) {

    if (steps > 0) {  //  To make sure the Stepper doesn't go beyond the Home Position
      digitalWrite(DIRECTION_PIN, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)

Correct, the pieces of code i'm trying to stitch together come from an analog sensor. I'm using an EasyDriver ic and nema17 stepper with a microswitch for 'homing' purposes. I'll give your code a go, thank you! @Blue Eyes

I've put your code in the void loop of my program and tried messing around a little but I think I have made a mistake somewhere since the serial only returns these messages (the code in this void loop now just flips the direction or step pin):

CCW = HIGH
CW = HIGH
CCW = HIGH
CW = HIGH
CCW = HIGH

#define RPMS                250.0
#define STEP_PIN                9
#define DIRECTION_PIN           8
#define CW_PIN                  3
#define CCW_PIN                 2
#define home_switch 10          // microswitch voor home position

byte cwVal = digitalRead(CW_PIN);
byte ccwVal = digitalRead(CCW_PIN);

//Serial.print("CW = ");
//Serial.println(cwVal == HIGH ? "HIGH" : "LOW");
//
//Serial.print("CCW = ");
//Serial.println(cwVal == HIGH ? "HIGH" : "LOW");

int steps;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);              
  pinMode(STEP_PIN, OUTPUT);     
  pinMode(DIRECTION_PIN, OUTPUT);
  digitalWrite(STEP_PIN, LOW);
  digitalWrite(DIRECTION_PIN, LOW);
  pinMode(home_switch, INPUT_PULLUP);
  pinMode(CW_PIN,INPUT);    // button 1
  pinMode(CCW_PIN, INPUT);  // button 2

  // Start Homing procedure of Stepper Motor at startup
  while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(DIRECTION_PIN, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(STEP_PIN, HIGH);
    delay(5);                       // Delay to slow down speed of Stepper
    digitalWrite(STEP_PIN, LOW);
    delay(5);   
}
  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(DIRECTION_PIN, LOW); 
    digitalWrite(STEP_PIN, HIGH);
    delay(10);                       // More delay to slow even more while moving away from switch
    digitalWrite(STEP_PIN, LOW);
    delay(10);
  }
  steps=0;  // Reset position variable to zero
  Serial.println(steps);
} ///// End of homing procedure which is executed before pressing buttons for control.. /////

void loop() {
  // put your main code here, to run repeatedly:
byte cwVal = digitalRead(CW_PIN);
  byte ccwVal = digitalRead(CCW_PIN);

  Serial.print("CW = ");
  Serial.println(cwVal == HIGH ? "HIGH" : "LOW");

  Serial.print("CCW = ");
  Serial.println(ccwVal == HIGH ? "HIGH" : "LOW"); 

    if( cwVal == HIGH ) {

    if (steps > 0) {  //  To make sure the Stepper doesn't go beyond the Home Position
      digitalWrite(DIRECTION_PIN, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(STEP_PIN, HIGH);
      delay(1);
      digitalWrite(STEP_PIN, LOW);
      delay(1);
      steps--;   // Decrease the number of steps taken
      Serial.println(steps);
    } 
    }
  pinMode(CW_PIN,INPUT);    // button 1
  pinMode(CCW_PIN, INPUT);  // button 2

How are your buttons wired?

I've wired the buttons the same as example 1.6

See the attached image