April Sunday 22

Bookmark and Share

Related Articles...

MILWAUKEE WI Arduino Meet Up Make Space


WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Arduino Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Raspberry Pi Meet Up Make Space


WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Raspberry Pi Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Internet of Things (IoT) Meet Up Make Space


WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Make Space Acquired!

Full Advanced Fabrication Facility, machine Shop, Welding, Electronics Lab Equipment Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Machine Repair, & Installation  - Tier 1 Business Support

Machine Technician Mechanic 20+ Years  Personal Experienced In Industrial Factory Machine Servicing, Rebuilding, Installation And Repair - Plus A 35+ Years Experienced Computer Engineer Service Technician Team Ready For Your Outsourced Market Advantage Needs!

Hartung Industrial - Member - CGI Science & Technology Group

Email :

COASMailBox @ gmail.com

2018 REVIEW | 'IoT & Embedded MAKER SPACE - MKE' Group


Arduino Time Intervals; Timming

2017 OCTOBER | by Gene Casanova

Senior Systems Engineer

Using Time Intervals

Processes progress by clockm ticks within any computer system, including an Arduino. Using the clock ticks, is the proficent method for timming process executions.

The logical method for using timming is by first saving a timestamp at a critcal moment in a process, and then later in the process, subtract a second point in time with the timestamp to get the interval time; the time passed since the timestamp.

Using Timming In A Process

When precision timming is not an issue in a program, use the "millis()" function for obtaining the number of milliseconds passed/elapsed since the Arduino microcomputer IC chip was powered.


uint32_t ts1 = millis();
// Some process code goes here.
uint32_t ts2 = millis();

// Output the elasped time in milliseconds:
Serial.println( ts2-ts1 );}	//	Basic math here.

When timming precision is crtical, use the "micros()" function; it returns the number of microseconds since the Arduino was powered, with a precision of 4 or 8 μsec, depending on the clock speed of the microcomputer.


uint32_t ts1 = micros();

// a process goes here.

uint32_t ts2 = micros();

// Output the elasped time in microseconds:
Serial.println( ts2-ts1 );

BEWARE, the examples above will work most of the time, but not always. The registers used to count milli- or microseconds, since the Arduino boot time, have a limited capacity of 32 bits. This means, when they reach their highest possible value (0xffffffff, or equally 4294967295) they will overflow and reset to zero.

If the overflow occurs anywhere outside the two calls to millis() or micros(), the code above will work fine, because ts2 will be greater than ts1 and the result of the subtraction will be correct.

If the overflow occurs between the first and the second call to millis() or micros(), then there is a problem. ts2 will not be greater than ts1, and simply subtracting ts2 - ts1 will yield a wrong result.

The overflow of the counters occurs once every 49.7 days for the milliseconds counter.

The microseconds counter overflows much more often: every 71.5 minutes!

It may not be always possible to tolerate a wrong calculation, even if it occurs seldom.

Calculate the time interval between those two timestamps as the sum of two sub-intervals: dt1 which is the interval between t1 and the overflow point (timestamp = 0), and dt2, which is the interval between 0 and t1. Then the total interval dt would be:

uint32_t dt= dt1 + dt2;

Calculate dt2 = t1 − 0; is t1:

uint32_t dt2 = t1;

Calculating dt1 is a bit more complicated, and requires to invert all the bits of t2, and then add +1 to it.

Add +1 to get the actual interval between t2 and 0:

uint32_t dt1 = 1 + ~t2;

Everything together:

uint32_t dt1 = 1 + ~t2;
uint32_t dt2 = t1;
uint32_t dt = dt1 + dt2;

Simplified to:

uint32_t dt = 1 + t1 + ~t2;

Next, discriminate the two cases:

uint32_t dt = t1 > t2 ? 1 + t1 + ~t2 : t2 - t1;

The statement above checks if t1 > t2 or not, and then performs the appropriate calculation.

Example Process

 * Example Interval Timer Process.
 * Example state machine.
const int ledPin =  LED_BUILTIN;	// the number of the LED pin.

int ledState = LOW;             // ledState used to set the LED

unsigned long previousMillis = 0;  // Used to save last time LED process updated

// A Constant Value:
const long interval = 1000;           // the Interval to do something void setup() { // set the digital pin as output: pinMode(ledPin, OUTPUT); } void loop() { // LOGIC: // Check if time passed to meet interval; IF the difference // between the current time and last time blinked the LED is bigger than // the interval to blink the LED. unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save last time blinked the LED previousMillis = currentMillis; // If LED Off, Turn It On and vice-versa: if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } // Set LED with the ledState of the variable: digitalWrite(ledPin, ledState); } }


Arduino Open Source Community member - Tier 1 Full Stack Developer

'IoT & Embedded MAKER SPACE - MKE'

CONACT to learn about a new group in METRO Area of WI & IL - Milwaukee, Racine, Kenosha, South Eastern WI And Northern IL, space and/or spaces - Forming Now!

We Have Industrial Zoned Making Space With Outfitted Machine Shop And Lab Space Available!  Multiple Dedicated Online Locations Available In Milwaukee 3rd Ward.

I am building a network of creative Makers/Doers!... Come Learn More, Join, Engage, Enjoy, Create A Solid Future Of Fun And Reward... Come Help..... Core Team Members Wanted, Or Simply be A Hangout Member Learning, Socializing/Networking... Start-Up Incubator Space, Students Tutoring Space, Retired... Hobbyists, Educators, Scientists, Researchers, Developers...

Mega Industrial Machines Building!!!!  AI Development.... Super Computing.... @ Tier 1 Level.  Locations looking to establish a network space, include Chicago IL, Melbourne Vic Australia.

Use The Technology Wisely & Keep It Simple

- Cheers!

Gene Casanova

Need More?   Need Help? ........Software, Network, System & Data Center Builder & Developer | Development Services Available - Freelance Small Jobs; To Outsourced Long Term Service Provider Contract Labor Available.

CGI Computer Wares | EST 1979

Send E-MailCONTACT: [click]@CGIComputerWares.com