In Blink without Delay example, I found out that the millis() function will reset its value each 50 days. Does that mean after 50 days, the LED might not blink anymore.
So I use fmod function to overcome this disadvantage.
But when I simulated the circuit with Proteus 78.0. The LED blinked each 3(s), not 1(s). Why? I haven't checked in practice yet.
Here is the code:
// constants won't change. Used here to set a pin number :
const int ledPin = 13; // the number of the LED pin
// Variables will change :
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
unsigned long checkingtime =0;
checkingtime= fmod (currentMillis,interval);
if ( checkingtime == 0) {
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
} else
{
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}