Is my code wrong?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define lm35 A0
#define red 10
#define green 9
#define blue 8

void setup() {
Serial.begin(9600);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
lcd.init();
lcd.backlight();
}

void loop() {
delay(2000);
int reading = analogRead(lm35);
float voltage = reading * (5.0 / 1024.0);
float temperatureC = voltage * 100;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print(" C");

lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperatureF);
lcd.print(" F");

delay(200);

float temp = temperatureC;
float temp_F = temperatureF;

Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" C");
Serial.print(temp_F);
Serial.println(" F");

if (temp > 0 && temp < 23) {
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
}
else if (temp <= 23 && temp < 33) {
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
}
else if (temp >= 33) {
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
}

delay(200);
}

YES, sure!
At least it inserted incorrectly.
Read the forum guidelines to see how to properly insert the code and some other good information on making a good post.

2 Likes

Yes, everything is wrong.
First, you should specify what is the problem from your point of view (what that code should do and what doesn't).
Second, you must edit your first post to enclose the whole code inside the "code tags" (see the button "<CODE/>" on the top button bar of the editor).

After you'll do those changes, we can say something to you.

2 Likes

Why bother?

I can't see any problems. At least it is structurally okay. Is there any problem that you're facing? Aren't you getting your desired result?

Did you mean ">= 23"?

code actually looks fine. but obviously it's not doing something that you expect it to

guessing, the value read from the lm35 is not changing

Not finding this (following) on the spec sheet, but from the internet:

"...One of the downsides of the [Texas Instruments LM35] is that it requires a negative bias voltage to read negative temperatures... The TMP36 by Analog Devices is very similar to the LM35 and can read temperatures from -40°C to 125°C without any external components..."

The sketch will yield these extremes...

Temperature: 0.00 C 32.00 F (at 0)
Temperature: 499.51 C 931.12 F (at 1023)

as it is written:

  int reading = analogRead(lm35);
  float voltage = reading * (5.0 / 1024.0);
  float temperatureC = voltage * 100;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

But should yield the LM35 extremes of

Temperature: -55.00 C -67.00 F (at 0)
Temperature: 150 C 302 F (at 1023)

(a simulation)

Since OP has not been heard from since it started, and has not even corrected his first post as requested by the forum rules, I think it is useless to continue this topic.

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