Some other things you may need to consider
- if the pin is already in the state you want to trigger for, the wake interrupt may not work, so wait for the pin to return to passive state before going to sleep
- I have found some issue with
detachInterrupt()which over time tends to barf things up
- reconnect after sleep depends on the state of the connection prior to sleep, if it was in a limbo state before the system tends to see this as the desired state for next wake
- while others seem to trust the auto-reconnect, I usually put my own connection monitoring in place and tear down the connection and manually reconnect, when I see odd behaviour
Just some personal views
Over time I came to the conclusion that it’s best to avoid
#define for constant definitions wherever possible, due to their “obscure” range of scope, sometimes interfering with otherwise unrelated code.
Hence I’d replace things like this
#define VERSIONNUMBER 9 // Increment this number each time the memory map is changed #define WORDSIZE 8 // For the Word size the number of bytes in a "word" #define PAGESIZE 4096 // Memory size in bytes / word size - 256kb FRAM #define HOURLYOFFSET 24 // First word of hourly counts (remember we start counts at 1) #define HOURLYCOUNTNUMBER 4064 // used in modulo calculations - sets the # of hours stored - 256k (4096-14-2) // First Word - 8 bytes for setting global values #define VERSIONADDR 0x0 // Where we store the memory map version number #define SENSITIVITYADDR 0x1 // Sensitivity for Accelerometer sensors #define DEBOUNCEADDR 0x2 // Where we store debounce in cSec or 1/10s of a sec (ie 1.6sec is stored as 16) #define RESETCOUNT 0x3 // This is where we keep track of how often the Electron was reset #define TIMEZONE 0x4 // Store the local time zone data // One byte is open here #define OPENTIMEADDR 0x5 // Hour for opening the park / store / etc - military time (e.g. 6 is 6am) #define CLOSETIMEADDR 0x6 // Hour for closing of the park / store / etc - military time (e.g 23 is 11pm) #define CONTROLREGISTER 0x7 // This is the control register for storing the current state //Second and Third words bytes for storing current counts #define CURRENTHOURLYCOUNT 0x8 // Current Hourly Count - 16 bits #define CURRENTDAILYCOUNT 0xC // Current Daily Count - 16 bits #define CURRENTCOUNTSTIME 0xE // Time of last count - 32 bits #define HOURLYPOINTERADDR 0x11 // Two bytes for hourly pointer
enum that has an explicit scope also allowing for the use of namespaces.
Any other kinds of constants and literals I’d just define as actual
#define HOURLYCOUNTOFFSET 4 // Offsets for the values in the hourly words #define HOURLYBATTOFFSET 6 // Where the hourly battery charge is stored // Finally, here are the variables I want to change often and pull them all together here #define SOFTWARERELEASENUMBER "0.59"
const int HOURLYCOUNTOFFSET = 4; // Offsets for the values in the hourly words const int HOURLYBATTOFFSET = 6; // Where the hourly battery charge is stored // Finally, here are the variables I want to change often and pull them all together here const char SOFTWARERELEASENUMBER = "0.59";