unsigned long current_time = 0;
unsigned long previous_time = 0;
byte time_interval = 30000;
void setup() {
Serial.begin(9600);
}
void loop() {
current_time = millis();
if ( current_time - previous_time > time_interval)
{
Serial.println("I am in if section");
previous_time = current_time;
}
Problem: if i set the data type of time_intervial variable to byte, then it comes right after 2 to 3 seconds into if block. It was supposed to come into if block after 30s.
If i set the data type of time_interval variable to int, then everything is fine. Whats wrong with byte data type???
This is safer. The timing will be delayed when the sketch is very busy, but the sketch does not clog up when delays are combined with millis. It is the right choice to blink a led and for a beginner:
previous_time = current_time;
This keeps the timing in sync with time. Use it only when needed, for example for a clock: