small code problem

I am having a problem with this function, its probably a simple problem. This is part of a much larger program but this function allows me to turn on a maintenance switch and let me run the maintenance program which simply allows me to run an H-bridge PWM motor speed controller. I can just send a continuous 5 volt signal to the EnablePin and everything works fine. But, I want to have the EnablePin only go HIGH when the Gate_Down_Switch or the Gate_Up_Switch is pressed. It would seem that the code for this would be:

if(digitalRead(Gate_Up_Switch)==0) //ON { digitalWrite(EnablePin,HIGH); //ON } else { digitalWrite(EnablePin,LOW); } if(digitalRead(Gate_Down_Switch)==0) //ON { digitalWrite(EnablePin,HIGH); //ON } else { digitalWrite(EnablePin,LOW); }

This does not work properly. I changed the LOWs to HIGHs just to keep 5 volts on the enable pin to make it work for now.

Is there something very simple that I am doing wrong?

===================================================== void runMaint() { while(digitalRead(Maintenance_Switch)==0)//ON { lcd.setCursor(0,1); lcd.print("Maint Mode:"); lcd.setCursor(0,3); //col 0,row 3 11/12/2014 lcd.print("Press Reset For Menu"); // 11/12/2014 delay(1); lcd.setCursor(11,1);

if(digitalRead(Gate_Up_Switch)==0) //ON { digitalWrite(EnablePin,HIGH); //ON } else { digitalWrite(EnablePin,HIGH); } if(digitalRead(Gate_Down_Switch)==0) //ON { digitalWrite(EnablePin,HIGH); //ON } else { digitalWrite(EnablePin,HIGH); }

if(digitalRead(Gate_Up_Switch)==0) //on { lcd.print("Up"); analogWrite(Motor_Up_Control,110); //on delay(1); } else { lcd.print(" "); digitalWrite(Motor_Up_Control,LOW); delay(1); } if(digitalRead(Gate_Down_Switch)==0) { lcd.print("Dn"); analogWrite(Motor_Down_Control,110); delay(1); } else { lcd.print(" "); digitalWrite(Motor_Down_Control,LOW); delay(1); } } lcd.setCursor(0,1); lcd.print(" "); } //END runMaint() ============================================

I think your problem is that you have two separate tests that cancel each other out. I suspect you need a single test like this pseudo code

if (X == ON || Y == ON) {
   // do something
else {  // this means that neither of them is on
  // do nothing

And please post code in code tags as it makes it much easier to deal with


Think about what happens if the gate up switch is active but the gate down switch isn't. The first if turns the pin HIGH, but then the second on turns it right back off again.


I tried this code instead but got error below:

expected primary-expression before "if"

I've tried putting parenthesis in several different places but could not get it right. Any ideas how to set up this ||?


if(digitalRead(Gate_Up_Switch)==0) || if(digitalRead(Gate_Down_Switch)==0) { digitalWrite(EnablePin,HIGH); //ON } else { digitalWrite(EnablePin,LOW); //off }

Thanks for the info on tags. Not sure what they are yet, I'm new to the site. I'll look them up.

Just read the other comment, I think you have something there, let me think about that.



if(digitalRead(Gate_Up_Switch)==0) || (digitalRead(Gate_Down_Switch)==0)


I tried it but got this error:

expected primary-expression before '||' token


if(digitalRead(Gate_Up_Switch)==0) || (digitalRead(Gate_Down_Switch)==0)

// if(digitalRead(Gate_Up_Switch)==0) || if(digitalRead(Gate_Down_Switch)==0)


digitalWrite(EnablePin,HIGH); //ON




digitalWrite(EnablePin,LOW); //off


Thanks everyone. I added another set of parenthesis and it works.

Thanks again,


if((digitalRead(Gate_Up_Switch)==0) || (digitalRead(Gate_Down_Switch)==0)) // if(digitalRead(Gate_Up_Switch)==0) || if(digitalRead(Gate_Down_Switch)==0)