If/else if statement

Can someone please explain why this doesnt work:

switchState = digitalRead(switchPin);
if (switchState == HIGH || smokeValue > 100) {
digitalWrite(LED,HIGH);
}

else if (switchState == LOW || smokeValue < 100) {
digitalWrite(LED,LOW);
}

else {
// Nothing
}

Hello arduino10134
Post your entire sketch, well formated, with comments and in so called code tags "</>" and schematic, not a Fritzy diagram, to see how we can help.
Have a nice day and enjoy coding in C++.

int smokeValue;
int motor = 3;
int switchPin = 4;
int switchState = LOW;

void setup()
{
pinMode(3,OUTPUT);
}

void loop()
{
int smokeValue = analogRead(A0);

switchState = digitalRead(switchPin);
if (switchState == HIGH || smokeValue > 100) {
digitalWrite(motor,HIGH);
}

else if (switchState == LOW || smokeValue < 100) {
digitalWrite(motor,LOW);
}

else {
// Nothing
}

}

Hello
The pinMode() for switchPin is missing.

Sorry I missed that out on her. I have that in my code: pinMode(4,OUTPUT);

The switch only works to control the motor when the smoke value is <100. If the smoke value is >100, the motor is running and i am unable to switch it off using the switch.

Hello
Check the coded logic of the conditions.

Do serial prints of smokeValue and switchState just before the if so you can see what they actually are. Do serial prints in each "leg" of the if so you can see if it actually goes where you expect, or not.

edit.. are you sure you want the pin for a switch to be an output? And anyway, show a wiring diagram so we can see how the switch is wired. You need a pullup or pulldown resistor to guarantee a known value always; convention wisdom is to wire switch to ground and use INPUT_PULLUP in pinMode.

edit edit... and what does this actually mean:

Hi thanks for your answer. How do you use INPUT_PULLUP in pinMode? Can you show an example of the code please?

pinMode man page.

INPUT_PULLUP example.

Wire the switch from the pin to ground. When doing it that way, the pin will read high when the switch is open, and low when closed- it's a pretty standard practice.

Can you suggest a solution?

Put serial prints in, and wire the switch correctly (or at least show how it is wired). Nobody can really help without some decent diagnostics from your side.

Did you ckeck the coded logic using some Serial.println() to monitor the values given.

How is is meant to work between the switch and the sensor?

There is no doubt it is doing what you told it to, even if it isn't what you want… so I am with @paulpaulson

Did you ckeck the coded logic using some Serial.println() to monitor the values given.

If the switch is HIGH the LED is on.

if the switch is LOW the LED is follows the sensor <> 100.

So describe in words what you really want.

    if (switchState == HIGH)
        if (smokeValue > 100)
            digitalWrite(LED, HIGH);
        else
            digitalWrite(LED, LOW);
    else 
        if (smokeValue > 100)
            digitalWrite(LED, LOW);
        else
            digitalWrite(LED, HIGH);

a7

The sketch is doing what you told it to do:

"If the switchState is HIGH or the smokeValue is greater than 100, turn on the motor". That will keep turning on the motor whenever the smokeValue is greater than 100 regardless of the 'switchState'.

Maybe you meant to say: "If the switchState is HIGH and the smokeValue is greater than 100, turn on the motor". That would be:

  if (switchState == HIGH && smokeValue > 100) 
  {
    digitalWrite(motor,HIGH);
  }

as IO pins are inputs by default, pinMode() is needed if one wishes to set the pin to INPUT_PULLUP or OUTPUT

Hello Perehama
Thank you for your advice, I have taken note of it. :nerd:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.