any help appreciated

Hi all - can anyone help me with this piece of code?
I have three solid-state on/off switches that I am using with internal pull-up and two 5v relays. I want three possible outcomes from the code: outcome 1...all three switches are switched ON within 1 second of each other and relay one switches high. outcome 2...not all three switches are switched or all three are switched but not within 1 second of each other and relay two switches high. Outcome 3...none of the above so loop.
(im using millis rather than delay because it will eventually be part of a larger project that prevents me from using delay)
Here is my code - there are no errors showing but the relays remain high no matter what state the switches are in??
Can anyone tell me what i've done wrong?

threeswitch.ino (1.1 KB)

If you want to check that all 3 switches are pressed within a time period you must save the value of millis() when the first switch is pressed and (in the simplest system) read the other 2 switches when the period has elapsed.

HOWEVER ... things can be a lot more complicated. Do you want to ensure that once a switch is turned ON within the period it is not released within the period. One second is a long time - stuff can happen.

Before saying more (and making things more complicated than necessary) I think it would be best if you describe the requirement more completely.

Also, what is triggering the switches?

And how do the switches work - are they the equivalent of push-button switches that can change often or are they like toggle switches that hold their state?

...R

Hi Rob,
Thanks for coming back to me. Yes they are toggle switches. It’s for a game...I want the user(s) to appear to switch all three switches at the same time in which case relay one triggers momentarily. If however they switch just one or two switches OR if the switch all three but it seems as though not at the same time, I want relay two to trigger momentarily in which case they would need to start again. Does that make sense?

magictom:
Yes they are toggle switches. It’s for a game...I want the user(s) to appear to switch all three switches at the same time in which case relay one triggers momentarily. If however they switch just one or two switches OR if the switch all three but it seems as though not at the same time, I want relay two to trigger momentarily in which case they would need to start again. Does that make sense?

In that case the simple suggestion I made in Reply #1 should work.

By the way, setting all 3 switches within 1000 millisecs does not really sound like "at the same time" - but you know your requirement better than I do :slight_smile:

...R

Thanks rob, yes I’m going to change it to 200 milliseconds but that requires two or more people to switch the switches so I’m using 1000 whilst I program and test :slight_smile:
Is there any chance you could help me with the code for how to save the millis at the time of the first switch- do I need eeprom? Not really sure how to do that.
Tfn

  first_switch_time = millis() ;

Where first_switch_time is a global variable of type unsigned long.

The use of millis() is illustrated in Several Things at a Time

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R