Problem with 2 digital inputs (switches)

Hi, I’m a beginner in Arduino and I’m having trouble with 2 switch buttons that I want to use to light 3 leds on. The only thing is, that I need them to be pushed at the same time, to create action. So if I push one of them, their must be no action. but how do I do that, since I’m a beginner and I don’t know that much about the Arduino programming language. Can anyone please help me here, cause I’m stuck and I can’t go forward now.

This is the code I’m currently using, but it doesn’t work…

pushbutton_while.ino (1.2 KB)

Well, digitalRead() takes one pin number as a parameter so this won't work:

digitalRead(pushButton1 && pushButton2)

There are probably other mistakes if you did this...

const int pushButton1 = 1;
const int pushButton2 = 2;
const int led1 =  8;
const int led2 =  9;
const int led3 = 10;
int count = 0;
int firstTime = 1;
void setup() {
  pinMode(pushButton1, INPUT);
  pinMode(pushButton2, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  Serial.begin(9600);
}

void loop() {
  while (digitalRead(pushButton1 && pushButton2)) {
    if (firstTime) {
      if (count == 0) {
        count = 1;
      } else if (count == 1) {
        count = 2;
      } else if (count == 2) {
        count = 3;
      } else if (count == 3) {
        count = 0;
      }
    }
    delay(10);
    firstTime = 0;
    //Serial.println(count);
  }
  if (!firstTime) {
    if (count == 1) {
      digitalWrite(led1, HIGH);
      digitalWrite(led3, LOW);
    } else if (count == 2) {
      digitalWrite(led2, HIGH);
      digitalWrite(led1, LOW);
    } else if (count == 3) {
      digitalWrite(led3, HIGH);
      digitalWrite(led2, LOW);
      count = 0;
    }
  }
  Serial.print(count);
  Serial.print(" ");
  Serial.println(firstTime);
  firstTime = 1;
}

Welcome to the forums. Please read the sticky post at the top to learn how to properly post your code using code tags. It will help people help you. They can see the code directly and not have to download it.
Like this:

const int pushButton1 = 1;
const int pushButton2 = 2;
const int led1 =  8;
const int led2 =  9;
const int led3 = 10;
int count = 0;
int firstTime = 1;
void setup() {
  pinMode(pushButton1, INPUT);
  pinMode(pushButton2, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  Serial.begin(9600);
}

void loop() {
  while (digitalRead(pushButton1 && pushButton2)) {
    if (firstTime) {
      if (count == 0) {
        count = 1;
      } else if (count == 1) {
        count = 2;
      } else if (count == 2) {
        count = 3;
      } else if (count == 3) {
        count = 0;
      }
    }
    delay(10);
    firstTime = 0;
    //Serial.println(count);
  }
  if (!firstTime) {
    if (count == 1) {
      digitalWrite(led1, HIGH);
      digitalWrite(led3, LOW);
    } else if (count == 2) {
      digitalWrite(led2, HIGH);
      digitalWrite(led1, LOW);
    } else if (count == 3) {
      digitalWrite(led3, HIGH);
      digitalWrite(led2, LOW);
      count = 0;
    }
  }
  Serial.print(count);
  Serial.print(" ");
  Serial.println(firstTime);
  firstTime = 1;
}

As for your code, you can not specify reading two pins like this

  while (digitalRead(pushButton1 && pushButton2)) {

It must be done as two separate calls to digitalRead()

  while (digitalRead(pushButton1) && digitalRead(pushButton2)) {

How to you have your buttons wired up? Do you have external pullup resistors? You will need them based on declaring your pins as INPUT and expecting them to be HIGH when not pressed. A more typical approach is to wire one side of the button to ground and the other to a pin and then declare the pin as INPUT_PULLUP. This does reverse the logic (HIGH = not pressed, LOW = pressed).

As for "simultaneous" you will never achieve that. A human may think they pressed the buttons at the same time but your arduino is fast it can easily detect a difference of a millisecond or two between those presses.

I'd recommend getting familiar with all the button press and state change examples in the IDE

Please post your code with tags as described in the post named "Read this before posting a programming question".

You are using pins 1&2 for input. Pins 0&1 should normally be avoided because they have special TX RX functions. You would be better of with pins 2 & 3.

You are setting the input pins with pinMode(x, INPUT); While that does work it does not attach a pullup resistor to the pin, so you need to do that outside the arduino so that the button can pull the pin from a high to a low. To avoid that just use setMode(x, INPUT_PULLUP); That simplifies the button.

I don't think this line does what you want: while (digitalRead(pushButton1 && pushButton2))
Assuming you want to fall through when both buttons are pressed, try this instead:
while (digitalRead(pushButton1) == 0 && digitalRead(pushButton2) == 0)

Don't know if that fixes everything, but it should be a good start.

This:

      if (count == 0) {
        count = 1;
      } else if (count == 1) {
        count = 2;
      } else if (count == 2) {
        count = 3;
      } else if (count == 3) {
        count = 0;
      }

would normally be coded:

      if (++count >3) count = 0;

Or

  count %= 4 ;

or

  count = (count + 1) & 3 ;

Thanks a lot guys, it really helped me out.

  count %= 4 ;

doesn’t increment

Since you are beginner, try to visualize what you want to do with a simple example of the following diagram that contains 2 switches and 1 LED.

led1sw2.png
Figure-1:

Task-1:
Connect K1 with internal pull-up resistor enabled. Now, create sketch to check that K1 is closed and then ignite L (built-in LED of UNO).

void setup()
{
    Serial.begin(9600);   //enable Serial Monitor
    //-------------------
    
   pinMode(9, INPUT_PULLUP);    //set direction of DPin-8 as INPUT with internal pull-up
   pinMode(13, OUTPUT);
   //------------------------------

   digitalWrite(13, LOW);   // L is initially OFF

    //----check again and again that K1 is closed--------
    do
    {
         ;   //do nothing and keep checking the condition of K1
    }
    while (digitalRead(9) != LOW);
   //-----------------------------

   digitalWrite(13, HIGH);      //K1 is closed; ignite L
}

void loop()
{

}

Task-2: Repeat Task-1 for K2 of Fig-1.

Task-3: Combine Task-1 and Task-2 so that L is ON when both K1 and K2 are found closed.

led1sw2.png

holdingpattern:
Why on Earth would you suggest a do..while () in setup() rather than letting loop() do the work?

Once K1 and K2 are found closed, the L is ON; there is nothing to do for the MCU; so, let it do the job on setup() and then be relaxing in the empty loop().

GolamMostafa:
Once K1 and K2 are found closed, the L is ON; there is nothing to do for the MCU; so, let it do the job on setup() and then be relaxing in the empty loop().

The logic is correct, but I find the use of do..while superfluous.

void setup()
{
  pinMode(9, INPUT_PULLUP);    //set direction of DPin-9 as INPUT with internal pull-up
  pinMode(LED_BUILTIN, OUTPUT);
  //------------------------------

  digitalWrite(LED_BUILTIN, LOW);   // L is initially OFF
  //----check again and again that K1 is not closed--------
  while (digitalRead(9) == HIGH) { };
  digitalWrite(LED_BUILTIN, HIGH);      //K1 is closed; ignite L
}

void loop()
{ }

aarg:
The logic is correct, but I find the use of do..while superfluous.

I used do-while, and now you are using while-do -- are they not equivalent/doing the same job?

GolamMostafa:
I used do-while, and now you are using while-do -- are they not equivalent?

I could have used a goto statement and it would be equivalent, but that wouldn't justify using it. It's best to use language that matches the intention of the code. 'do-while' is a relatively rare construct, and forces the reader to stop and decide why it is used instead of the much more common 'while'.

Whether the do-nothing code block precedes or follows the first conditional test, doesn't matter. So it's better to express it in a conventional and concise way.

I used do-while, and now you are using while-do -- are they not equivalent?

No, they are not equivalent

do/while causes the code in the associated code block to be executed at least once because the test happens after the code block

Using while/do causes the test to happen before executing the code in the code block so it may never be executed

aarg:
I could have used a goto statement and it would be equivalent, but that wouldn't justify using it. It's best to use language that matches the intention of the code. 'do-while' is a relatively rare construct, and forces the reader to stop and decide why it is used instead of the much more common 'while'.

Whether the do-nothing code block precedes or follows the first conditional test, doesn't matter. So it's better to express it in a conventional and concise way.

+karma.

holdingpattern:
That was actually my point: why not have loop() actually reading the switches for changes rather than relaxing. A more general solution can always be limited if some functionality is not required; it's more difficult to generalise from your unnecessarily restrictive approach.

Because I have wished to remain in full compliance with the notion that task (s) that is executed only for once or for a limited number of times are to be placed under setup() function. Checking again and again if the switch is closed or not may appear at a first thought a looping function; but, when viewed carefully it is not -- once the switch is found closed, the job is done. Therefore, it is my opinion that there is not much programming illiteracy in placing the task under setup() function.