Skip to content

Instantly share code, notes, and snippets.

@sorenmalling
Created April 29, 2021 09:59
Show Gist options
  • Save sorenmalling/2d8063ed0514e3227ff77619da017404 to your computer and use it in GitHub Desktop.
Save sorenmalling/2d8063ed0514e3227ff77619da017404 to your computer and use it in GitHub Desktop.
Motion Sensor
int nudgingLedPin = 13; // choose the pin for the LED
int pushButtonPin = 9;
int greenLedPin = 11; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int push = 0;
void setup() {
pinMode(nudgingLedPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pushButtonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
push = digitalRead(pushButtonPin);
if (val == HIGH) {
digitalWrite(nudgingLedPin, HIGH); // turn LED ON
if (pirState == LOW) {
Serial.println("Motion detected!"); // print on output change
pirState = HIGH;
}
} else {
digitalWrite(nudgingLedPin, LOW); // turn LED OFF
if (pirState == HIGH) {
Serial.println("Motion ended!"); // print on output change
pirState = LOW;
}
}
if (push == LOW) {
//val = LOW;
digitalWrite(nudgingLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
delay(2000);
digitalWrite(greenLedPin, LOW);
}
// hvis knappen bliver trykket
// sluk ledPin 13
// Tænd grøn led pin i X sekunder
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment