Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <avr/interrupt.h>
  2. #include <avr/sleep.h>
  3. #include <avr/power.h>
  4. #include <avr / wdt.h>
  5.  
  6.  
  7.  
  8. int ledPin = 13; // LED connected to digital pin 13
  9. int sleepPin = 12; // active LOW, ground this pin momentary to sleep
  10. int interruptPin = 10; // LED to show the action of a interrupt
  11. int wakePin = 2; // active LOW, ground this pin momentary to wake up
  12. int sleepStatus = 0; // variable to store a request for sleep
  13.  
  14.  
  15. ISR(TIMER1_OVF_vect)
  16. {
  17. Serial.println("------------timer1 overflow------------");
  18. }
  19.  
  20. void setup()
  21. {
  22. Serial.begin(9600);
  23. pinMode(ledPin, OUTPUT); // sets the digital pin as output
  24. pinMode(interruptPin, OUTPUT); //
  25. pinMode(sleepPin, INPUT_PULLUP); // sets the digital pin as input
  26. pinMode(wakePin, INPUT_PULLUP);
  27. attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
  28. /* Normal timer operation.*/
  29. TCCR1A = 0x00;
  30. TCNT1=0x0000;
  31. TCCR1B = 0x05;
  32. TIMSK1=0x01;
  33. }
  34.  
  35.  
  36.  
  37. void sleepNow() // here we put the arduino to sleep
  38. {
  39.  
  40. set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
  41. sleep_enable(); // enables the sleep bit in the mcucr register
  42. power_timer1_enable();
  43. attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
  44. // wakeUpNow when pin 2 gets LOW
  45. sleep_mode(); // here the device is actually put to sleep!!
  46.  
  47. sleep_disable(); // first thing after waking from sleep:
  48. // disable sleep...
  49. detachInterrupt(0); // disables interrupt 0 on pin 2 so the
  50. // wakeUpNow code will not be executed
  51. // during normal running time.
  52. delay(1000); // wat 2 sec. so humans can notice the
  53. // interrupt.
  54. // LED to show the interrupt is handled
  55. digitalWrite (interruptPin, LOW); // turn off the interrupt LED
  56. Serial.println("----------desligou o led------");
  57.  
  58. }
  59.  
  60.  
  61.  
  62. void wakeUpNow() // here the interrupt is handled after wakeup
  63. {
  64. //execute code here after wake-up before returning to the loop() function
  65. // timers and code using timers (serial.print and more...) will not work here.
  66. digitalWrite(interruptPin, HIGH);
  67. Serial.println("----------ligou o led-------");
  68. }
  69.  
  70.  
  71.  
  72.  
  73. void loop()
  74. {
  75. digitalWrite(ledPin, HIGH); // sets the LED on
  76. delay(1000); // waits for a second
  77. digitalWrite(ledPin, LOW); // sets the LED off
  78. delay(1000); // waits for a second
  79. sleepStatus = digitalRead(sleepPin); // read sleep pin here. only active
  80. //when blink led is off.
  81. if (sleepStatus == LOW) { // start to put the device in sleep
  82. sleepNow(); // sleep function called here
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement