New to this forum, so apologies if this is a daft question.
I'm building a penny waterfall/coin pusher toy and using an Arduino/servo motor to move the top shelf backwards and forwards. So far, so good but when power is first applied to the Arduino, I don't want the shelf to jump quickly to the first angle I've set in the loop.
For example, using the example "Servo Sweep" program:
if I switch off the machine and the servo is at 180 degrees, when it is next turned on it will jump back to 0 degrees at the maximum rotation speed of the servo. This could be quite a physical movement.
Is there a way to either determine the last position (i.e. is the last myservo.write() value retained across reboots) or is there some other way in which I could return the shelf to the "0" position slowly?
lb483:
As a follow-up, I presume I could include an EEPROM.write() call after the myservo.write() and then read that value at start-up?
The problem with that is that the EEPROM cell will wear out after about 100,000 writes and if you want to keep track of every move of the servo that might not take very long.
Do you have the option of an organised shut-down so that you can get the servo to go to ZERO smoothly just before the Arduino is powered off?
For my servos I get them the move to the halfway position at startup - it just minimises the initial move regardless of where the servo was when it stopped.
Off the top of my head, how about using a feedback servo? When you power up, read its position, and travel to the required start position in an orderly fashion.
Normally to set a specific position on startup you do a myservo.write() in startup() BEFORE the myservo.attach().
If you use VarSpeedServo.h instead of Servo.h you won't need to use that Sweep technique because it has a speed parameter in the write() command. What I don't know (and can't at the moment test) is if that speed parameter will work for a write() before attach(). But it might be worth a try.
slipstick:
Normally to set a specific position on startup you do a myservo.write() in startup() BEFORE the myservo.attach().
Yes, but I think that still causes the same problem. Whether that initial position is specified as you suggest, or in the OP's for(), it will still fly there (wherever "there" may be) from the arbitrary position at which it was before power off.
The issue's not about setting the initial position, but about knowing the last position. (If I read the OP right, anyway.)
I was offering a possible answer for the OPs question "is there some other way in which I could return the shelf to the "0" position slowly?" That's why included the second paragraph that you apparently ignored.
This is the classic problem with servos and with what you currently have there isn't a good solution. You could add a button to press to send the servo to a known startup position which you must press before turning it off, but a power outage or someone yanking the power will sabotage that.
You could add a supercap or the like and monitor the mains power and home it when you see that power is gone.
As mentioned, you can replace your servo with one that can report its position - they're generally more expensive although I note that that Adafruit one isn't excessively priced.
You could use a stepper instead of the servo. You will need a limit switch to home it at startup.
But, as you see from all this, you need more or different hardware. A foolproof way to avoid a startup jerk on occasion with what you have doesn't exist as far as I can see.
leandra_reis:
Off the top of my head, how about using a feedback servo? When you power up, read its position, and travel to the required start position in an orderly fashion.
Thanks for this suggestion. It's the way that I went and it solves my problem perfectly.