LM35 with LED

I have used the LM35 along with a LED pin as an indicator using an if/else statement. The error message that I am getting states that there is an "else without previous if" although the if command is there. What am I doing wrong? Here is the code:

float tempC;
int LM35;
int tempPin = 0;
int ledPin[] = {1,2};

void setup()
{
analogReference(INTERNAL);
pinMode(ledPin[1], OUTPUT);
pinMode(ledPin[2], OUTPUT);
Serial.begin(9600);
}
void loop(){
LM35 = analogRead(tempPin);
tempC = (5.0 * analogRead(tempPin) * 100.0) / 1024;
{
if
(tempC > 566);
digitalWrite(ledPin[1], LOW);
Serial.println("-565");
else
(tempC < 566);
digitalWrite(ledPin[2], HIGH);
Serial.println("567+");
delay(1000);
}
}

I typed 'else without previous if' into the search box (top right) and got over 13,000 results. Didn't any of those help?

Let me check. Thanks

rewrite your code like this:

float tempC;
int LM35;
int tempPin = 0;
int ledPin[] = {1,2};

void setup()
{
analogReference(INTERNAL);
pinMode(ledPin[1], OUTPUT);
pinMode(ledPin[2], OUTPUT);
Serial.begin(9600);
}
void loop(){
LM35 = analogRead(tempPin);
tempC = (5.0 * analogRead(tempPin) * 100.0) / 1024;

if (tempC > 566) {
  digitalWrite(ledPin[1], LOW);
  Serial.println("-565");
}
else if  (tempC < 566){ 
  digitalWrite(ledPin[2], HIGH);
  Serial.println("567+");
  delay(1000);
}
}

analogReference(INTERNAL);
tempC = (5.0 * .....
The maths line assumes 5volt Aref.

if (tempC > 566)
Will it ever reach that number?
Leo..

Yes, when the room temperature reaches 61 C. :o

outsider:
Yes, when the room temperature reaches 61 C. :o

Assume the max possible A/D value: 1023

tempC = (5.0 * analogRead(tempPin) * 100.0) / 1024;
tempC = (5 * 1023 * 100) /1024 = 511500 / 1024 = ~499

Aref is set to internal (~1.1volt).
Maths line, if actual Aref is e.g 1.065volt, should be:
tempC = (1.065 * analogRead(tempPin) *100.0) / 1024;

Hint:
The IF statement, could be in tempC (~5 to 100C) or in A/D value (0 to 1023).
Leo..

The OP has Internal AREF set (1.1V), 1.1 x 566 x 100 / 1023 = 60.86 degrees C.

if (tempC > 566)

When will the temperature be 566 degrees C.
When the A/D value is 1159? or maybe 5269?

Maybe it should/could be
if (LM35 > 566)

Leo...this ain't lookin' good. ::slight_smile: G'nite all.

outsider:
Leo...this ain't lookin' good. ::slight_smile: G'nite all.

Maybe you see the light in the morning...

Thank you. The compilation loaded fine. Can't wait to get home and try

Wawa:
analogReference(INTERNAL);
tempC = (5.0 * .....
The maths line assumes 5volt Aref.

if (tempC > 566)
Will it ever reach that number?
Leo..

My temp sensor show various levels of temperature and that number was based on the last read from using the sensor alone.

This code (from your other post) is working, but not very accurate.
Instable and coarse (~0.5C steps).

int val;
int tempPin = 1;

void setup()
{
Serial.begin(9600);
}
void loop()
{
val = analogRead(tempPin);
float mv = ( val/1024.0)*5000;
float cel = mv/10;
float farh = (cel*9)/5 + 32;

/*Serial.print("TEMPRATURE = ");
Serial.print(cel);
Serial.print("*C");
Serial.println();
delay(1000);

/*uncomment this to get temperature in farenhite */

Serial.print("TEMPRATURE = ");
Serial.print(farh);
Serial.print("*F");
Serial.println();
delay(1000);
}

The code from your original post was stable and had ~0.1C steps.
It only needed a few changes.
Stick with one post/code, so we can help you.
Leo..

Here is some example code I wrote.
It uses the internal reference and smoothing.
Ignore the LCD shield parts.
Connect the sensor to A1.
Set serial monitor to 115200
Leo..

// TMP35 or TMP36 temp sensor connected to Analogue input A1, 3.3volt and ground
// or LM35 temp sensor connected to A1, 5volt and ground
// temp range ~2C to ~105C
// display on serial monitor and/or LCD
// for a TMP36 (-40C to ~55C), change line 45 to:   tempC = total * Aref * 0.1 / numReadings - 50.0;

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // your LCD pins could be different
byte ledPin = 10; // backlight pin
const byte numReadings = 32; // number of readings for smoothing (max 64)
int readings[numReadings]; // readings from the analog input
byte index = 0; // index of the current reading
unsigned int total = 0; // running total
int inputPin = A1; // the pin that the sensor is connected to
float Aref = 1.0759; // change this value to the actual Aref voltage of ---YOUR--- Arduino (1.0 - 1.2), or adjust to get accurate readings
float tempC; // Celcius
float tempF; // Fahrenheit

void setup() {
  //analogWrite(ledPin, 200); // optional dimming
  analogReference(INTERNAL); // use the internal ~1.1volt reference | change (INTERNAL) to (INTERNAL1V1) for a Mega
  Serial.begin(115200); // ---set serial monitor to this value---
  lcd.begin(16, 2); // shield with 2x16 characters
  lcd.print("Thermometer"); // info text
  lcd.setCursor(0, 1); // second row
  lcd.print("0-100 Celcius");
  for (index = 0; index < numReadings; index++) { // fill the array for faster startup
    readings[index] = analogRead(inputPin);
    total = total + readings[index];
  }
  index = 0; // reset
  delay(2000); // info display time
}

void loop() {
  total = total - readings[index]; // subtract the last reading
  readings[index] = analogRead(inputPin); // one unused reading to clear ghost charge
  readings[index] = analogRead(inputPin); // read from the sensor
  total = total + readings[index]; // add the reading to the total
  index = index + 1; // advance to the next position in the array
  if (index >= numReadings) // if we're at the end of the array
    index = 0; // wrap around to the beginning

  // convert value to temp
  tempC = total * Aref * 0.1 / numReadings; // value to celcius conversion
  tempF = tempC * 1.8 + 32; // Celcius to Fahrenheit conversion

  // print to LCD
  if (total == 1023 * numReadings) { // if overflow
    lcd.clear();
    lcd.print("---TOO HOT---");
  }
  else {
    lcd.clear();
    lcd.print(tempC, 2); // two decimal places
    lcd.setCursor(6, 0); // position 6, first row
    lcd.print("Celcius");
    lcd.setCursor(0, 1); // second row
    lcd.print(tempF, 1); // one decimal place
    lcd.setCursor(6, 1); // position 6, second row
    lcd.print("Fahrenheit");
  }

  // print to serial monitor
  Serial.print("Raw average = ");
  Serial.print(total / numReadings);
  if (total == 1023 * numReadings) {
    Serial.println("  ----too hot----");
  }
  else {
    Serial.print("   The temperature is  ");
    Serial.print(tempC, 2);
    Serial.print(" Celcius  ");
    Serial.print(tempF, 1);
    Serial.println(" Fahrenheit");
  }

  delay(1000); // use a non-blocking delay when combined with other code
}