Go Down

Topic: Blink without Delay +fmod +proteus (Read 761 times) previous topic - next topic

Rain_In_River

Nov 18, 2015, 05:06 am Last Edit: Nov 18, 2015, 05:10 am by Rain_In_River
 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);
  }
}

GrooveFlotilla

#1
Nov 18, 2015, 10:11 am Last Edit: Nov 18, 2015, 10:22 am by Groove
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?
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

Rain_In_River

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??

GrooveFlotilla

#3
Nov 18, 2015, 04:27 pm Last Edit: Nov 18, 2015, 04:28 pm by Groove
And I don't understand why you used fmod to "overcome" a non-existent problem, so shall we call it quits?
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

Rain_In_River

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.

GrooveFlotilla

Forget fmod - it's a very simple function that is totally irrelevant in this context.
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

Rain_In_River

A very good explanation:
http://www.gammon.com.au/millis

Go Up