Arduino turn on LED with temperature from LM35

I’m trying to make the Arduino to turn on a LED if the number I put on the Serial is bigger than the temperature the LM35 is measuring. The LED always turn on and after a few seconds turn off, and I don’t know why. Can anyone help me?

const int LM35 = A0;
float temperatura;

void setup() {

Serial.begin(9600);
pinMode(13, OUTPUT);

}

void loop() {

temperatura = (float(analogRead(LM35))*5/(1023))/0.01;

Serial.print("Temperatura: ");
Serial.println(temperatura);
delay(2000);

if(Serial.available() > 0){

if(temperatura <= Serial.read()){
digitalWrite(13, HIGH);
}
else if(temperatura > Serial.read()){
digitalWrite(13, LOW);
}
}
}

Serial.read() returns a single character. I assume that you are putting digits in. They're going to come over as ascii so a '0' is 48, a '1' is 49, '2' is 50, all the way up to '9' at 58.

Unless it is very very hot in your room, those numbers will always be larger than the temperature in Celsius and unless it is fairly cool in the room those numbers will almost always be less than the temperature in Fahrenheit.

A common way to convert a single digit in ascii to a real number is to simply subtract '0' from it. Have a look at the ascii table and think math to figure out why that works.

num = Serial.read() - '0';

But that leaves you with another problem. Serial.read will only get you one digit at a time. And unless it is very cold in the room, a single digit number will surely be less than the temperature.

So you would have to read in all the characters first and store them in a char array and then use atoi to convert into a number. Have a look at the Serial Input Basics thread for some inspiration on how to use Serial more effectively.

I used Serial.parseFloat instead of Serial.read and worked. I unertood that Serial.parseFloat read as a float and Serial.read read the ASCII from each number you put.

Now i’m trying to send the temperature I want with this C program, bit it’s not working.

#include <stdio.h>
#include <windows.h>
#include <conio.h>

HANDLE hSerial;
wchar_t port[6] = {L"COM3"};

int main() {

hSerial = CreateFile(“COM3”, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
GetCommState(hSerial, &dcbSerialParams);
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
SetCommState(hSerial, &dcbSerialParams);

COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
SetCommTimeouts(hSerial, &timeouts);

float comando, temperatura;
int menu;

while (menu != 27){

printf("\n\nQual temperatura você deseja?\n");
scanf("%f", &comando);

float TxBuff [1];
unsigned int TotalBytesaEnviar = 12;
unsigned long BytesEnviados;

TxBuff [0 ] = comando;
WriteFile(hSerial, &TxBuff, TotalBytesaEnviar, &BytesEnviados, NULL);

float RxBuff [1];
unsigned int TotalBytesaReceber = 1;
unsigned long BytesRecebidos;

ReadFile(hSerial, &RxBuff, TotalBytesaReceber, &BytesRecebidos, NULL);

temperatura = RxBuff[0];

printf("\n\nTemperatura atual: %f\n", temperatura);

printf("\n\nTecle ESC para sair. Outra tecla para reiniciar");
menu = getch ();
}

CloseHandle (hSerial);
return 0;
}

I'm trying to use Serial.parseInt and it's not working too. Can you give me an example code with the method you said?

I did already give you a bunch of examples. Hit the link I posted.

Please be considerate enough to read the How to use this forum thread as well and start posting your code in code tags like you're supposed to.

I readed the text on the link, but didn't work for me. I tryed to use but it's not working well.

ok. If you want help with it then show us what you did and maybe someone can help explain where you went wrong. If I gave you some example I'd just copy it from that thread. But nobody can help you if all you're willing to give is:

I tryed to use but it's not working well.

You're going to have to participate here. Nobody is going to just write your code for you. We will help you get going but you've got to put in some effort here.