Hi everyone!
I'm trying to put Millis instead of delays in my code, following the 'Bink without delay' example. I'm using it with a temperature sensor. This was my first code, with delays:
int tempC;
int tempPin = 2;
void setup()
{
Serial.begin(9600);
}
void loop()
{
tempC = analogRead(tempPin);
tempC = (5.0 * tempC * 100.0)/1024.0;
Serial.print(tempC);
delay (1000);
}
And this is the second code, with millis:
int tempC;
int tempPin = 2;
int tempState = LOW;
long previousMillis =0;
long interval = 1000;
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval2){
previousMillis = currentMillis;
if (tempState == LOW)
tempState = HIGH;
else
tempState = LOW;
digitalWrite(tempPin, tempState);
}
tempC = analogRead(tempPin);
tempC = (5.0 * tempC * 100.0)/1024.0;
Serial.print(tempC);
}
It compiles well, with no errors, but the temperature sensor doesn't work. It gives the first temperature value right, but after that it gets frozen and that value doesn't change.
I don't know what I'm making bad, please check out the codes and let me know it!