Hallo an alle
Ich habe mir vor ein paar Tagen in unsern Heizugskeller ein Abluftsystem eingebaut.
Es ist nur ein Lüfter (12 Volt) der ins Freie geht. Daraufhin habe ich mit einem Arduino, PIR Sensor und einem Relay eine kleine Schaltung gebaut. Durch Zufall bin ich dann auf den sketch gestossen und habe ihn dann aufgebaut.Der sketch läuft auch aber ich verstehe den Ablauf des sketch nicht. Kann mir mal Bitte einer sagen wie der sketch arbeitet. Wenn ich den Serial Montior öffne fängt er immer an von 30 runterzuzählen und hört dann bei 28 auf, und das gleiche geht von vorne los.
Danke Stefan
#include "Countimer.h" //Countdown timer library
const byte bootingLed = 4;
const byte notDetectedLed = 5;
const byte detectedLed = 6;
const byte buzzer = 7;
const byte relay = 8;
//TIMER FOR BUZZER INTERVALS
unsigned long previousMillis = 0;
const byte interruptPIRLow = 2; // Interrupt pin to trigger when motion sensor goes low
const byte interruptPIRHigh = 3; // Interrupt pin to trigger when motion sensor goes high
//TIMER BUZZES EVERY 30 SECONDS
const long INTERVAL = 1000;
Countimer onDelayTimer; //Initialialize a new instance of the Countimer object for on-delay (delay before relay is turned on)
Countimer offDelayTimer; //Initialialize a new instance of the Countimer object for off-delay (delay before relay is turned off)
void counter();
void bootSystem(); //Allows the PIR sensor sometime to boot up and start giving stable reading
void displayTimerCount(); //Displays current time left in the count down timer in serial monitor
void checkRelayStatus(); //Checks if relay is already on or off
void beepbuzzer(); //Beeps the buzzer
void switchRelayON();
void switchRelayOFF();
void setup(){
Serial.begin(9600);
pinMode(relay, OUTPUT);
pinMode(bootingLed, OUTPUT);
pinMode(detectedLed, OUTPUT);
pinMode(notDetectedLed, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(interruptPIRLow, INPUT_PULLUP);
pinMode(interruptPIRHigh, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPIRLow), checkRelayStatusOFF, FALLING); //Set an interrupt which is triggered when PIR sensor goes from High to low
attachInterrupt(digitalPinToInterrupt(interruptPIRHigh), checkRelayStatusON, RISING); //Set an interrupt which is triggered when when PIR sensor goes from low to high
onDelayTimer.setCounter(0, 0, 30, onDelayTimer.COUNT_DOWN, switchRelayON); //Set a 30 second countdown timer as a delay before switching on relay when motion is detected
onDelayTimer.setInterval(displayOnDelayTimerCount, 1000); //Display the time left in the countdown every 1 second
offDelayTimer.setCounter(0, 5, 00, offDelayTimer.COUNT_DOWN, switchRelayOFF); //Set a 30 second countdown timer as a delay before switching off relay when no motion is detected
offDelayTimer.setInterval(displayOffDelayTimerCount, 1000); //Display the time left in the countdown every 1 second
bootSystem(); //Delay for 60 seconds to allow PIR sensor to start giving stable values
}
void loop(){
unsigned long currentMillis = millis();
onDelayTimer.run(); //Initialize the onDelayTimer function
offDelayTimer.run(); //Initialize the offDelayTimer function
if( !offDelayTimer.isCounterCompleted()){
if (currentMillis - previousMillis >= INTERVAL) {
previousMillis = currentMillis;
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
}
}
}
//BOOT THE SYSTEM
void bootSystem(){
Serial.println("System is booting...Wait for 1 minute");
digitalWrite(bootingLed, HIGH); //Turn booting LED (blue LED) on while system is booting
delay(10000);
digitalWrite(bootingLed, LOW);
digitalWrite(notDetectedLed, HIGH); //Turn NOT Dected LED on (red led) after system finished booting
Serial.println("Booting Complete...");
}
/* =====================================================================
CHECK IF THE RELAY IS ON AND START ON-DELAY COUNTDOWN TIMER IF IT ISNT.
======================================================================*/
void checkRelayStatusON(){
Serial.println("Motion Detected. Low To High Interrupt Triggered");
digitalWrite(notDetectedLed, LOW);
digitalWrite(detectedLed, HIGH);
//CHECK IF OFF-DELAY COUNTDOWN TIMER WAS RUNNING WHILE MOTION WAS DETECTED AND STOP IT
if(!offDelayTimer.isCounterCompleted()){
Serial.println("Stopping OFF Delay Timer now...");
offDelayTimer.stop(); //STOP THE OFF DELAY TIMER
if(offDelayTimer.isStopped()){
Serial.println("Successfully Stopped OFF delay Timer");
}
else{
Serial.println("Error Stopping Off-Delay Timer");
}
}
//CHECK IF THE RELAY IS OFF AND TURN IT ON IF IT ISNT
if(digitalRead(relay)==LOW){
Serial.println("checkRelayStatusON Results: Relay is OFF...");
startOnDelayTimer();
}
else{
Serial.println("checkRelayStatusON Results: Relay is on ON... No further action");
}
}
//CHECK IF THE RELAY IS OFF AND START OFF DELAY COUNTDOWN TIMER IF IT ISNT.
void checkRelayStatusOFF(){
Serial.println("Motion Not Detected Anymore. High to low Interrupt Triggered");
digitalWrite(detectedLed, LOW);
digitalWrite(notDetectedLed, HIGH);
//CHECK IF ON-DELAY COUNTDOWN TIMER WAS RUNNING WHILE MOTION WAS NOT DETECTED ANYMORE AND STOP IT
if(!onDelayTimer.isCounterCompleted()){
Serial.println("Stopping ON Delay Timer now...");
onDelayTimer.stop(); //STOP THE OFF DELAY TIMER
if(onDelayTimer.isStopped()){
Serial.println("Successfully Stopped ON delay Timer");
}
else{
Serial.println("Error Stopping ON-Delay Timer");
}
}
if(digitalRead(relay)==HIGH){
Serial.println("checkRelayStatusOff Results: ON...");
startOffDelayTimer();
}
else{
Serial.println("checkRelayStatusOff Results: OFF...No further action");
}
}
//START 15 SECOND ON DELAY COUNTDOWN TIMER AND TURN ON RELAY AFTER COUNTDOWN GOES TO ZERO
void startOnDelayTimer(){
Serial.println("Starting OnDelay Timer...");
digitalWrite(notDetectedLed, LOW); //Change LED statuses
digitalWrite(detectedLed, HIGH);
offDelayTimer.stop();
onDelayTimer.start();
}
//START 30 SECOND OFF DELAY COUNTDOWN TIMER AND TURN OFF RELAY AFTER COUNTDOWN GOES TO ZERO
void startOffDelayTimer(){
Serial.println("Starting OffDelay Timer...");
digitalWrite(notDetectedLed, HIGH); //Change LED statuses
digitalWrite(detectedLed, LOW);
onDelayTimer.stop();
offDelayTimer.start();
}
//SWITCH ON RELAY AFTER COUNTDOWN
void switchRelayON(){
Serial.println("Turning relay on...");
digitalWrite(relay, HIGH);
if(digitalRead(relay)==HIGH){
Serial.println("Relay successfully turned ON");
}
else{
Serial.println("Switching ON relay FAILED...");
}
}
//SWITCH OFF RELAY AFTER COUNTDOWN
void switchRelayOFF(){
Serial.println("Turning relay off...");
digitalWrite(relay, LOW);
if(digitalRead(relay)==LOW){
Serial.println("Relay successfully turned OFF");
}
else{
Serial.println("Switching OFF relay FAILED...");
}
}
//DISPLAY COUNTDOWN TIMER
void displayOnDelayTimerCount()
{
Serial.print("Time To Switch On: ");
Serial.println(onDelayTimer.getCurrentTime());
}
void displayOffDelayTimerCount()
{
Serial.print("Time To Switch Off: ");
Serial.println(offDelayTimer.getCurrentTime());
}