Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/interrupt.h>
- #include <avr/sleep.h>
- #include <avr/power.h>
- #include <avr / wdt.h>
- int ledPin = 13; // LED connected to digital pin 13
- int sleepPin = 12; // active LOW, ground this pin momentary to sleep
- int interruptPin = 10; // LED to show the action of a interrupt
- int wakePin = 2; // active LOW, ground this pin momentary to wake up
- int sleepStatus = 0; // variable to store a request for sleep
- ISR(TIMER1_OVF_vect)
- {
- Serial.println("------------timer1 overflow------------");
- }
- void setup()
- {
- Serial.begin(9600);
- pinMode(ledPin, OUTPUT); // sets the digital pin as output
- pinMode(interruptPin, OUTPUT); //
- pinMode(sleepPin, INPUT_PULLUP); // sets the digital pin as input
- pinMode(wakePin, INPUT_PULLUP);
- attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
- /* Normal timer operation.*/
- TCCR1A = 0x00;
- TCNT1=0x0000;
- TCCR1B = 0x05;
- TIMSK1=0x01;
- }
- void sleepNow() // here we put the arduino to sleep
- {
- set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
- sleep_enable(); // enables the sleep bit in the mcucr register
- power_timer1_enable();
- attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
- // wakeUpNow when pin 2 gets LOW
- sleep_mode(); // here the device is actually put to sleep!!
- sleep_disable(); // first thing after waking from sleep:
- // disable sleep...
- detachInterrupt(0); // disables interrupt 0 on pin 2 so the
- // wakeUpNow code will not be executed
- // during normal running time.
- delay(1000); // wat 2 sec. so humans can notice the
- // interrupt.
- // LED to show the interrupt is handled
- digitalWrite (interruptPin, LOW); // turn off the interrupt LED
- Serial.println("----------desligou o led------");
- }
- void wakeUpNow() // here the interrupt is handled after wakeup
- {
- //execute code here after wake-up before returning to the loop() function
- // timers and code using timers (serial.print and more...) will not work here.
- digitalWrite(interruptPin, HIGH);
- Serial.println("----------ligou o led-------");
- }
- void loop()
- {
- digitalWrite(ledPin, HIGH); // sets the LED on
- delay(1000); // waits for a second
- digitalWrite(ledPin, LOW); // sets the LED off
- delay(1000); // waits for a second
- sleepStatus = digitalRead(sleepPin); // read sleep pin here. only active
- //when blink led is off.
- if (sleepStatus == LOW) { // start to put the device in sleep
- sleepNow(); // sleep function called here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement