Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: Rain_In_River on Nov 18, 2015, 05:06 am

Title: Blink without Delay +fmod +proteus
Post by: Rain_In_River on Nov 18, 2015, 05:06 am
 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:

Quote
// 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);
  }
}
Title: Re: Blink without Delay +fmod +proteus
Post by: GrooveFlotilla on Nov 18, 2015, 10:11 am
Quote
Does that mean after 50 days, the LED might not blink anymore.
Does your life stop working when your mantelpiece clock flips over at midnight?

Quote
So I use fmod function
At what point did time become a floating-point quantity?
Title: Re: Blink without Delay +fmod +proteus
Post by: Rain_In_River on Nov 18, 2015, 04:07 pm
Does your life stop working when your mantelpiece clock flips over at midnight?
At what point did time become a floating-point quantity?
Not understand what you mean. I tried with arduino Uno v3 and the result is: It blink 1s, turn off 1s, then 1s later LED  is bright slightly, turn off 1(s) then is bright for 3s. Why??
Title: Re: Blink without Delay +fmod +proteus
Post by: GrooveFlotilla on Nov 18, 2015, 04:27 pm
And I don't understand why you used fmod to "overcome" a non-existent problem, so shall we call it quits?
Title: Re: Blink without Delay +fmod +proteus
Post by: Rain_In_River on Nov 19, 2015, 02:19 am
And I don't understand why you used fmod to "overcome" a non-existent problem, so shall we call it quits?
Sorry. I didn't think carefully. You're right.
OK. A lesson for me. Still do some research about my code so that I can understand fmod function.
Still many thing to learn.
Title: Re: Blink without Delay +fmod +proteus
Post by: GrooveFlotilla on Nov 19, 2015, 08:23 am
Forget fmod - it's a very simple function that is totally irrelevant in this context.
Title: Re: Blink without Delay +fmod +proteus
Post by: Rain_In_River on Nov 19, 2015, 10:14 am
A very good explanation:
http://www.gammon.com.au/millis