I have a motion detector (PIR), a few LEDs and a toggle button on my breadboard, with a Particle Photon running it.
The button works with an interupt, and simple sets a boolean value.
attachInterrupt(SILENT_BOTTON_INTERRUPT, handleButtonStateISR, RISING);
Where SILENT_BOTTON_INTERRUPT is the pin number the button is connected it.
The code for that ISR is:
void handleButtonStateISR() {
Particle.publish(“EVENT”, “Button pressed”, PUBLIC);
isSilent = !isSilent;
}
My PIR was using the loop function and checking the state, but it seems Interrupts are better for the PIR. The PIR has no jumper on it, so it goes to HIGH when motion is detected and stays HIGH for 8 seconds after motion stops.
So I created two interrupts. One for when the pin goes HIGH, and one for it goes LOW.
attachInterrupt(PIR_PIN, handleMovementStart, RISING);
attachInterrupt(PIR_PIN, handleMovementStop, FALLING);
The two methods are:
void handleMovementStart() {
if(AllStill) {
if(isSilent==false) {
// Particle.publish(“Event”, “Movement Detected!”, PUBLIC);
buzzer.Beep(160, 1500);
}
}
AllStill = false; // All still is false if we have movement.
}
void handleMovementStop() {
AllStill = true; // All still is false if we have movement.
}
So, when we go HIGH, we set a boolean to false. And when we go LOW, we set the boolean to true.
I also have these values that get set in these functions as volatile.
// Setup global variables.
bool AllStill = true;
volatile bool isSilent = false;
But it’s failing.
When I make movement, nothing happens at all.
If I click the button, the board freezes. Breathing light stops at the brightness it was hen I pressed the button. Need to reset the board.
If I comment out the ‘FALLING’ attachInterrupt, and make movement, the boolean changes as expected, but never deactivates (As I commented out the ‘falling’ code).
And if I press the button, the board freezes again.
Can anyone spot the issue?