// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// print the NAME and the VALUE of variable "variableName"
// once every "timeInterval" milliseconds
#define dbgi(myFixedText, variableName,timeInterval) \
{ \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
}
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
const byte PIR_POWER = 7;
const byte PIR_READ = 12;
const byte ledPin = LED_BUILTIN;// the number of the LED pin
const byte PIR_MOTION_DETECTED = 11;
// make LED blink at 2 Hz
// 250 milliseconds on 250 milliseconds off = period 500 milliseconds
// 2 times on/off per second (2 * 500 ms = 1000 ms)
const unsigned long Interval = 250;
unsigned long myLedBlinkTimerVar;
int ambientLightLevel;
void setup () {
Serial.begin(115200);
Serial.println("Setup-Start");
digitalWrite(ledPin, LOW);
digitalWrite(PIR_MOTION_DETECTED, LOW);
pinMode (ledPin, OUTPUT);
pinMode (PIR_POWER, OUTPUT);
pinMode (PIR_READ, INPUT);
pinMode (PIR_MOTION_DETECTED, OUTPUT);
//digitalWrite (PIR_POWER, HIGH); // switch Power for PIR-Sensor on
// initialise variable named "myLedBlinkTimerVar"
// with actual value of millis()
myLedBlinkTimerVar = millis();
Serial.println("exiting Setup");
}
// -----------------------------------------------------------------------------
void loop () {
// TOP-OF-LOOP
ambientLightLevel = analogRead(A5);
//dbgi("00",ambientLightLevel,1000);
// check if it is dark
if (ambientLightLevel < 200) {
// when it IS dark
digitalWrite(PIR_POWER,HIGH); // switch on PIR-sensor
}
else { // = it is bright light
digitalWrite(PIR_POWER,LOW); // switch PIR-sensor OFF
}
//dbgi("01",digitalRead (PIR_READ),1000 );
// check if PIR-output has signal level "nothing detected"
if (digitalRead (PIR_READ) == LOW ) {
// when signal level REALLY is "nothing detected"
digitalWrite (ledPin, LOW);
digitalWrite (PIR_MOTION_DETECTED, LOW);
return; // jump back to TOP-OF-LOOP
}
// when the above if-condition evaluates to false
// which means the PIR-sensor has signal level "motion-detected"
// the code inside the if-condition is NOT executed => the return is NOT executed
// => code-execution goes on below
// check if PIR-sensor output-signal-level is "motion detected"
if (digitalRead(PIR_READ) == HIGH ) {
digitalWrite(PIR_MOTION_DETECTED,HIGH);
}
// check if more time than stored in constant "Interval" has passed by
if ( TimePeriodIsOver(myLedBlinkTimerVar,Interval) ) {
// when REALLY more time than "Interval" HAS passed by
digitalWrite (ledPin, ! digitalRead (ledPin)); // invert the logic state of the IO-pin
}
// long explanation for a short code
// The attention-mark is the NOT-operator. NOT means logical invertion
// NOT true = false
// NOT false = true
// as the attention-mark "!" is used as the NOT-operator
// !true = false
// !false = true
// applied to an IO-pin
// when digitalRead (ledPin) results in "1"
// !1 = 0 where "0" is the same as LOW
// when digitalRead (ledPin) results in "0"
// !0 = 1 where "1" is the same as HIGH
// !HIGH = LOW
// !LOW = HIGH
// the logical state always gets inverted
// this means with
// digitalWrite (ledPin, ! digitalRead (ledPin)); // invert the logic state of the IO-pin
// the LED is switched On/Off
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
Motion detected
PIR-Power
dark
bright