Ok, so I have a question about the most efficient way to go about writing a piece of code. I have 7 devices attached to my arduino (teensy 3). Each device has a pin that I write to (to turn it on/off) and a pin that I read from (we'll call it our status pin). Now, if the status pin is blinking at ~500ms on, ~500ms off, then everything is fine and we don't need to do anything. However, if there is a problem with one of the devices, upon being turned on it will rapidly blink (around ~65ms on, ~65ms off) about 18 times and then turn itself off. If this happens, I need to kill the rest of the devices as well, as they all have to be on/off together. What is the best way to go about this? Write now, my code is extremely sloppy feeling, I don't have it with me or I would post it as a base. I think it might be good that I not post my method, as I'm interested to see what you guys can come up with. Thanks a lot!
Well we are not writing it for you. The best way is to use a state machine like the blink without delay example.
Also if you need to do the same thing to many pins then use arrays to define them ans so the same code can do the same thing to the pins. What pin is used is defined by the array's index. This lends itself to being implemented in a for loop.
Grumpy_Mike:
Well we are not writing it for you. The best way is to use a state machine like the blink without delay example.
Also if you need to do the same thing to many pins then use arrays to define them ans so the same code can do the same thing to the pins. What pin is used is defined by the array's index. This lends itself to being implemented in a for loop.
EDIT: My last comment seemed pretty rude.
I understand this already, I'm a software guy not a hardware guy. I have currently implemented my way, but it seems sloppy and less than optimal. I didn't post my code simply so that everyone who looked at this could propose a solution from a clean slate. Thanks for the feedback though.
My first though would be to keep track of the transition times for each device. If you detect a transition time below a certain threshold, turn everything off. Doesn't sound like anything too complicated.
Keep an array of unsigned longs. Loop through your pins and digitalRead them. For any that are high, store millis in the corresponding array entry. Loop through the array and compare the times to millis. If any of them have a difference > 750mS, shut everything down.
This assumes that you can wait a little. A similar, but slightly more complex approach will be needed if you want to catch the "I'm going to shut down shortly" signal.