#include <avr/sleep.h> #include <avr/interrupt.h> #include <RCSwitch.h> const int sensor = 3; RCSwitch mySwitch = RCSwitch(); void sleep() { GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts PCMSK |= _BV(PCINT3); // Use PB3 as interrupt pin ADCSRA &= ~_BV(ADEN); // ADC off set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) sei(); // Enable interrupts sleep_cpu(); // sleep cli(); // Disable interrupts PCMSK &= ~_BV(PCINT3); // Turn off PB3 as interrupt pin sleep_disable(); // Clear SE bit ADCSRA |= _BV(ADEN); // ADC on sei(); // Enable interrupts } ISR(PCINT0_vect) { } void initADC() { ADMUX = (1 << ADLAR) | (0 << REFS1) | (0 << REFS0) | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0); ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0); } void setup() { initADC(); pinMode(sensor, INPUT_PULLUP); mySwitch.enableTransmit(1); mySwitch.setProtocol(1); mySwitch.setPulseLength(401); mySwitch.setRepeatTransmit(5); } void loop() { ADCSRA |= (1 << ADSC); while (ADCSRA & (1 << ADSC) ); if (ADCH > 180) { int state; state = digitalRead(sensor); if (state == HIGH){ mySwitch.send(10107000, 24); //On delay(2000); sleep(); } else{ mySwitch.send(10108000, 24); //Off delay(2000); sleep(); } } else { mySwitch.send(10109000, 24); //low battery code delay(2000); sleep(); } }
Is this code any good??
It suppose to be a door sensor that should send the state signal and then goes to sleep and wakes again when the interrupt pin changes.
I am not able to send the close command when i shut the door.
please help me :)