Skip to content

Instantly share code, notes, and snippets.

@PatrickHallek
Last active January 31, 2021 14:40
Show Gist options
  • Save PatrickHallek/62208f82479bfa6e8cd30c7bd316bc7a to your computer and use it in GitHub Desktop.
Save PatrickHallek/62208f82479bfa6e8cd30c7bd316bc7a to your computer and use it in GitHub Desktop.
#define inputPin 5 // PIR Sensor
#define outputPin 4 // Relay
#define switchPin 2 // Switch button state
int val = 0;
bool motionState = false;
int timeBuffer = 6; // in seconds
int measurementInterval = 100; // in milliseconds
int i = 0;
int switchState = 0;
void setup() {
Serial.begin(115200);
Serial.println("\n Starting");
pinMode(inputPin, INPUT);
pinMode(switchPin, INPUT);
pinMode(outputPin, OUTPUT);
}
void motionLightLoop() {
val = digitalRead(inputPin);
if(val == true) {
while(i < timeBuffer * 1000 / measurementInterval){
Serial.println("loop");
digitalWrite(outputPin, HIGH);
val = digitalRead(inputPin);
if(val == true) {
Serial.println("reset");
digitalWrite(outputPin, HIGH);
i = 0;
}
delay(measurementInterval);
i++;
}
}
Serial.println("End");
digitalWrite(outputPin, LOW);
i = 0;
}
void loop() {
switchState = digitalRead(switchPin);
if(switchState == true) {
motionLightLoop();
} else {
digitalWrite(outputPin, HIGH);
}
delay(measurementInterval);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment