0
\$\begingroup\$

I am trying to use a watchdog on Attiny13. With this code I get 70-100sec (it is not constant) sleeping time and 8sec wake up (pull X pin high) time:

#include <avr/interrupt.h>
#include <avr/sleep.h>


// Pin 4 has an mosfet connected on most Arduino boards.
int mosfet = 4;

int count = 0;

ISR(WDT_vect) {
  digitalWrite(mosfet, LOW);   // turn the mosfet off - keep it off
  count = count +1;
  if (count > 15)  //15*8s=120s=2min
  {
    // turn the mosfet on (HIGH is the voltage level) till the next interrupt
    digitalWrite(mosfet, HIGH);   
    count = 0;
  }

}

// the setup routine runs once when you press reset:
void setup() {             
  // initialize the digital pin as an output.
  pinMode(mosfet, OUTPUT);   

  // disable ADC (before power-off) // ADC uses ~320uA
  (ADCSRA &= ~(1<<ADEN));

  // turn the mosfet on (HIGH is the voltage level)
  digitalWrite(mosfet, HIGH);   
  delay(4000);
  count = 0;

  //SET timer for 8s
  WDTCR |= (1<<WDP3)|(0<<WDP2)|(0<<WDP1)|(0<<WDP0); // 8s

  // Enable watchdog timer interrupts
  WDTCR |= (1<<WDTIE);
  sei(); // Enable global interrupts 

  // Use the Power Down sleep mode
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);

  for (;;) {
    sleep_mode();   // go to sleep and wait for interrupt...
  }
}

void loop() {

}

I would like to send sleep for 2 minutes and wake up for 4sec.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.