1

I am running my Arduino projects like this: enter image description here

My goal is to have this project run at 8mhz so that (1) battery last longer and (2) so that I am following the specs. I was able to burn a 8mhz bootloader to my Atmega328 chip thanks to this video: https://www.youtube.com/watch?v=fzZ2TR_mwgs

After following the video I am able to run sketches at 8mhz and they consume less current. But my original project crashes to the point where I have to burn a bootloader again. Even if I press the reset button it will not become responsive. I have discovered that this line of code is the one that makes my atmega328 chip unresponsive:

wdt_enable(WDTO_4S);  // #include <avr/wdt.h>

I find out about that because removing that line made my project work. That line enables waking up after 4 seconds. So I basically perform some work and go to sleep for 4 seconds. I then repeat the same steps forever in order to save battery. So before going to sleep I execute this:

// wake up from sleep after 4 seconds
wdt_enable(WDTO_4S);


// go to deep sleep
if (true)
{
    // BIG difference when in sleep
    // Diable ADC (analog to digital converter)
    ADCSRA &= ~(1 << 7);

    SMCR |= (1 << 2); // power down mode
    SMCR |= 1;        // enable sleep;

    // BOD DISABLE (big difference when in sleep only)
    MCUCR |= (3 << 5);                      // set both BODS and BODSE at the same time
    MCUCR = (MCUCR & ~(1 << 5)) | (1 << 6); // then set the BODS bit and clear the BOSE bit at the same time

    __asm__ __volatile__("sleep");
}

// this line will not execute....

Anyways how could I solve this problem? Should I keep the original project and just replace the crystal to 8mhz? I will then have to keep in mind that all delays will be half the speed correct?

2 Answers 2

1

Try power cycling the UNO when the boards stops responding. That should reset the watchdog settings. Just pressing reset doesn't disable the watchdog, as far as I know.

The bootloader doesn't know you enabled the watchdog, and if it takes more that 4 seconds, you get stuck in an infinite loop. You could try using the optiboot bootloader instead. That one resets the watchdog when it runs.

Thirdly, you could enable the watchdog interrupt. If you do that the MCU will still wake up after 4 seconds of sleep, but it will resume you code, instead of doing a reset. Doing this will make it so the bootloader only runs once.

1

Your delays are probably what is killing you. If you have delays longer than the WDT time that will cause a WDT timeout. Remember the Delay function does nothing but burn time, nothing else is processed. Go to the Millis or something like that as recommended many times in the Arduino forums.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.