Centering / Homing Stepper Motor in case of power failure

Please guide me on below prorgam. I want to home the stepper when ever motor starts, be it normal start or after abrupt power failure.
I am using EEPROM to define Zero position initially and then reading predefined position to center arm at start.
I am not getting desired results. Please guide.

#include <AccelStepper.h>
#include <EEPROM.h>

//RF24 radio(9, 10); // CE, CSN

int x , i , ang , arm_sp;
int EncA = 4; //EA+ from encoder
int EncB = 5;  //EB+ from encoder
const int AC = 8; // button
int Zero_Pos, Current_Pos;
int buttonstate = HIGH;

AccelStepper stepper(1, 7, 6);

void setup()
{
//  EEPROM.write(0, 0);
  Serial.begin(9600);
  Zero_Pos = EEPROM.read(0);
//  Serial.println("Zero_Pos");
//  Serial.print(": ");
//  Serial.print(Zero_Pos);

  pinMode(EncA, INPUT);
  pinMode(EncB, INPUT);
  pinMode(AC, INPUT);
  Current_Pos = ((digitalRead(EncB)));
//  Serial.println("Current_Pos");
//  Serial.print(": ");
//  Serial.print(Current_Pos);
  
  if (Zero_Pos != Current_Pos) {
    stepper.setMaxSpeed(1000);
    int accelaration = 250;
    stepper.setSpeed(500);
    stepper.setAcceleration(accelaration); // Set Acceleration
    stepper.moveTo(Zero_Pos);
    stepper.runToPosition();
  }
  return;
}

void loop()
{
  arm();
}

void arm()
{
  buttonstate = digitalRead(AC);
  if ( buttonstate == LOW)
  {
    int ang = 50;
    int arm_sp = 500;

    //      ang = map( ang , 0 , 860 , 160 , 320 ); // mapping angle with Pot value. Pot max value is 860.

    stepper.setMaxSpeed(arm_sp); // change the speed by pot setting.
    stepper.setAcceleration(1.10 * arm_sp); // Set Acceleration

    ang = ang * 20; // to include gear ratio
    stepper.moveTo(ang / 2); // Run to target position with set speed and acceleration/deceleration:
    stepper.runToPosition();
    delay(100);

    stepper.moveTo(-ang); // Reverse direction
    stepper.runToPosition();
    delay(100);

    stepper.moveTo(ang / 2); // Reverse direction
    stepper.runToPosition();
    delay(100);
//    Serial.println(buttonstate);
//    Serial.println(ang);
    Serial.println("In Loop");
  }

  else
    Serial.println("Not in loop");
//  exit;
}

You need to post your schematic. You're reading a single input but comparing it to position command. One bit will only get you a zero, not the actual position unless you seek to zero and count from there.

What exactly is the code supposed to do?
What is happening instead?

EEPROM.read() will return one byte. Your position variables are 2 byte int data type. To save an int you can use the put() function and to retrieve the int from EEPROM use the get() function. See the EEPROM library reference.

as far as i know, encoders allow incremental changes to position and direction of change. they don't provide absolution position. the code above will set "Current_Pos" to either 1/0

a microswitch can be used as a "limit" switch position a stepper motor at a known position at the start of, or possibly during operation. it would be located at the end of travel in one direction (clockwise, CCW).

at startup, the motor would be stepped one step at a time toward the limit switch until the limit switch becomes active and the relative position set to zero

For re-positioning a stepper-motor after a power-off
It is a must to have a switch of any kind that has a fixed position.

If power is switched off you can move the stepper-motor by hand for any distance
beeing 0,01 mm or to the maximum or minimum-position that is mechanical possible.

Your microcontroller-code does not "know" about this movement.
So it is impossible to "know" the new position.

Except you have a special kind of encoder which reports absolute position.

So the way to get your program knowing again at what position the stepper-motor / the mechanic is you need to to do a reference-drive

moving stepper-motor at reduced speed towards limit-switch until switch is triggered.
Then move aways from the switch very slowly until switch is untriggered.
Then set position to a fixed value of your choice.

After this procedure your code "knows" where the mechanic is.

best regards Stefan

Thanks All for your valuable feedback

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