Reading the state of multiple switches

Hi
I am using Uno. I have 3 switches wired and 4 LED's.
When I press Sw1, LED1 lights up as it should. The same goes for Sw2, LED2 and Sw3, LED3.
I however want LED4 to light when all 3 switches go HIGH at the same time.
This is the code that I have written;

const int buttonPin1 = 5;     // the number of the pushbutton pin
const int buttonPin2 = 6;
const int buttonPin3 = 7;
const int ledPin1 =  2;      // the number of the LED pin
const int ledPin2 =  3;
const int ledPin3 =  4;
const int ledPin4 = 8;


int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);      
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);     
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin1, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW); 
  }
  
  if (buttonState2 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin2, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin2, LOW); 
  }
  
  if (buttonState3 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin3, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin3, LOW); 
  }
  
   if (buttonState1 == HIGH && buttonState2 == HIGH && buttonState3 == HIGH) {
     digitalWrite(ledPin4,HIGH);
   }
   else {
     digitalWrite(ledPin4, LOW);
   }
  
  
}

What is your question?

Use "boolean" for your buttonState variables.

I however want LED4 to light when all 3 switches go HIGH at the same time.

They can't all go HIGH at the same time. They can all BE HIGH at the same time.

This is the code that I have written;

So, what is the problem?

dlloyd:
Use "unsigned int" for your buttonState variables.

What?
Waste fifteen bits?

AWOL, you're fast ... but I did beat you to the correction :wink:

dlloyd:
Use "boolean" for your buttonState variables.

What?
Waste seven bits?
Actually, waste 21 bits.

@dlloyd use strike through when correcting things S

Just a tip.

You can replace ALL of this

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin1, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW); 
  }
  
  if (buttonState2 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin2, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin2, LOW); 
  }
  
  if (buttonState3 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin3, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin3, LOW); 
  }
  
   if (buttonState1 == HIGH && buttonState2 == HIGH && buttonState3 == HIGH) {
     digitalWrite(ledPin4,HIGH);
   }
   else {
     digitalWrite(ledPin4, LOW);
   }

With this

  digitalWrite(ledPin1,buttonState1);
  digitalWrite(ledPin2,buttonState2);
  digitalWrite(ledPin3,buttonState3);
  digitalWrite(ledPin4,(buttonState1 & buttonState2 & buttonState3));

Personally, I think it's easier to read.

Personally, I think it's easier to read.

Except it isn't the same. Plus, I'd have to think about the bitwise AND vs logical AND (that I know would work).

PaulS:

Personally, I think it's easier to read.

Except it isn't the same. Plus, I'd have to think about the bitwise AND vs logical AND (that I know would work).

Give me ONE example where it would produce any result, different to the code it replaces!

Sorry, still very new.
My question I guess is how to get this to work?
For the switches, they are a toggle type, so they remain in the on position.
When all 3 switches are in the on position, the LED's 1 through 3 remain lit.
Which is what I want, however in addition to that I want LED4 to light up when all switches are in the on position.
Perhaps I should be reading the states of the LED 1 through 3, instead of the switch position? Can they all be HIGH at the same time?
As I am still learning, at this point I care more about getting it to function than the most efficient method of coding.
Although I hope to grasp efficient ways at some point.
Thanks
Mark

Which is what I want, however in addition to that I want LED4 to light up when all switches are in the on position

And what happens when they are all on?

When Sw1 through 3 are in the on position, LED1 through 3 are lit, however LED4 remains off.

You could try changing this:

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

to this:

boolean buttonState1 = 0;
boolean buttonState2 = 0;
boolean buttonState3 = 0;

KenF:

PaulS:

Personally, I think it's easier to read.

Except it isn't the same. Plus, I'd have to think about the bitwise AND vs logical AND (that I know would work).

Give me ONE example where it would produce any result, different to the code it replaces!

You have 5 lines of code diddling with LEDs on pins 1, 2, 3, 1, and 4. The original code has 4 blocks, dealing with 1, 2, 3, and 4.

OK. Make that HAD.

dlloyd:
You could try changing this:

int buttonState1 = 0;

int buttonState2 = 0;
int buttonState3 = 0;




to this:


boolean buttonState1 = 0;
boolean buttonState2 = 0;
boolean buttonState3 = 0;

Thanks dloyd,
I tried, but the results are the same.
Since I am new to all of this, would most posts be better off in the "Project Guidance" section or elsewhere?
I appreciate everyone's input.
Mark

IMMarkmc:
When Sw1 through 3 are in the on position, LED1 through 3 are lit, however LED4 remains off.

Just to check that it's not a hardware problem you could add a line digitalWrite(ledPin4,HIGH); at the end of your loop.
Upload and that LED should come on. If it doesn't, you'll know that it's not the logic of your sketch causing the problem.

I'm not sure how the push-button type toggle switches are wired and if you're using external pullup resistors. If not, you should be using INPUT_PULLUP in your pinMode statements.

Well KenF, you are on to something...but I don't know what :~
I added your code at the end of the loop, and I do not get LED4 to light. I did switch it out with one of the other LED's to make sure that it was good and it is? Very confused!

dloyd,
regarding the switches, they are wired with an external (8K2) pulldown resistor each, as in examples, Digital I/O, Button.

Mark