// there is no code for the servo yet
// there is no code for the servo yet
// there is no code for the servo yet
// there is no code for the servo yet
// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
// 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 MotionDetectedPin = 11;
const byte ledPin = LED_BUILTIN;// the number of the LED pin
const byte actionIndicator_Pin = 2;
unsigned long myActionTimerVar;
const unsigned long actionTimePeriod = 18000; // 18 seconds
boolean motionDetected = false;
void setup () {
Serial.begin(115200);
Serial.println("Setup-Start");
digitalWrite(ledPin, LOW);
digitalWrite(actionIndicator_Pin, LOW);
digitalWrite(MotionDetectedPin, LOW);
pinMode (ledPin, OUTPUT);
pinMode (PIR_POWER, OUTPUT);
pinMode (MotionDetectedPin, OUTPUT);
pinMode (actionIndicator_Pin, OUTPUT);
pinMode (PIR_READ, INPUT);
digitalWrite (PIR_POWER, HIGH); // switch Power for PIR-Sensor on
Serial.println("exiting Setup");
}
// -----------------------------------------------------------------------------
void loop () {
// TOP-OF-LOOP
Show_PIR_Output_state_with_LED();
// check if NO motion is (yet) detected
if (motionDetected == false) {
// when NO motion is yet detected
// check PIR-Output
if (digitalRead(PIR_READ) == HIGH) {
// when PIR-Output is HIGH (= motoin detected)
motionDetected = true;
myActionTimerVar = millis(); // store actual timestamp of millis()
digitalWrite(actionIndicator_Pin,HIGH);
}
}
// check if flag-variable "motionDetected" is true
if (motionDetected == true) {
// when variable motionDetected IS true
// check if more time than stored in constant "actionTimePeriod" has passed by
if ( TimePeriodIsOver(myActionTimerVar,actionTimePeriod) ) {
// when REALLY more time than "actionTimePeriod" HAS passed by
digitalWrite (actionIndicator_Pin, LOW);
motionDetected = false;
}
}
}
// 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
}
void Show_PIR_Output_state_with_LED() {
// reflect logic state of the PIR-Output to yellow LED
// check if PIR-Output is HIGH
if (digitalRead(PIR_READ) == HIGH) {
// when PIR-output is HIGH
digitalWrite(MotionDetectedPin,HIGH); // switch yellow LED ON
}
else { // PIR-Output is LOW
digitalWrite(MotionDetectedPin,LOW); // switch yellow LED OFF
}
// once every 1000 milliseconds print logic state of IO-pin PIR_READ
dbgi("PIR",digitalRead(PIR_READ),1000);
}Motion detected
Action