I have a project with 4 switches and would like to control them via a MEGA. There are 4 outputs going to lamps, that flash at a set rate, and four inputs from mechancal switches with a 0 or 5 volt signal to turn the lmps on or off. I can make any one function OK , but what does the code look like so that the prgram will just keep looking for a switch change and then take the required action with a number of lamps ? I am new to programming C++ but have done a fair bit in Basic.
Thank you.
I know this is simple question but I couldn’t find the answer by a search.
If you posted a sample of what you are doing now we could offer a recommendation on how to extend it to more than one switch. The issue is are you using interrupts or just polling?
I haven’t done anything yet. I was hoping for an outline of what I needed to write. There are plenty of sketches that show debouncing and switching a single LED on or even flashing but not for a multiple of them. Assume that the momementary contact is for only 100msecs, is it possible that it could be missed? Should I use interupts or polling ? please advise. thank you. There is no risk that two switches are operated at one time since the inputs are manual. ( FYI The system is for my ecar that I have almost completed building. The switches are microswitches operated by a joystick with a central return.I used 4 as an example but in reality there are many more switching functions , think car…)
Given that your target application is many momentary switches, I would use a timer interrupt set to sample rate at least 100 hz (10 mS period). In the ISR, read the actual state of the switches, debounce and then set a global debounced switch state. By keeping it in the ISR, you avoid the complexity of dealing with switches and maintaining switch states in your main loop. Makes your switches more responsive, too.
My favorite abstraction is to have a counter per switch. When you sample a switch closed, increment counter per interrupt to some debounce max. Switch open, decrement counter per interrupt down to 0. If a switch counter is at debounce max, it’s closed. otherwise, it’s open. For example, at a 10 mS sample period max can be 4 for a 40 mS debounce period. This allows you to easily play with the debounce period - some switches seem to bounce forever. You can wrap toggle state around this as well (require fully off to fully on back to fully off for a toggle transition).
themotorman:
I have a project with 4 switches and would like to control them via a MEGA. There are 4 outputs going to lamps, that flash at a set rate, and four inputs from mechancal switches with a 0 or 5 volt signal to turn the lmps on or off. I can make any one function OK , but what does the code look like so that the prgram will just keep looking for a switch change and then take the required action with a number of lamps ? I am new to programming C++ but have done a fair bit in Basic.
Thank you.
I know this is simple question but I couldn’t find the answer by a search.
The simplest thing you can do is check each switch one after the other. There is a counter for each switch. Check the switch and if closed increment the counter if the counter is not already at some maximum value. If the switch is open decrement the counter but stop at zero
Next after checking all the switches you have a veriable or “flag” for each lamp that says if the lamp is on or off. Check the variable and the counter and determine if the status of the lamp needs to be changed. Change the lamp only if the counter is at maximum or zero. If so change it and update the variable. After you’ve done this for all lamps go back and check the switches. The flag prevents you from turning on a lamp that is already on
I guess there is no reason to do all the switches first then all the lamps. You can mix them up. The trick is picking a debounce counter max value. You want the smalled value that works.
I cannot link to youtube from work but search for sciguy14. He has a series of tutorials on the arduino and he specifically covers turning an LED on and off using a switch including software debouncing. Once you can do one you can do more than one. The whole series is great and taught me a lot when I first started.