Not yet tested, but I think it should be something like this:
Code: Select all
on System#Boot do
Monitor GPIO,0 // PIR sensor
Monitor GPIO,2 // Manual mode
endon
on GPIO#0=1 do
// PIR motion detected
if [Plugin#GPIO#Pinstate#2]=0
// Manual mode is not active, so we must act on the motion sensor
timerSet,1,1800 // 30 minutes timer
let,1,1 // Set variable #1 to indicate we had some motion detected
asyncevent,UpdateLightIntensity=[serre_lichtmeting#Analog]
endif
endon
on GPIO#2 do
// Change of "manual mode"
asyncevent,UpdateLightIntensity=[serre_lichtmeting#Analog]
endon
On serre_lichtmeting#Analog do
asyncevent,UpdateLightIntensity=%eventvalue1%
endon
on Rules#Timer=1 do
// 30 minutes has past with no motion detected
let,1,0 // Clear variable #1 to indicate no motion was detected.
asyncevent,UpdateLightIntensity=[serre_lichtmeting#Analog]
endon
on UpdateLightIntensity do
if [Plugin#GPIO#Pinstate#2]=0 and [int#1]=0
// Manual mode is not active, and no recent motion detected.
// So we must turn off the lights.
gpio,10,0
gpio,13,0
else
// Either manual mode is active, or recently detected motion
If %eventvalue1%<60
PWM,16,00
Elseif %eventvalue1%<450
let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
PWM,16,[int#1]
Else
PWM,16,1000
Endif
endif
endon
In short, there are 4 events which create a new event "UpdateLightIntensity" with as eventvalue the analog reading: [serre_lichtmeting#Analog]
- If PIR sensor detects something with "manual mode" disabled
- If "manual mode" changes
- When there is a new analog sample read
- 30 minutes after the last PIR motion detection.
At boot, I enable monitoring for the PIR sensor and the manual mode pin.
This allows me to receive events when those pins change.
Since we get an event immediately on PIR detection, the lights can be updated immediately and we don't have to wait for a new analog sample from the light intensity meter.
When motion is detected, a timer is being set to 30 minutes.
Every time a new motion is detected, this timer is being reset to 30 minutes.
Thus the timer will only expire 30 minutes after the last detected motion.
I also set a flag in variable #1 to keep track of the "recently motion detected" state we're in.
The actual work is being done when processing the "UpdateLightIntensity" event.
If manual mode is not active and no recent motion detected, turn off the lights.
Otherwise, change the PWM based on the measured light intensity.