Created
November 28, 2017 15:17
-
-
Save circuitsenses/4dc61ef89324898dbe3182fe6714de04 to your computer and use it in GitHub Desktop.
PIR sesor with power saving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/sleep.h> | |
const int32_t led_pin = 13; | |
const int32_t pir_input_pin = 2; | |
int32_t pir_state = LOW; | |
volatile int32_t pinval = 0; | |
void setup() | |
{ | |
pinMode(led_pin, OUTPUT); | |
pinMode(pir_input_pin, INPUT); | |
Serial.begin(115200); | |
Serial.println("Armed"); | |
Serial.flush(); | |
attachInterrupt(digitalPinToInterrupt(pir_input_pin), pir_irq_handler, CHANGE); | |
} | |
void loop() | |
{ | |
// put your main code here, to run repeatedly: | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | |
sleep_enable(); | |
sleep_mode(); | |
sleep_disable(); | |
} | |
void pir_irq_handler() | |
{ | |
sleep_disable(); | |
pinval = digitalRead(pir_input_pin); | |
if(pinval == HIGH) | |
{ | |
digitalWrite(led_pin, HIGH); | |
if(pir_state == LOW) | |
{ | |
Serial.println("Target movement Detected"); | |
Serial.flush(); | |
pir_state = HIGH; | |
} | |
} | |
if(pinval == LOW) | |
{ | |
digitalWrite(led_pin, LOW); | |
if(pir_state == HIGH) | |
{ | |
Serial.println("Target Lost"); | |
Serial.flush(); | |
pir_state = LOW; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment