Hello, I read Gammon Forum : Electronics : Microprocessors : Power saving techniques for microprocessors and "Extend" Watchdog Timer Beyond 8 Seconds? - Project Guidance - Arduino Forum and based on them created my code to be used for sleeping for 60 seconds and then performing a run through sensor reading, printing their values and printing current time based on RTC DS1307.
1.
Apparently setting the myWatchdogEnable (0b100001); which should give exactly 8 seconds for 6 times = 48s and adding 0b100000 (4s) and 0b000111 (2s) should give me in total 54s, but as it turned out it produces "delays" of 60-61s. What is the reason of that mismatch?
I want to update the time every 60 seconds to change the minute, if there is a +/- 1 second I may end up with minute NOT incremented when it should be but after two ~60s periods which is not acceptable. Since it's not very precise can I somehow use DS1307 to perform the interrupt every 60 seconds while still staying in SLEEP_MODE_PWR_DOWN?
delay(10); was used because when powered from battery - initially there is no temp/humid reading, but suprisingly the reading from BMP180 and RTC is present(!). Unfortunately that must not be the case because it didn't help. It doesn't happen when powering from USB - all 4 readings are there after powering up. What may be the reason?
I got a USB error showing
The last time it happened to me was when I created a program with memory leak, but I suppose it's not the case here since I can upload new sketches with some problems (only after pressing reset button) and with memory leak Arduino froze completely.
Thank You in advance for any help!
#include <dht.h>
#include "U8glib.h"
#include <Wire.h>
#include <BMP180.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
BMP180 barometer;
dht DHT;
#define DHT22_PIN 8
int x=0;//index of array
int array[64];
int tempsave;//saved temperature
U8GLIB_DOGM128 u8g(11, 10, U8G_PIN_NONE); //U8GLIB_DOGM128(cs, a0 [, reset])
void setup()
{
Wire.begin();
if(barometer.EnsureConnected())
{
// When we have connected, we reset the device to ensure a clean start.
barometer.SoftReset();
// Now we initialize the sensor and pull the calibration data.
barometer.Initialize();
}
}
void loop()
{
// READ DATA
int chk = DHT.read22(DHT22_PIN);
tempsave = DHT.temperature;
if(x<63)
{
array[x] = tempsave;
x++;
}
else
{
for(int i=0;i<63;i++)
{
array[i]=array[i+1];
}
array[63] = tempsave;
}
}
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
delay(10);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100001);
myWatchdogEnable (0b100000);
myWatchdogEnable (0b000111);
}
void draw(void) {
//printing values from 2 sensors (DHT22 and BMP180), together with update of current time from DS1307 RTC, and a XBMPfile drawing
}
//drawgraph is called within draw(), its the temperature history
void drawgraph() {
for(int i=0;i<=x;i++)
{
u8g.drawPixel(i,(array[i])*(-1)+135);
}
}
void myWatchdogEnable(const byte interval) {
MCUSR = 0; // reset various flags
WDTCSR |= 0b00011000; // see docs, set WDCE, WDE
WDTCSR = 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
}
ISR(WDT_vect) {
wdt_disable(); // disable watchdog
}