Attiny85 Restarts After Coming out of Sleep and I Don't Know Why

I don't completly understand how sleep works and I have been trying to figure it out but I haven't been able to find a good refrence or a forum that describes the issues I have run into. I wrote a basic code that blinks a light every second and goes to sleep instead of delaying. I wake up from this sleep with a interrupt supllied by a watchdog timer. Here is my code.

#include <avr/sleep.h>
#include "watchdogHandler.h"
#include <avr/wdt.h>

const int ledPin = PB3;

void setup() {
  wdt_disable();
  pinMode(ledPin, OUTPUT);
}


void loop() {

  digitalWrite(ledPin, LOW);
  goSleep();
  digitalWrite(ledPin, HIGH);
  delay(1000);

}



void goSleep() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA &= ~(1<<ADEN); //Disable ADC
  setup_watchdog(6); //Setup watchdog to go off after 1sec
  sleep_mode(); //Go to sleep! Wake up 1sec later.
  ADCSRA |= (1<<ADEN); //Enable ADC
}

I would expect the led to blink but instead it is always on. This suggusts that the digitalWrite to low after the sleep isn't run at all. With some experimentation, I realized that after it wakes up from each sleep it resets the code entirely and runs the setup again. I figured this out by running this code (I turned the led on in the setup):

#include <avr/sleep.h>
#include "watchdogHandler.h"
#include <avr/wdt.h>

const int ledPin = PB3;

void setup() {
  wdt_disable();
  pinMode(ledPin, OUTPUT);
  digitalWritee(ledPin, HIGH);
  delay(3000);
}


void loop() {

  digitalWrite(ledPin, LOW);
  goSleep();
  //digitalWrite(ledPin, HIGH);
  //delay(1000);

}



void goSleep() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA &= ~(1<<ADEN); //Disable ADC
  setup_watchdog(6); //Setup watchdog to go off after 1sec
  sleep_mode(); //Go to sleep! Wake up 1sec later.
  ADCSRA |= (1<<ADEN); //Enable ADC
}

This causes the led to blink HIGH for 3 seconds and low for one. I think this sugguests that the watchdog is working and stopping the sleep, because it is waking up, but it keeps restarting the whole code after it wakes up. My understanding of this was that after it wakes up from sleep it would continue the code where it left off. Am I wrong? If so how does it work? Does the set_sleep_mode() affect anything? Does anyone know a way to acomplish what I want? Please let me know if you need any more information.

Here is an outstanding tutorial on sleep modes. It is for the ATmega328, but most of it applies to the ATtinys.

Post a link to "watchdogHandler.h".

jremington:
Here is an outstanding tutorial on sleep modes. It is for the ATmega328, but most of it applies to the ATtinys.

Post a link to "watchdogHandler.h".

That is the Github link. Is that what you meant?

If that link is where you obtained "watchdogHandler.h", then that would be the appropriate link.

You might want to study the docs and examples for that library. You evidently forgot to include the required ISR, as described in those docs. But by all means, work through Gammon's tutorial and you will have a much better understanding of the topic.

ISR(WDT_vect) {

jremington:
Here is an outstanding tutorial on sleep modes. It is for the ATmega328, but most of it applies to the ATtinys.

Post a link to "watchdogHandler.h".

The tutorial is very informative but I don't think it answers my question. After the Attiny85 wakes up, does it restart the code? Thanks for pointing out the ISR(WDT_vect) I noticed the turtorial you linked had that. I assumed it wasn't required. I'll make sure to add it

I assumed it wasn't required.

The worst possible assumption! Interrupts require Interrupt Service Routines (ISRs). You have now seen what happens when you leave them out.

The tutorial gives several examples of appropriate ISRs.

What I found odd is that even without the ISR function defined, the attiny was still woken up after the approprate amount of time. The only thing that I found to be not working was the fact that the program restarted after waking up.

I think I will be able to use the torturial for the project I have in mind, but I want to understand how it works.

The only question I have left is: Is the program supposed to restart after waking up from sleep? Or is the program supposed to continue where it left off?

If you don't know the answer I bet I could dig for it in the Github page, but I am not the best coder and the code on the Github isn't very well documented so I would rather not search for it there.

P.S

Thank you, you have been very helpful jremington

If no ISR is coded and declared for an active interrupt, the Arduino reboots every time the interrupt occurs. That is the default behavior.

We strongly recommend that beginners put off experimenting with or attempting to implement interrupts.

Interrupts almost always cause more problems than they solve, and mastering the technique requires a great deal of study, as well as fairly complete knowledge of the detailed behavior of the microprocessor. In other words, study the processor data sheet very carefully before you try.

That answers my question perfectly. Thank you so much! I don't consider myself a complete beginer. I started doing Arduino projects about five years ago. But, perhaps implementing interupts into my code is a little ambitious but that doesn't mean I won't try. Thanks again!