else if problem

Can someone please look over this code and tell me what I did wrong with the “if/else” statement? I copied what was online, but basically what I want is for a temperature sensor to turn on an LED when a certain temperature is exposed or if a switch is pressed and indicates a high, the LED turns on.

arduino_code_v5.ino (1.39 KB)

In the future, please post code inside code tags, not as attachment - that way we don't have to download a file and run a separate program just to read it.

Problem is this line:

else if (4, HIGH);

You need a condition check that means something - (4, HIGH) evaluates to HIGH, which is true.

You don't put a ; after an if statement (you did this right elsewhere).

okay, thank you soooooooo much!

I want the output of pin 2 to be high whenever the temperature sensor senses a temperature below 6 degrees celsius, however, it is turning on all the time, even when the input from pin 4 is low. How do I fix this?

//TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to pin 0
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures

/*
 * setup() - this function runs once when you turn your Arduino on
 * We initialize the serial connection with the computer
 */
void setup()
{
  pinMode(2, OUTPUT); //set digital 2 pin as an output
  pinMode(4, INPUT);  //set digital 4 pin as an input
  
}

void loop()                     // run over and over again
{
  int rawvoltage= analogRead(sensorPin); //reading the sensor
  float volts= rawvoltage/205.0;  //changing the voltage
  float temperatureC= 100.0 * volts - 50; //getting the celsius temperature

  if (temperatureC < 6)                    //if the temperature is less than 6 degrees celsius
  {
    digitalWrite (2, HIGH); //high output to the thermostat
  } 
  else if (4, HIGH)       //if the switch is set to a high
  {
    digitalWrite (2, HIGH); //high output to the thermostat
  }
  else
  {
    digitalWrite (2, LOW); //low output to the thermostat
  }
  delay(300000);                                     //waiting five minutes in seconds

}
else if (4, HIGH)

What's that? Did you mean to do a digitalRead or something?

@delta_g yeah, it’s supposed to be reading from a digital switch

I pointed out this issue in the last thread:

(4, HIGH)

evaluates to HIGH ( http://en.wikipedia.org/wiki/Comma_operator ), which is 1, which is considered true. So that if code will always run, which is the behavior you observe.

If you want to read the pin, and do something if it's high, you need to, you know, read the value on the pin (with digitalRead(4) ) and see if that == HIGH...