Schupp

Taster wdt

Jan 31st, 2021 (edited)
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <avr/sleep.h>
  3. #include <avr/wdt.h>
  4.  
  5. const byte b4 = 2; //Button 4
  6. ButtonKing button4(b4, true);
  7. int ticks = 0;
  8. void setup()
  9. {
  10.   Serial.begin(115200);
  11.   pinMode(LED_BUILTIN, OUTPUT);
  12.   digitalWrite(13, LOW);
  13.   pinMode(b4, INPUT);
  14. }
  15.  
  16. void loop()
  17. {
  18.   button4.isClick();
  19. }
  20.  
  21. void doubleclick4()
  22. {
  23.   enter_sleep();
  24.   //Serial.println("Button 4 doubleclick.");
  25. }
  26.  
  27. void enter_sleep(void)
  28. {
  29.   /*
  30.     display.setSegments(OFF);
  31.  
  32.     tone(speakerPin, 2000, 1000);
  33.     delay(1000);
  34.     noTone(speakerPin);
  35.   */
  36.  
  37.   attachInterrupt(digitalPinToInterrupt(b4), PINisr, FALLING);
  38.  
  39.   /* Arduino schlafen legen */
  40.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  41.   sleep_enable();
  42.   sleep_mode();
  43.   sleep_disable();
  44.  
  45.  
  46.  
  47.   // nach dem wecken
  48.   if (ticks == 5) { //5*125ms Watchdog Ticks
  49.     wdt_disable();
  50.     ticks=0;
  51.     beep();
  52.   }
  53. }
  54.  
  55. void beep(void) {
  56.   Serial.println("test");
  57. }
  58.  
  59. void PINisr(void)
  60. { // ISR fuer Pin 2
  61.   // detach Interrupt, damit er nur einmal auftritt
  62.   detachInterrupt(digitalPinToInterrupt(b4));
  63.   wdt_start();
  64. }
  65.  
  66. ISR(WDT_vect) {
  67.   if (!digitalRead(b4))
  68.     ticks++;
  69.   else {
  70.     ticks = 0;
  71.     wdt_disable();
  72.   }
  73. }
  74.  
  75. void wdt_start(void) {
  76.   cli();
  77.   wdt_reset();
  78.   WDTCSR |= (1 << WDCE) | (1 << WDE);
  79.   WDTCSR = (1 << WDIE) | (0 << WDE) | (1 << WDP1) | (1 << WDP0); // 125ms Interrupt
  80.   sei();
  81. }
Add Comment
Please, Sign In to add comment