Schupp icon

Taster wdt

Schupp | PRO | 01/31/21 08:53:38 PM UTC (Edited) | 0 ⭐ | 564 👁️ | Never ⏰ | []
Arduino |

1.36 KB

|

None

|

0 👍

/

0 👎

#include <Arduino.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
 
const byte b4 = 2; //Button 4
ButtonKing button4(b4, true);
int ticks = 0;
void setup()
{
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(13, LOW);
  pinMode(b4, INPUT);
}
 
void loop()
{
  button4.isClick();
}
 
void doubleclick4()
{
  enter_sleep();
  //Serial.println("Button 4 doubleclick.");
}
 
void enter_sleep(void)
{
  /*
    display.setSegments(OFF);
 
    tone(speakerPin, 2000, 1000);
    delay(1000);
    noTone(speakerPin);
  */
 
  attachInterrupt(digitalPinToInterrupt(b4), PINisr, FALLING);
 
  /* Arduino schlafen legen */
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode();
  sleep_disable();
 
 
 
  // nach dem wecken
  if (ticks == 5) { //5*125ms Watchdog Ticks
    wdt_disable();
    ticks=0;
    beep();
  }
}
 
void beep(void) {
  Serial.println("test");
}
 
void PINisr(void)
{ // ISR fuer Pin 2
  // detach Interrupt, damit er nur einmal auftritt
  detachInterrupt(digitalPinToInterrupt(b4));
  wdt_start();
}
 
ISR(WDT_vect) { 
  if (!digitalRead(b4)) 
    ticks++;
  else {
    ticks = 0;
    wdt_disable();
  }
}
 
void wdt_start(void) {
  cli();
  wdt_reset();
  WDTCSR |= (1 << WDCE) | (1 << WDE);
  WDTCSR = (1 << WDIE) | (0 << WDE) | (1 << WDP1) | (1 << WDP0); // 125ms Interrupt
  sei();
}

Comments