The setup I have involves having 2 switches, I want to 'write' a pin 'ledPin2' LOW IF both buttons are pressed (button 1 and button 2 are both pressed simultaneously)
The lines of code are as follows:
if(buttonState1 == HIGH && buttonState2 == HIGH)
{
digitalWrite(ledPin2,LOW);
}
The above code is what I have however, I am checking the output on ledPin2 with a multi-meter and it is not displaying the correct value.
I am thinking my issue is incorrectly using the '==' statement. How would I correctly address this?
Thanks in advance!
Full code as follows:
// set pin numbers:
int buttonPin1 = 3; // Trigger pin 2 Single Click of Trigger
int buttonPin2 = 4; // Trigger Pin 5 Two Click of Trigger
int ledPin1 = 8; // BODNAR - Trigger switch if clicked once GREEN LED
int ledPin2 = 12; // BODNAR - Trigger switch if clicked once RED LED
int buttonState1 = 0; // READS TRIGGER PIN 2 (GREEN)
int buttonState2 = 0; // READS TRIGGER PIN 5(RED)
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
//initalize the ledpins
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
//******************************** READ TRIGGER CLICK 1 TO TRIGGER GREEN LED ********************************
if (buttonState1 == LOW)
{
// Turn GREEN LED OFF:
digitalWrite(ledPin1, HIGH);
}
if (buttonState1 == HIGH)
{
// Turn GREEN LED ON:
digitalWrite(ledPin1, LOW);
}
//******************************** READ TRIGGER CLICK 2 TO TRIGGER RED LED ********************************
if (buttonState1 == LOW && buttonState2 == LOW)
{
// Turn RED LED OFF:
digitalWrite(ledPin2, HIGH);
}
if(buttonState1 == HIGH && buttonState2 == HIGH)
{
// Turn RED LED ON:
digitalWrite(ledPin2,LOW);
}
}
This minimal code below shows that your && works correctly. My buttons are high to start, due to input pullups, so the delay() in setup() shows the led is on, and as soon as loop() starts, the led goes off since both buttons are high (un-pressed in my case).
bool buttonState1;
bool buttonState2;
byte ledPin2 = 10;
byte button1 = 2;
byte button2 = 3;
void setup()
{
Serial.begin(9600);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP); //so both pins will be high
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, HIGH);
delay(1000); //to show the led on for a bit
} //setup
void loop()
{
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
Serial.print(buttonState1); Serial.println(buttonState2); //this shows a stream of 11's
if (buttonState1 == HIGH && buttonState2 == HIGH)
{
digitalWrite(ledPin2, LOW);
}
} //loop
Put that Serial.print in like mine to see if you get the right values from the buttons together.
You talk of clicks: maybe you think the button state is latched until it gets pressed again. Are you holding the buttons?
Sorry I have added my code in the original post now.
I have hard wired the switch with the Arduino 5V feed and signal pins (pins 3 and 4 for switch 1 and 2 respectively).
The ledPin1 and ledPin2 are from pins 8 and 12 respectively and are outputs.
These are actually plugged into a BU0836X 12-Bit Joystick Board with the goal to illuminate buttons through the 'usb gaming controller' application of the computer.
At the moment ledPin1 works as expected however, the output on ledPin2 is incorrect.
FEBaily:
Did you put in the Serial.print()s I suggested, to see what those two states really are just as the if() is checked?
I have just performed another test. Using your code, when no buttons are pressed it out puts '01'.
However, when i put the multi-meter onto ground and the switch, it prints '00' with no buttons pressed, 01 with 1 button pressed and 11 when both are pressed. (This is what I want!)
Would this mean there is a grounding error through the switch? Is there a way I can get around having the multimeter permanently in circuit
No we were just working between the switch - bodnar and Arduino boards. I gave away with the Arduino board and hard wired things through some relays and everything is working.
It seems it was a conflict issue between the Bodnar board and the Arduino 5V input / ground somewhere in the circuit.
tpratt95:
It seems it was a conflict issue between the Bodnar board and the Arduino 5V input / ground somewhere in the circuit.
You could, for your peace of mind and / or understanding, not to mention others' curiosity, post the before and after schematics to see if anyone can spot what was wrong.