Go Down

Topic: Example advise not using the Millis function. (Read 586 times) previous topic - next topic


johnwasser

Code: [Select]



const byte SensorPin = A0;    // select the input pin for the Soil moisture sensor


const unsigned long TwelveHourInterval = 12UL * 60UL * 60UL * 1000UL;


unsigned long StartTime;


void setup()
{
  Serial.begin(9600);
  StartTime = 0;
}


void loop()
{
  if (millis() - StartTime >= TwelveHourInterval)
  {
    StartTime += TwelveHourInterval;
    int sensorValue = analogRead(SensorPin);
    Serial.print("sensor = " );
    Serial.println(sensorValue);
  }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

dataoikogarden

i have to thank everyone for the help that give to me .
God bless you all.

PaulS

Quote
There is absolutely no time limit problem for you using millis() as long as you write your code correctly, and that's quite easy to do.
Suppose that you want to do something every 60 days...

Can you show me the easy code to do that, using millis()?
The art of getting good answers lies in asking good questions.

UKHeliBob

Quote
Suppose that you want to do something every 60 days...
pert will no doubt be along soon to reply, but timing 2 sequential 30 day periods is hardly difficult as you well know 
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

gfvalvo

#20
Nov 05, 2018, 05:45 pm Last Edit: Nov 09, 2018, 02:31 pm by gfvalvo
Record and handle millis() rollover manually using an extra uint32_t? You'd then be good for 2^64 milliseconds or more than 584 million years.

pert

Suppose that you want to do something every 60 days...
Yes, I know. That is why I wrote the "for you" in that sentence you quoted. I originally had written something about the time limit in there but decided that was just going to cause confusion since it clearly didn't apply for this application so I removed it before posting.

johnwasser

Suppose that you want to do something every 60 days...
Can you show me the easy code to do that, using millis()?
Sure!
Code: [Select]
const byte SensorPin = A0;    // select the input pin for the Soil moisture sensor


const unsigned long OneDayInterval = 24UL * 60UL * 60UL * 1000UL;


unsigned long StartTime;
unsigned DayCounter = 0;  // For intervals up to 65535 days (179 years)


void setup()
{
  Serial.begin(9600);
  StartTime = 0;
}


void loop()
{
  if (millis() - StartTime >= OneDayInterval)
  {
    StartTime += OneDayInterval;
    DayCounter++;
    if (DayCounter == 60)
    {
      DayCounter = 0;
      int sensorValue = analogRead(SensorPin);
      Serial.print("sensor = " );
      Serial.println(sensorValue);
    }
  }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

dataoikogarden

just to be sure . the code that you write it hasent overflow problem. Do i undestastand correctly ?

GoForSmoke

The unsigned subtraction used to tell the difference between 2 times (call them start and end) is not affected by rollover between the 2 times except when the difference is more than the unsigned variables can count. Unsigned long milliseconds can count to 49.71-some days as the longest interval.

To make it simple, look at a round 12 hour clock. It is unsigned. How many hours between start at 10 and end at 4? Put the hour hand on 4 and then subtract 10 by moving the hand backwards 10 places and the hour hand points to 6 which is the number of hours between 10 and 4.

But you don't want to use the Arduinos with resonator for clock unless you want to correct it a few minutes every day. At least get or make one (you can with instructions available) with a crystal. Best thing for day to day timing is to use an RTC or other clock.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

johnwasser

just to be sure . the code that you write it hasent overflow problem. Do i undestastand correctly ?
Neither of the sketches I wrote have an overflow problem.
The first one (reply #16) measures in milliseconds and works for intervals up to 49 days.
The second (reply #22) measures in days and works for intervals up to 179 years.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

GoForSmoke

We had one member whose name slips by me, he wrote beautiful code and did leave a 64-bit time library that uses unsigned long long (Arduino does have) to millis time what I figured as past the time when our sun goes red giant, 2 billion or so years from now.

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

dataoikogarden

Thank you very much for the reply. Have a noce day !

GoForSmoke

An RTC can be set up to make a pulse at chosen intervals, good for interrupt use.

When I time stamped logs it was with the system clock as an 8 char hex value (on the PC it was 32-bit Unix Seconds, on Arduino it was 32-bit millis, the 8 chars make a nice even left edge for the log) and then the event message. Some of the messages were point of sale info with time in them, others came from the video recorder and others from sensors in one job and NONE of the clocks were synchronized which didn't bother me at all as long as I had the system time to match device time messages I could show the time between any two log lines according to any of the clocks.
Your Arduino is less accurate than the RTC but making and processing RTC calls takes a good chunk of cycles while the Arduino drift over an hour is very small, perhaps less than the time to make an RTC time stamp.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

Go Up