Turning on 2 lights simultaneously with 2 switches

We have two lights that we wish to turn on at the same time, controlled by two switches. If both switches are activated within the same second then both lights will turn on and stay on. If only one switch is activated within the second, only one light will turn on then turn off after two seconds. Here is my code that I have at the moment, if anyone can help out please do. Thanks.

int wireS1_1 = 16;
int wireS1_2 = 17;
int wireS2_1 = 18;
int wireS2_2 = 19;

int relay1 = 14;
int relay2 = 15;

int switchstate = 0;
void setup() {
  pinMode(wireS1_1, INPUT_PULLUP);
  pinMode(wireS1_2, INPUT_PULLUP);
  pinMode(wireS2_1, INPUT_PULLUP);
  pinMode(wireS2_2, INPUT_PULLUP);

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
}
void loop() {
            pinMode(wireS1_1, OUTPUT);
          digitalWrite(wireS1_1, LOW);

          pinMode(wireS2_1, OUTPUT);
          digitalWrite(wireS2_1, LOW);

if(digitalRead(wireS1_2) == LOW or digitalRead(wireS2_2) == LOW)
          {
            delay(1000);
             if(digitalRead(wireS1_2) == LOW and digitalRead(wireS2_2) == LOW)
             {
               switchstate = 3;
             }
             else if(digitalRead(wireS1_2) == LOW)
             {
              switchstate = 2;
             }
             else if(digitalRead(wireS2_2) == LOW)
             {
              switchstate = 1;
             }
             else
             {
              switchstate = 0;
             }
          }
             switch(switchstate){
              delay(1000);
              case 0: 
                digitalWrite(relay1, LOW);
                digitalWrite(relay2, LOW);
                break;
              case 1:
                digitalWrite(relay2, HIGH);
                digitalWrite(relay1, LOW);
              case 2:
                digitalWrite(relay1, HIGH);
                digitalWrite(relay2, LOW);

              case 3:
                digitalWrite(relay1,HIGH);
                digitalWrite(relay2,HIGH);
             }
}

That delay(1000) causes your code to wait one second, then check again whether a button is pressed. The press on the second button within that second will be missed.

Continue checking for the second button, using millis() to keep track of whether a second has passed since the first detected press.

Where are wireS1_1 and 2 / wireS2_1 and 2 defined?

.

larryd:
Where are wireS1_1 and 2 / wireS2_1 and 2 defined?

And apart from that, what are they? Are they 2 sides of each of 2 switches?- are the switches across i/o pins? That’s a a bit unusual: switches usually takes a pin to ground, not to a low i/o pin.

1_1 and 2_1 start life as inputs (with pullups), then in loop() become low outputs which is what makes me think they’re one side of each switch.

If I’m right, you could probably simplify that by taking the switch to ground.

1_1 and 2_1 start life as inputs (with pullups), then in loop() become low outputs which is what makes me think they're one side of each switch.

I think you give too much credit.

.

larryd: I think you give too much credit.

Diagram would help

Hi, What Arduino are you using? Please do not go back to earlier posts and edit them to answer later post. Make a new post with your changed code, this will help anybody, including me who has just read the thread, try to understand the situation.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? This so we can see how you have your components are connected.

Thanks.. Tom.. :)

Mmm... OP silently edited the code in #1.

This is also incorrect:

if(digitalRead(wireS1_2) == LOW or digitalRead(wireS2_2) == LOW)

I think you mean:

if(digitalRead(wireS1_2) == LOW || digitalRead(wireS2_2) == LOW)

Now in this case the bitwise or will indeed work but it's not the proper way of doing it.

Zsapp1460: We have two lights that we wish to turn on at the same time, controlled by two switches. If both switches are activated within the same second then both lights will turn on and stay on. If only one switch is activated within the second, only one light will turn on then turn off after two seconds. Here is my code that I have at the moment, if anyone can help out please do. Thanks.

int wireS1_1 = 16;
int wireS1_2 = 17;
int wireS2_1 = 18;
int wireS2_2 = 19;

int relay1 = 14; int relay2 = 15;

int switchstate = 0; void setup() {   pinMode(wireS1_1, INPUT_PULLUP);   pinMode(wireS1_2, INPUT_PULLUP);   pinMode(wireS2_1, INPUT_PULLUP);   pinMode(wireS2_2, INPUT_PULLUP);

  pinMode(relay1, OUTPUT);   pinMode(relay2, OUTPUT); } void loop() {             pinMode(wireS1_1, OUTPUT);           digitalWrite(wireS1_1, LOW);

          pinMode(wireS2_1, OUTPUT);           digitalWrite(wireS2_1, LOW);

if(digitalRead(wireS1_2) == LOW or digitalRead(wireS2_2) == LOW)           {             delay(1000);             if(digitalRead(wireS1_2) == LOW and digitalRead(wireS2_2) == LOW)             {               switchstate = 3;             }             else if(digitalRead(wireS1_2) == LOW)             {               switchstate = 2;             }             else if(digitalRead(wireS2_2) == LOW)             {               switchstate = 1;             }             else             {               switchstate = 0;             }           }             switch(switchstate){               delay(1000);               case 0:                 digitalWrite(relay1, LOW);                 digitalWrite(relay2, LOW);                 break;               case 1:                 digitalWrite(relay2, HIGH);                 digitalWrite(relay1, LOW);               case 2:                 digitalWrite(relay1, HIGH);                 digitalWrite(relay2, LOW);

              case 3:                 digitalWrite(relay1,HIGH);                 digitalWrite(relay2,HIGH);             } }

Please show your changes in a new post rather than editing the original post.

.