weinerm21

Untitled

Jun 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. int ledPin = 13; // choose the pin for the LED
  2. int inputPin = 2; // choose the input pin (for PIR sensor)
  3. int pirState = LOW; // we start, assuming no motion detected
  4. int val = 0; // variable for reading the pin status
  5.  
  6. void setup() {
  7. pinMode(ledPin, OUTPUT); // declare LED as output
  8. pinMode(inputPin, INPUT); // declare sensor as input
  9.  
  10. Serial.begin(9600);
  11. }
  12.  
  13. void loop(){
  14. val = digitalRead(inputPin); // read input value
  15. if (val == HIGH) { // check if the input is HIGH
  16. digitalWrite(ledPin, HIGH); // turn LED ON
  17. if (pirState == LOW) {
  18. // we have just turned on
  19. Serial.println("Motion detected!");
  20. // We only want to print on the output change, not state
  21. pirState = HIGH;
  22. }
  23. } else {
  24. digitalWrite(ledPin, LOW); // turn LED OFF
  25. if (pirState == HIGH){
  26. // we have just turned of
  27. Serial.println("Motion ended!");
  28. // We only want to print on the output change, not state
  29. pirState = LOW;
  30. }
  31. }
  32. }
Add Comment
Please, Sign In to add comment