One of the else if statement i s not running

In the below code the first two conditions are working properly but the third condition is not working at all.infra and red are two functions which creates frequencies which vary with time (they are working as expected ) .i have tested the below code in oscilloscope and am getting the desired output for the first 2 conditions but it fails to produce desired output in the third conditions instead it produces some junk value.why is this happening?

void loop() { if(digitalRead(2)==HIGH&&digitalRead(3)==LOW)//trigger conditions infra();

else if(digitalRead(2)==LOW&&digitalRead(3)==HIGH) red(); else if((digitalRead(2)==HIGH&&digitalRead(3)==HIGH)||(digitalRead(2)==LOW&&digitalRead(3)==LOW)) { digitalWrite(6,LOW);



Use code tags. See the "how to use this forum" post at the top of every forum.

Remember the Arduino has to work in the real world. It doesn't know when the inputs change. Each time you use digitalRead() it goes and looks at that input. For logic statements to work like they do in the mathematician's brain, you must grab a set of inputs and then use that set, even if something might change in between reading the input and using the value.

if(digitalRead(2) != digitalRead(2)) Serial.println("Yes, this really can evaluate to TRUE sometimes.");
else if((digitalRead(2)==HIGH&&digitalRead(3)==HIGH)||(digitalRead(2)==LOW&&digitalRead(3)==LOW))

That seems like an awfully long winded way to say

else if(digitalRead(2) == digitalRead(3)){

Actually, it's worse than that. Given what the other two if conditions are, this is actually an insanely long winded way to say:


as there are no other possibilities left.

Pins 2,3 are manually triggered and based on its state I want 6 to give the each condition will make pin 6 give different outputs. After connecting to the oscilloscope I have observed that the first 2 conditions are working fine so when I give VCC to pin 2 and ground pin 3 or vice versa I am getting the desired output,but in the last case it is not making pin 6 low rather it is producing a junk signal.My problem lies only with last condition which is a simple digitalwrite statement which seems to not work.

What sort of junk signal? What do you mean? Please be specific. What does the rest of the code do. Can you post the whole thing. Would you please use code tags when posting it. Read the How to use this forum - please read thread for more info on how to do that.

so each condition will make pin 6 give different outputs.

How? Pin 6 is either on or off. Given that you could have 1) Neither pin high 2) Pin 2 only high 3) Pin 3 only high 4) Both pins high How are you going to make a digital pin show which of those 4 conditions is true?

I don't like this style of programming


especially when followed by

else if(digitalRead(2)==LOW&&digitalRead(3)==HIGH)

first, because there is no means to print the pin values for debugging purposes second, because different values are used in the second test

Read all the pins and save their values and then do the tests based on the saved values - something like

pin2State = digitalRead(2);
pin3State = digitalRead(3);
if(pin2State == HIGH && pin3State == LOW) {

else if (pin2State == LOW && pin3State == HIGH) {


It may also make the logic more obvious if you do the tests separately like

if(pin2State == HIGH) {
   if (pin3State == LOW) {

else {
    if (pin3State == HIGH) {



Read all the pins and save their values and then do the tests based on the saved values - something like