I am trying to create a program where I am able to control the relays on an 4 relay shield board to pulsing one of the GPIO pins, for example, first pulse turns on relay 1, second pulse turns off relay 1, third pulse turns on relay 3. That kind of thing. Problem is that the pulsing of the GPIO pins runs through the program to quickly. There are 22 total steps and without delays it will end up on step 16 after a 10ms pulse! I have tried implementing delays to stop this but the program can sometimes miss the inputs due to these delays. What is a better method of doing this? I have attached my program so any help would be great!
The StateChangeDetection example is pertinent too I think.
There is a difference between responding to an input being high, and to it going high. The
later requires using a variable to remember the previous state of the input. Remember loop() is
supposed to (and generally does) run thousands to hundreds of thousands of times a second.
The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.
Hey guys, thanks for all the great advice, I removed the delays and looked into the state detection example which has fixed the issue I was having with the loop being to fast, however, the program still seems to miss a lot of the inputs. I tried changing the length of the pulse that is attached to Arduino GPIO pins to 2 seconds in the hopes the program wouldn't possibly miss it, still seems to though. Is there anything stupid I have done in the code that is preventing anything?
Please use the AutoFormat tool to lay out your code in a way that is familiar and easy to follow. Putting several things on one line is no benefit to anyone.
Robin2:
Please use the AutoFormat tool to lay out your code in a way that is familiar and easy to follow. Putting several things on one line is no benefit to anyone.
Find it easier to understand, will make it more convenient in the future
edgemoron:
What is your program supposed to do? Please explain in detail.
Was this not explained in my first post?
Anyway, I am trying to make a sequence of relays turn on and off speakers and lights at different intervals. Something will pulse the Arduino, which will turn on relay one lets say, on the next pulse, it will turn it off, on the third pulse, relay 4 will turn on, etc etc. I actually have two Arduinos running very similar iterations of the code, one for the first four relays and one four the second for, not that this is relevant though.
Hope this clears things up a bit, not looking to provide any confusion.
Thanks for the great advice, went and had a look at the state change example again and changed the bit relevant and it has solved a number of issue I was having! I have linked up a switch to test it however and seem to be suffering from some debouncing effects going on. What could I do to prevent this issue as much as possible?
chickwolf:
What could I do to prevent this issue as much as possible?
You have made changes to the program but you have not posted the new version - and PLEASE DO NOT over-write the earlier version. It is helpful to be able to compare them.
Robin2:
You have made changes to the program but you have not posted the new version - and PLEASE DO NOT over-write the earlier version. It is helpful to be able to compare them.
AWOL:
Remind me what benefits these functions bestow?
It may not be clear, but the first function, turns on relay 1, and the second function, turns off relay 1. Should really get into the habit of commenting to make things clearer.
chickwolf:
It may not be clear, but the first function, turns on relay 1, and the second function, turns off relay 1. Should really get into the habit of commenting to make things clearer.
Yes, I can see what they do (assuming non-inverting logic), but what bbenefit do they bestow? (Apart from bulking-out your line-count, assuming you get paid by the line-of-code)
chickwolf:
seem to be suffering from some debouncing effects going on.
Remove line 159 so you only read the button once.
AWOL:
but what bbenefit do they bestow?
I can see that they might make the action of the code more obvious. But if that is the intention I think I would prefer to call the functions relayOFF() and relayON()