nRF8001 and Delay

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
zylezuin
 
Posts: 3
Joined: Thu Mar 27, 2014 10:25 pm

nRF8001 and Delay

Post by zylezuin »

Howdy,
I want to make a BTLE device that sends sensor readings to my phone. I don't need the sensors updating constantly nor do I want to send the data constantly. Once a second is fine.

Originally my sensor code was something like:

Code: Select all

temp = read_temp();
Delay(1000);
But now with BTLEserial.pollACI(); I'm nervous about using the delay as I want the pollACI to fire as often as possible right? What's the best way to approach controlling the loop function of an arduino while allowing the BTLE to run unobstructed? Or whats the alternative to the delay function?

Thanks!

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: nRF8001 and Delay

Post by adafruit_support_mike »

Use the 'millis()' function to track the number of milliseconds since the last data chirp:

Code: Select all

long lastSendTime = 0;
const int delayPeriod = 1000;

void long () {
	long now = millis();
	
    if (( now - lastSendTime ) > delayPeriod ) {
        //	send data
        lastSendTime = now;
    }
}

User avatar
zylezuin
 
Posts: 3
Joined: Thu Mar 27, 2014 10:25 pm

Re: nRF8001 and Delay

Post by zylezuin »

Thanks mike!

Will I have an issue with these numbers getting too big? If this program runs for days or weeks could these longs overrun their allocation size?

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: nRF8001 and Delay

Post by adafruit_support_mike »

The clock does roll over after about 50 days.

There are formally correct ways to handle the rollover, but you'd be just as well off doing this:

Code: Select all

if ( now < lastSendTime ) {
	//  send data  <--- bonus packet!
	lastSendTime = now;
}
That gives you a hitch step every 4.3 million packets, which is probably acceptable.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”