I have a panel with 25 servos ( 15 on one control board + 10 on the other board) and
Arduino Uno. The panel has a 220v PIR motion sensor that powers it up for 5 minutes when someone approches it.
The sketch has 10 different tasks which make the servos move in different patterns.
For example, task1 sweeps all, task2 moves servos 1-10 in one direction and 11-25 in other direction...
etc..etc.
How do I generate a random number ( or "one number out of the hat" ) by which the panel would
perform that numbered task on power up ?
Most likely, on power up, it will continue an unfinished task if the power was shut down before it was finished. Is there a way to avoid that ?
If you are starting the arduino from a power off condition, as opposed to waking from sleep, you will need to use the randomseed() command otherwise the random number sequence will always be identical.
ba47:
How do I kill an unfinished task on power up and start anew?
Current program state is lost on power up, so it will do this with no special action. Making it remember the task and restart it where it left off would require you record the relevant aspects of program state in the EEPROM.
UKHeliBob, I have a 220v PIR detector that shuts off the whole panel (takes away current).
DrAzzy, I do not want to restart the unfinished task. In fact, starting where it left off is my problem.
What I need is to kill the unfinished task on power up then start with my function that sets all servos to 0 position. After that, I can generate a random number and jump to a random task indicated by that number.
The fact that it restarts an unfinished task, from where it left it, on power up gives me big a problem with
servo positions.
12Stepper lit up a led in my "hollow" head. He said it doesn't restart an unfinished task on power up.
So, I added code in the setup to make all servos go to 0 position, no matter where they
were when power went off. That solved the problem of having servos acting crazy
when a new task starts. But, somehow (?) they don't go to 0 position, they go to half position.
Strange, because I'm using the code I used before to set them to 0.
As soon as I get this issue straighten out, I can start with your valuable suggestions.
Thanks a million for the code Blackfin, you made my day.
ba47:
But, somehow (?) they don't go to 0 position, they go to half position.
Strange, because I'm using the code I used before to set them to 0.
By default a servo is attach()-ed at 90 (the middle), then goes where you tell it. So do your write(0) befoe the attach(), then they will attach() at 0.
I'm using the code... we can't see that though ;). Rule of thumb, whenever you talk code, include it for all to see.
What was wrong with econjack's code I linked 2 days ago?/i] Nothing. I haven't tried it yet. As you might have guessed I'm a newbie still trying to figure out how to restart from power up. Here's the code I'm using just to try things out but it's not working. i.e. all 25 servos sweep then the timer shuts down the power. After that, PIR senses approach, power up ... Now the trouble starts. i.e. the 10 servos connected to control board 1 (PWM0X40) just shake. ```
*#include <Wire.h> #include <Adafruit_PWMServoDriver.h>
ba47:
still trying to figure out how to restart from power up.
Not sure what that means tbh. If you mean (and so far it seems you don't) that you want to carry on where you left off, you would need (as suggested) to do some saving at regular intervals, and even maybe provide battery power so you always do an orderly shutdown to a known state. Then your restart would expect that.
That's what a "RE-start from power up" would mean to me; otherwise it's just a "start"
Or, attach() the servos at known positions, so at least once setup() is finished you know where they are. BUT if they just randomly stopped somewhere when the power goes off, they will still move to the position at which you attach them.
If you need to know where they actually are at power up, so you can handle the fact that those points could be pretty much anywhere, then you need servos with a 4th wire.
Thanks Stepper for bearing with me, I'll try to explain myself better.
I DO NOT want to carry on where I left.
Using two PCA9685 16 channel control boards, I've never seen " Attach Servos "(?)
When power shut down occurs by the timer, the servos are indeed left somewhere unknown.
So, on power up, before I "start " a new task I need all servos to go to 0 position. I tried that with the
code I posted but no luck.
I've already built this panel as a gift to an institute for kids. It will be displayed in a hallway.
What I wanted was when a kid passes by, it lights up (led strips) and performs a random movement
of the servos, not always the same routine.
I'm stuck on power up and "start" as you call it. If I can get the servos to 0 position on power up then
I think I can manage the rest.