Pages: [1]   Go Down
Author Topic: Sleep and Millis() - I need both?  (Read 1604 times)
0 Members and 2 Guests are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 126
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am creating a project that needs to save power - both battery and my house power.

I am creating a simple circuit using an ATMega328 + a few components to count the pulses from a power meter.

The device must run on batteries and about once per minute send a small payload via 2.4Ghz

My data packet I send is

TimeNowMS
TimePrevMS
PulseNow
PulsePrev

So based on this I can work out the power consumption in my house at the current time.

I have the basics working but I now want the device to sleep between updates over WIFI to save power.

I need to have the pulses being counted using interrupts even when sleeping.

Without any sleep the board is using 19ma - I want to reduce that by a huge amount - remember this is not an arduino board just the bare essentials to get it running with arduino code.

All of the arduino sleep modes of any value corrupt the value returned by Millis() since the timer is not running when the device is sleeping.

Normally I count the pulses between updates and also the duration in ms since the last update in ms.

I thought about a RTC but 1 second accuracy is not good enough - I need more accuracy !

Is there a way I can use a crystal to get ms? maybe using some sort of external circuit? but still readable by Arduino and all the while not using much power?

ANy thoughts appreciated

chris

Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 439
Posts: 23797
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not sleep, use RTC's square wave output as an interrupt to wake up more often than once a second? Check the time, if not ready to send yet go back to sleep, check again.
You have these choices with a DS1307 for example:
1 Hz, 1000mS
4 KHz, 0.250mS
8KHz, 0.125mS
32kHZ, 0.03125mS

4 KHz seems like plenty, keep track of where you are within the second using that.
Wake up increment time interval counter, see of power pulse occurred yet.
Waking up every 250uS, or 4000 clocks, I guess you'd stll be sleeping most of the time.

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Why not have the receiver timestamp the data?  Why does the sender have to do it?
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 126
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not sleep, use RTC's square wave output as an interrupt to wake up more often than once a second? Check the time, if not ready to send yet go back to sleep, check again.
You have these choices with a DS1307 for example:
1 Hz, 1000mS
4 KHz, 0.250mS
8KHz, 0.125mS
32kHZ, 0.03125mS

4 KHz seems like plenty, keep track of where you are within the second using that.
Wake up increment time interval counter, see of power pulse occurred yet.
Waking up every 250uS, or 4000 clocks, I guess you'd stll be sleeping most of the time.



This sounds interesting - have you actually used the interrupt to count time?

It does not matter when I update the data - 60 seconds, 61 seconds etc.... But I need to know how long since the last update to get accurate data durations for the pulse count.

Are you saying with this interrupt I could increment a volatile variable and simulate my own millis function?

Chris
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 126
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Why not have the receiver timestamp the data?  Why does the sender have to do it?


I guess this could work but if I lost a packet instead of a gap I would have corrupt data.

I will look into this option as well.

cheers

chris
Logged

Offline Offline
Edison Member
*
Karma: 4
Posts: 1729
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could perhaps use some logic ics to count while you sleep, not sure how much they would draw tho
you can use a clock signal fed into a ripple counter(or multiple for more precision using a faster clock), then get that parallel output into a piso shifter and when the arduino wakes, clock the data in with spi or bitbang it to recieve how many clock cycles have passed since it was asleep
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11179
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are power management libraries which can perform the sleep and maintain the 'millis()' value correctly, which simplifies your sketch.

All you really need to send is the count, and the length of time over which that count was collected. I don't see any point sending the previous values since the absolute numbers don't mean anything - it is only the change that is significant.

I suggest that you accumulate the count and duration and only reset when a successful upload has been completed. That way, if there is any difficulty with the upload you will lose granularity in the logged data but the total values will still be correct.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not have the receiver timestamp the data?  Why does the sender have to do it?
I guess this could work but if I lost a packet instead of a gap I would have corrupt data.

A sequence number solves the problem.  Including a few previous values will allow recovery from multiple dropped packets.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 126
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are power management libraries which can perform the sleep and maintain the 'millis()' value correctly, which simplifies your sketch.

I have not seen any - the only mode that maintained the millis function was SLEEP_MODE_IDLE when I did some testing.

But in this mode there is not much power savings since the processor wakes up every millisecond.

All other modes I tried the millis() had stopped.

chris
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11179
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have not seen any - the only mode that maintained the millis function was SLEEP_MODE_IDLE when I did some testing.

Have you tried searching? There are quite a few power management libraries around.

http://www.engblaze.com/low-power-libraries-for-arduino-control-sleep-with-single-function-calls/
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Full Member
***
Karma: 1
Posts: 126
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have not seen any - the only mode that maintained the millis function was SLEEP_MODE_IDLE when I did some testing.

Have you tried searching? There are quite a few power management libraries around.

http://www.engblaze.com/low-power-libraries-for-arduino-control-sleep-with-single-function-calls/

But do they give me access to a functioning millis() function?

I can't see how a library does as the chip disabled the timers?

I am at work so can not test until tonight!
Logged

Pages: [1]   Go Up
Jump to: