I am new to Arduino. I have an UNO that I want to use for a project. I want to use a push button as an INPUT to initiate a loop to run an 8-channel relay sequence just once. Please help with writing the code to run this sequence. The help will be sincerely appreciated.
My setup right now is:
I have a prototype shield on the UNO. I have 5v from the UNO shield to one side of the switch. The other end of the switch going to digital I/O pin 1 of the shield. The relay has 10 pins. 1 pin is VCC which is connected to another 5v pin on the shield. 1 pin of the relay is ground which I have connected to a ground pin on the shield. The 8 IN(IN1-IN8) pins of the relay are connected to digital I/O pins 2-9. If I need to make changes to this setup please let me know.
Get that switch off digital pin 1 as it is used by the Serial interface which you will almost certainly need to output to the Serial monitor for debugging.
Wire one side of the switch to a digital pin and the other side to GND so that the pin is taken LOW when the switch is closed. Use
pinMode(switchPin, INPUT_PULLUP);
so that the input is at a known state at all times.
Forget the relays for now and use the switch to turn the LED on pin 13 on and off. Look at the Button example in the IDE to see how to do it. Once that works substitute a single relay for the LED and get that working.
What sequence of relay outputs do you want, both the order of operation and the timing.
The order of operation for the relay is not really that important, as long as I know which relay goes on first and next and so on. The timing will have to be tweaked in the code later while I am building the project to ensure proper sequence timing. The relays will be used to turn different servos that have different functions in the project. Thanks for the help.
By "just once" I mean, that when the button is pressed, the loop will run and open and close each relay in turn and then stop, not running the loop a second time. But, be able to push the button again to do the same thing as many times as the button is pressed. I believe I need to put and "stop" in the loop in order for the loop to not run again. Not sure how to write that though. Can you help with that?
The order of operation for the relay is not really that important
It will be for the better solutions using a for loop and an array of relay pins and timing periods to replace the clumsy serial code that you have now. For instance will 2 relays ever be on at the same time and will any relays turn on more than once in the sequence ?
I believe I need to put and "stop" in the loop in order for the loop to not run again. Not sure how to write that though.
No need for a stop command. Detect the change of pin state from off to on instead of just detecting that it is on. Look at the StateChangeDetection example in the IDE.
You are right. A relay will have to be run a second time in the sequence but also change direction of the servo. Man, I am way out of my league here but I am determined to do this project. And I really don't understand alot of the terminology in the replies that I am getting. Can anyone recommend a book on Arduino and/or electronics to help me understand?
What type of servos are involved in this project ?
Draw up timelines showing which actions happen and in what order. Put time on the X axis and each thing to be controlled on the Y axis. This will help clarify your requirements and help to suggest solutions. Will it ever be a requirement to terminate the sequence early or maybe halt it and continue ? This could affect the approach taken in programming the solution.
Ok. Yes, one part of the sequence will be halted by a sensor. Once the sensor makes the connection the sequence will continue. I can share the details of the project if you want them. I have a really good idea of the project and the sequence of steps. And a general idea of the parts I will need to buy or fabricate. The finished project will be a voice activated mini-fridge that dispenses a bottle of beer with the cap removed through the top of the fridge.
As for the type of servos: I don't really know what is available or really how to select the proper one for each piece of work being done in the project. I do know that I will need a few continuous rotation bi-directional servos for a few of the parts.
The reason that I asked about the type of servos is that using a relay to drive a servo seems perverse, and actually quite difficult, when the Arduino can control them directly. Please note that an external power supply other than the Arduino will be necessary.