@Postler, you invited me to this thread, so here are my first impressions
You are calling some functions that use delay()
from within a timer callback. This is no good practice and may have unwanted side effects.
1.011s Software Timer -> reportTheData() -> ColorFade() -> delay(delayColorFade * 1000)
When using timers, the timer callbacks mustnot use any delays anywhere near the timer periode.
Some unrelated hints:
If you use meaningful variable names you can avoid the need for comments like this
if (analogvalue /*light*/ <= light_threshold) {
When using lightValue
or ambientBrightness
instead of analogvalue
the meaning of the condition becomes clear without the need for the inline comment.
Similarly the use of val
and pirState
actually obscure their relation. If you use pirNewState
and pirState
the relation becomes obvious and also helps figuring out that the following code could be streamlined since it actually only should be executed when pirNewState != pirState
.
void reportTheData() {
int pirNewState = digitalRead(inputPin);
if (pirNewState == pirState) return; // no further action required
pirState = pirNewState;
setLed(pirState);
if (pirState) {
// send events and start color fading
}
}
Also try to stick with one kind of variable naming scheme (e.g. analogvalue
vs. light_threshold
vs. calibrateTime
…).
Instead of publishing four events in sequence (which may also result in a rate limit violation when timers get called back to back) like here
Particle.publish("PIR Motion", "Motion detected", PRIVATE);
pirState = HIGH;
setLED( pirState );
if (analogvalue /*light*/ <= light_threshold) {
Particle.publish("PIR Motion", String(analogvalue), PRIVATE);
Particle.publish("PIR Motion", String(light_threshold), PRIVATE);
Particle.publish("PIR Motion", "LED Streifen an", PRIVATE);
ColorFade();
}
Try to incorporate all data into only one event.
char data[128];
...
snprintf(data, sizeof[data]
, "Motion detected (%d <= %d -> LED Streifen %s)"
, lightValue, lightThreshold, (lightValue <= lightThreshold) ? "an" : "aus");
Particle.publish("PIR Motion", data, PRIVATE);
Also function names like reportTheData()
appear to be meaningful, but “the data” doesn’t really give away what kind of data will be reported.