Anyone have a basic example on how I would save a stepper position in the EEPROM? This would need to be saved upon power down and retrieved later on next power up, after re-homing. Would I set up a int position variable? And would I use something like this:
if (digitalWrite(saveposition1button) == LOW {
EEPROM.put(0,nowPos);
}
and
if (digitalWrite(recallposition1button) == LOW {
EEPROM.get(0,nowPos);
}
Here's my code
// Define the Pins used
#define step_pin 2 // Pin 2 connected to Step pin
#define dir_pin 3 // Pin 3 connected to Direction pin
#define home_switch 9 // Pin 9 connected to Home Switch (MicroSwitch)
#define cw_button 7 // Pin 7 connected to clockwise button
#define ccw_button 8 // Pin 8 connected to counter clockwise button
#define home_button 10 // Pin 10 connected to home button (press to send the stepper home)
#define led_end 11 // Pin 11 connected to LED for end of travel reached
int direction; // Variable to set Rotation (CW-CCW) of the motor
int steps; // Used to set HOME position after Homing is completed
void setup() {
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(home_switch, INPUT_PULLUP);
pinMode(cw_button, INPUT_PULLUP);
pinMode(ccw_button, INPUT_PULLUP);
pinMode(home_button, INPUT_PULLUP);
pinMode(led_end, OUTPUT);
}
void loop() {
// Start Homing procedure of Stepper Motor
if (digitalRead(home_button) == LOW) { // Start homing when this button is pressed
while (digitalRead(home_switch)) { // Do this until the switch is activated
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delay(3); // Delay to slow down speed of Stepper
digitalWrite(step_pin, LOW);
delay(3);
}
while (!digitalRead(home_switch)) { // Do this until the switch is not activated
digitalWrite(dir_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
}
// Enable movement of Stepper Motor using the CW and CCW buttons
while (digitalRead(cw_button) == LOW) {
if (steps > 0) { // To make sure the Stepper doesn't go beyond the Home Position
digitalWrite(dir_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
}
if (steps < 1000) {
digitalWrite(led_end, LOW); // Keep LED off if end of travel is not reached yet
}
}
while (digitalRead(ccw_button) == LOW) {
if (steps < 1000) { // Maximum steps the stepper can move away from the Home Position
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++; // Increase the number of steps taken
}
if (steps == 1000) {
digitalWrite(led_end, HIGH); // Turn on LED if end of travel is reached
}
}
}