I finally got the accelerometer delivered, and I wanted to integrate it into the circuit and the code.
I first experimented with the accelerometer as a separate entity, and then once I had developed a working code for the accelerometer, integrated it into the main alarm box code.
The big challenge was not reading the accelerometer raw values or putting in a sensitivity to determine if it has moved, but for telling how LONG there has been movement, and only if there is movement for say 5 seconds does it trigger the alarm… if it is moving for longer than 5 seconds then it has been stolen! The difficult part is that there may be moments in there where there is no movement, e.g. for a half second it may stop but then start moving again, and I don't want that to 'reset' the movement time counter.
Here is the approach:
- I would store the "moved" value in an array, shifting the new one in each time I take a reading. Since I have the sensor reading at 1/4 of a second right now, to store the previous 5 seconds of "moved" would take 20 elements. Each time I read a sensor value, I drop the oldest one, and add the newest one to the array.
- I would then look to see if there is a more than 5 second period between two moments of movement (i.e. it has been potentially been moving for more than 5 seconds). As if I only stored 5 seconds of data and the alarm only goes off if there is more than 5 seconds of movement, it would only go off if the very first and the very last elements in the array were recorded as moved, I needed to store MORE than the movement time. I decided to store the previous 10 seconds of data, and look at any more than 5 second period in that.
- Then, I would loop through each element of the array and count how many are "moved" and how many are "not moved" between those two moments of movement, to see if it exceeds a threshold amount (say I allow 20% not movement before).
This way, there would be two tests:
- Does the time between the oldest "moved" and the newest "moved" exceed 5 seconds? (i.e. it has potentially been moving for more that 5 seconds)
- If so, does "moved" exceed the threshold value for that time period? (i.e. it has actually been moving)
I can thus exclude the two events I want to exclude:
- Two blips more than 5 seconds apart, but with little movement in between (below the threshold).
- Movement (even 100% of the time) of say 4 seconds, and then stop.
I would only include it as "stolen" if:
- There are two blips of movement more than 5 second apart (with some maximum time period between moments of movement)
- AND there has been movement above the threshold in between those two blips (e.g. 80% of the time it has been moving)