After powering the Arduino board, an hour should be waited and then the lamp should be on for 10 seconds. After the next hour, again 10 seconds, and so on. Is that correct ?
Do you want to change the sketch someday to start it with a button ?
Do you want to add extra things to the sketch, so that a delay() should be avoided ?
Do you want to select the moment that the lamp is on ? For example directly when powering on the Arduino board, or after a few minutes.
Instead consider millis() as a ever elasping millisecond timer that rolls over every 70 odd days. You want to add your own unsigned long that you can set, synchronize to and compare to the value returned by millis(). Genrally speaking, and there are serveral very popular guides already on this topic, the convention is:
if (millis() - intervalStart >= Interval) {// <-- the compare. Interval in your case is READ
intervalStart = millis();// or intervalStart += Interval; <-- the synchronize
//do the thing...
}
1. When uploading of a sketch is done in the UNO, a "32-bit Time-Counter or Millis-Counter" is automatically started within the MCU and updated by 1 ms. The maximum time that the counter can accomodate is 232 ms.
2. The accumulated time of the Millis-Counter could be read at any time using the following command:
unsigned long presentMillis = millis();
3. Now, you can design your program as follows: (1) Keep your lamp at OFF position. (2) Read the present time of the Millis-Counter and keep it in the variable presentMillis. (3) Check that Millis-Counter has advanced by 1 hr (60601000 = 3600000 ms) from the value of the past Millis-Counter of Step-2. (4) If Step-4 is satisfied, make your lamp ON. (5) Wait for 10 seconds. (6) Goto Step-(1).
4. Convert the Text Codes of Step-3 into instructions as follows: (L of UNO works for your Lamp)
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, LOW); //lamp is OFF
unsigned long presentMillis = millis();
while(millis() - presentMillis <= 5000)//3600000UL)
{
; //do nothing and chcek again
}
digitalWrite(13, HIGH); //lamp is ON
delay(10000); //lamp is ON for 10 seconds
}
5. Check that L remains ON for 10 seconds and OFF for 5 seconds.
while(millis() - presentMillis <= 5000)//3600000UL)
{
; //do nothing and chcek again
}
Congratulations
You have just invented a method of using millis() that blocks program operation. You might just as well use delay()
Come on. You know better than that
This is a single tasking environment where the MCU has nothing to do other that blinking a Lamp/L. Let the MCU be in waiting for an hour.
@TheMemberFormerlyKnownAsAWOL
(3) Check that presentMillis of Step-2 is less than or equal to (<=) 1 hr (60UL60UL1000UL = 3600000 ms).
The above statement is changed to the following: (3) Check that Millis-Counter has advanced by 1 hr (60601000 = 3600000 ms) from the value of presentMillis of Step-2.
GolamMostafa:
The statement is changed to the following: (3) Check that Millis-Counter has advanced by 1 hr (60601000 = 3600000 ms) from the value of presentMillis of Step-2.
You may also seed some clues for the noobs, for when their code goes tango uniform.
TheMemberFormerlyKnownAsAWOL:
You may also seed some clues for the noobs, for when their code goes tango uniform.
Does the following statement seem to be better?
(3) Check that Millis-Counter has advanced by 1 hr (60601000 = 3600000 ms) from the value of the past Millis-Counter of Step-2
GolamMostafa:
Does the following statement seem to be better?
(3) Check that Millis-Counter has advanced by 1 hr (60UL60UL1000UL** = 3600000 ms) from the value of the past Millis-Counter of Step-2
Like I said, clues for later, when it goes tango uniform.
TheMemberFormerlyKnownAsAWOL:
You already used delay for the short delay; why not show some consistency?
No point in obfuscation for the noobs.
Then the delay() function could be used for both 1hr (3600000 ms) and 10 sec (10000) time delays. But, the poster has used both millis() and delay() functions for the time delays; so, I have wished to be with him. These are blocking codes and cause no harm for a single tasking environment.
TheMemberFormerlyKnownAsAWOL:
You wished to be with him, digging a deeper hole?
I remember the 1979's saying of a 60+ years old Training Manager (P.J. Arret) of GEC Kidsgrove (UK): "The difference of knowledge/experience base between you and I is more than 6"; so, I will supervise your steps and would try to slowly bring you up."
GolamMostafa:
I remember the 1979's saying of a 60+ years old Training Manager (P.J. Arret) of GEC Kidsgrove (UK): "The difference of knowledge/experience base between you and I is more than 6"; so, I will supervise your steps and would try to slowly bring you up."
Not sure if he is digging deeper or piling higher :D.