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
}
}
}