how to restart and ardiuno but get how much steps moved last run?

Hi…
I just bought a stepper motor and arduino and I want to build a smart curtain system … I want to put the motor at the shaft of the curtain so it can open and close depend on the light sensor read… if it’s sunny it will open and if it’s dark at night it will close sine no need to be opened for example…

I create the algorithm that first of all see how much steps will need to fully close the the curtain then fully open it and start the ardiuno then mapping the light intensity to the amount of steps from zero to steps that make it fully close and save the steps that moved into variable… next read from light sensor I will also map it and then I will subtract it from previous value if it’s + then Ill move backward with the amount of subtraction. if it’s negative the same but forward… if it’s zero it will still no move… this algorithm got a big problem :sweat_smile: when I will restart the ardiuno the curtain will not be fully opened or closed and it will take the initial value as a zero and when it mapped to the maximum amount of light it should fully open “in case it started fully closed” but that will not happened which will make it reach fully opened and still moving until the motor burn out of curtain resistance of move >>>> any solution guys?

You can store information in the EEPROM memory built into most Arduino boards that survives the arduino reboot but this EEPROM memory has a relatively low number of write cycles before it will fail so you need to only write to it when necessary.

This is not an easy problem to solve.

You could save the current position to the EEPROM non-volatile memory after each move, but there is a limit to how many times you can write to the EEPROM.

If you can be sure of an orderly shutdown you could have a shutdown routine that saves the value to the EEPROM before shutdown - this would greatly reduce the number of EEPROM writes. But it would be useless for an unexpected shutdown.

You could add a shield that can take an SD card and write the value to that after every move, but that approach adds expense.

The simplest solution would seem to be to leave the Arduino running all the time and if it occasionally resets have a startup routine that moves the curtain to fully open as identified by a limit switch. That is the usual way to reset the zero point for stepper motors.

...R

Robin2: This is not an easy problem to solve.

You could save the current position to the EEPROM non-volatile memory after each move, but there is a limit to how many times you can write to the EEPROM.

If you can be sure of an orderly shutdown you could have a shutdown routine that saves the value to the EEPROM before shutdown - this would greatly reduce the number of EEPROM writes. But it would be useless for an unexpected shutdown.

You could add a shield that can take an SD card and write the value to that after every move, but that approach adds expense.

The simplest solution would seem to be to leave the Arduino running all the time and if it occasionally resets have a startup routine that moves the curtain to fully open as identified by a limit switch. That is the usual way to reset the zero point for stepper motors.

...R

nice but "have a startup routine that moves the curtain to fully open as identified by a limit switch." can you explain that sentence more ... didn't get it!

The idea of a limit switch is that you have something (a microswitch, perhaps) which is pressed by the curtain slider when the curtain reaches the fully open position. The Arduino moves the stepper motor one step at a time and checks between each step whether the switch has been pressed. When it detects the switch it knows that that is the "zero" point for curtain movement and all subsequent movements can be counted from there.

Hope that makes sense.

...R