Hi everyone , I am a complete newbie to Ardouino, I have a project for which I think this would be a great platform.
I would like to fire 4 relays randomly.
1 start button is pressed.
2 after 10 second delay a random relay is activated
3 this relay would remain activated for 10 seconds.
then deactivate.
4 unit waits for start button again.
what hardware would I need to buy and how would my code look.
thanks for any help.
Make sure your relay coils are rated at 5V and 40mA or less, or else you'll need a transistor, MOSFET, or other relay driver. The relay contacts need to be rated for whatever you are driving.
I assume you've found the delay() function? (10 seconds is 10,000 milliseconds.)
Start with the Blink LED example. Attach 4 LEDs to 4 output pins and modify the Blink LED example to choose an LED randomly.
Note that the upper number in the random() function is exclusive. That means if you want to choose between I/O pins 0, 1, 2, or 3, you'd use:
LEDpin = random(0, 4); // Pin 4 is never chosen
Then add a buttion and an if-statement at the top of your main loop so the LEDs only blink when you push the button.
The relay MUST have a reverse connected diode across the coil. I also prefer a driver for the relay rather than connecting direct to the Arduino output.
DVDdoug:
I assume you've found the delay() function? (10 seconds is 10,000 milliseconds.)
Start with the Blink LED example.
Let's stop recommending this.
Instead, scba911 should look at and understand how the "blink without delay" example works (and eventually progress to learning how state machines work).
Granted, for this project it isn't really necessary - but here's the thing:
What scba911 has described for us may very well be a "beta test" platform - something to try out - and not the final project. Or, scba911 might get the project done, then decide "I'd like to add this new functionality XYZ now".
What we'll then get, should scba911 learn and implement the naive delay() blocking code without understanding why that's bad, and why there's a better way, is scba911 coming back to us saying "I wanted to implement this-or-that, but now I have to wait N seconds or what-not between pressing my button and the LCD displaying the words, etc..."
...and then we tell him/her "You need to stop using delay() and instead learn how to implement a state machine and blink-without-delay and etc".
We've all seen this here before, so instead let's tell scba911 now - so we don't have to do round-two in 2 weeks to 6 months (also, by learning the process early, scba911 won't be confused or frustrated by the not-as-intuitive nature of the code).
scba911:
Thanks for all your replies, most helpful. I don't understand the comment by crOsh did I do something wrong ?
no. you did it right.
we (as the community) started by giving you a half answer that would lead to problems very soon.
what crOsh was saying is that we should give you the full answer now, and not part of the answer.
delay() is a blocking command. it stops everything as effectively as a kill switch.
it takes some time to understand 'blink without delay' for a couple reasons.
first, the name is horrible. it could just as easily be labeled blink without peanut butter. who cares was is not used ?
second, blink without delay requires a 3-card-montie.
you have to address the parts you do not care about. so that makes things confusing.
you have the start, a switch or some such.
you are looking at the switch, but need to note the time.
then you need to see if you have done this before, not looking at the switch or the time.
then you need to compare 'now' to 'then'
but, once you understand that 'blink using millis()' or 'blink without delay' is a fundamental part of getting things done while doing other things, your life gets easier.
and once you master it, your life gets even easier.