An easy to solve problem, but I'm not sure how to do it. When my sensor wakes up, it takes some accelerometer position readings, and then (is supposed to) go back to sleep. The issue lies with this piece of code (I suspect) where upon wake-up I use this line to detect changes in position. Afterwards the sensor refuses to go to sleep. Any ideas?
// This delay should not be necessary, but sometimes things don't seem to work right
// immediately coming out of sleep.
awake = ((accel.clearInterrupt() & LIS3DH::INT1_SRC_IA) != 0);
delay(500);
attachInterrupt(WKP, positionInterruptHandler, RISING);
delay(5000);
LIS3DHConfig config;
config.setPositionInterrupt(16);
bool setupSuccess = accel.setup(config);
Serial.printlnf("accelerometer set up", setupSuccess);
Serial.printlnf("entering verification mode");
state = VERIFICATION_STATE;
stateTime = millis();
break;
My code is largely based on his library. However, I wanted to adapt it a bit to suit my own needs; after a lot of work everything seems to work fine now except that the sensor cannot go back to sleep anymore now after it is woken up, due to
In theory you can do wake on position change. However, you'll wake on any position change, as you can't, for example, only wake when you change from position 3 to position 4.
However, I was unable to make it work. It seems to still wake when you move it, as well. Here's the code that should work, but doesn't:
Is there any way, with my above code, to have the sensor wake up on movement, do a position check (both works as of now) and then go back to sleep? It’s the going to sleep part that is giving me issues now.
If you set the accelerometer into position interrupt mode using setPositionInterrupt, make sure you set it back to wake on move mode using setLowPowerWakeMode before you try to calibrate prior to sleep.
I’m working on the same type of Electron GPS tracker setup and I was wondering if I could use the GPS speed as the factor to gauge whether to put the device to sleep or speed up or slow down the published rates.
Not sure if it’s a reliable method or not but just a thought for now.
Once it starts calibrating the accelerometer, it immediately prints ''resetting lastMovement int1_src=0x60'' and then stay turned on, until it IS moved in which case it sleeps and immediately wakes up again.
in the beginning of sleep_state would configure it correctly?
EDIT: Thanks, I don't fully understand it yet, but redirecting everything to RESET_STATE first works fine. Is this because the accelerometer configuration doesn't apply yet to statements in the same case i.e. the config must be in a case preceding the one System.sleep is in?
Correct, the states are not necessarily executed sequentially. It jumps to whatever you set the state variable to on the next loop. The first time through it executed the RESET_STATE, but it didn’t go back through that again after ACCELEROMETER_POSITION_STATE.
Ok thanks, got it! I had a hunch that’s how it worked, good to know for sure now. Then I suppose it also was a really bad idea of me to put the config.position in the same case as system.sleep.