NickGammons led/sleep/watchdog code, timer setup kills sleep function? why?

Hello all,

ATtiny 85 using Arduino sketch environmen. to write the code and download via tinyusb programmer from sparkfun;
I started with Nick Gammons code for the led blink on for 2 seconds, sleep, then watchdog wake after 8 seconds. I changed the pin to #define LED 3 for an ATtiny85 - works perfect. I now need to use the timer/counter 0 and timer counter 1. when I add this code (previously known to work in another project) this LED stops working. Code below, I commented out the timer code in startup() for now. Interestingly if I setup the top portion for timer/counter 1 the LED on stays on for less than a seconds, adding timer/counter 0 kills it completely.

My question is can I not use the timer/counters if I’m using sleep and the watchdog in the same code for this part ?

I’m quite new, and any information would be appreciated. Thanks guys. BTW the PROGMEM data is what I’ll be feeding into the PWMs.

#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/power.h>    // Power management
#include <util/delay.h>   // Adds delay_ms and delay_us functions
#define adc_disable()  (ADCSRA &= ~(1<<ADEN))

#define LED 3


// Audio encoded as unsigned 8-bit, 8kHz sampling rate
PROGMEM const unsigned char hello[]= {
 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
 };

unsigned int hello_len = 24;
unsigned int p = 0;

// watchdog interrupt
ISR(WDT_vect) 
 {
 wdt_disable();  // disable watchdog
 }


void myWatchdogEnable(const byte interval) 
 {  
 MCUSR = 0;                          // reset various flags
 WDTCR |= 0b00011000;               // see docs, set WDCE, WDE
 WDTCR =  0b01000000 | interval;    // set WDIE, and appropriate delay

 wdt_reset();
 set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
 sleep_mode();            // now goes to Sleep and waits for the interrupt
 } 



void setup()  //setup function will only run once, after each powerup or reset of the Arduino board.
{

pinMode (LED, OUTPUT);

/*
 // Enable 64 MHz PLL and use as source for Timer1
 PLLCSR = 1<<PCKE | 1<<PLLE;     

 // Set up Timer/Counter1 for PWM output
 TIMSK = 0;                              // Timer interrupts OFF
 TCCR1 = 1<<PWM1A | 2<<COM1A0 | 1<<CS10; // PWM A, clear on match, 1:1 prescale
 GTCCR = 1<<PWM1B | 2<<COM1B0;           // PWM B, clear on match
 OCR1A = 128; OCR1B = 128;               // 50% duty at start

 // Set up Timer/Counter0 for 8kHz interrupt to output samples.
 TCCR0A = 3<<WGM00;                      // Fast PWM
 TCCR0B = 1<<WGM02 | 2<<CS00;            // 1/8 prescale
 TIMSK = 1<<OCIE0A;                      // Enable compare match
 OCR0A = 124;                            // Divide by 1000

 pinMode(4, OUTPUT);
 pinMode(1, OUTPUT);
*/


}  // end of setup



void loop()
{
 digitalWrite (LED, HIGH);  // awake
 delay (2000);    // ie. do stuff here
 digitalWrite (LED, LOW);  // asleep


 myWatchdogEnable (0b100000);  // 4 seconds

}  // end ofloop

It's the delay. Delay and millis use timer 0, so if you're mucking around with timer 0 you'll have to time your led differently.

Bill, you really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It's still not too late to edit your post and do this. ;)

sry about that old steve, hopefully this corrects it...

BillWaterson: sry about that old steve, hopefully this corrects it...

Thanks Bill. Much better. (And most of us did it when starting out, until someone like me mentioned it. :D)

Delta_G I do see that now. I removed the delay, it appears to be going into sleep but never coming out. So, does anyone know if the timer/counters 0 and 1 also affect the watchdog. I don't see anything in the datasheet that would indicate this.

BillWaterson: My question is can I not use the timer/counters if I'm using sleep and the watchdog in the same code for this part ?

Look at datasheet. I'm pretty sure all the timers stop in power-down mode.

Guys, I'm sorry , I need to properly phrase my question, because I'm just wasting your time. If anyone is still reading; I understand the timers stop in sleep, and the program makes it to the watchdog sleep call setup inside the ISR and it goes to sleep, but never wakes up. but the question is, does setting up the counters the way I did, have any effect on the watchdog ?

BillWaterson: Guys, I'm sorry , I need to properly phrase my question, because I'm just wasting your time. If anyone is still reading; I understand the timers stop in sleep, and the program makes it to the watchdog sleep call setup inside the ISR and it goes to sleep, but never wakes up. but the question is, does setting up the counters the way I did, have any effect on the watchdog ?

No, setting up the timers has no effect on the WDT.