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);
}
}
// 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);
}
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
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.