Odd Use of Watch Dog Timer
![FILE0006.JPG](/proxy/?url=https://content.instructables.com/FWT/R5O5/IL25Z512/FWTR5O5IL25Z512.jpg&filename=FILE0006.JPG)
![FILE0003.JPG](/proxy/?url=https://content.instructables.com/F5I/DYYY/IL25Z4TA/F5IDYYYIL25Z4TA.jpg&filename=FILE0003.JPG)
![FILE0004](/proxy/?url=https://content.instructables.com/F58/2B2C/IL25Z4TT/F582B2CIL25Z4TT.jpg&filename=FILE0004)
What I meant of Odd Use is the result not my expected but exactly what I needed.
I experiment 7 segment LED display driven by 74HC595, I found the circuit and the code to build on, I previously built the DHT11 and 1602 LCD to test the temperature and humidity without problem, now I give it a try to display with 7 segment LED. I found a problem, I cannot run the getData() and displayData() simultaneously in the loop() function, the left three digits kept on flickering, though I still can guess it.
void loop() {
//getData();
ledNumberWrite(displayData);
}
The getData() is the function to fetch data from DHT11, I have heard DHT11 is slowly respond, so I need a method to run it out of the loop().
void getData() {
int humid=0, temp=0;
switch(dht11.read()) {
case DHT11::OK:
humid=dht11.getHumidity();
temp=dht11.getTemperature();
displayData=temp*100+humid;
break;
}
}
Watch Dog Timer
![FILE0005](/proxy/?url=https://content.instructables.com/FYB/63G5/IL25Z4U3/FYB63G5IL25Z4U3.jpg&filename=FILE0005)
After research, I found some piece of code regarding Watch Dog Timer, I have no full experience on WDT, I only knew that it will reset the computer if something has not done properly. I merge them and have the getData() put inside the setup() function, when the WDT executing the program will reset once and force run the getData().
From the video you'll find the digits are not flickering any more, but steadily for 3 seconds and blink out 3 second, I don't have to guess it.
#include <DHT11.h>
int pinLatch = 4;
int pinClock = 7;
int pinData = 8;
int pinDht = 2;
unsigned char characterMap[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99,
0x92, 0x82, 0xF8, 0X80, 0X90};
unsigned char characterBuffer[] = {0xF1, 0xF2, 0xF4, 0xF8};
DHT11 dht11(pinDht);
int displayData = 0;
void setup (){
pinMode(pinLatch, OUTPUT);
pinMode(pinClock, OUTPUT);
pinMode(pinData, OUTPUT);
delay (2000);
getData();
watchdogSetup();
}
Final Code
There is no final code, the code given here are not my work, I just post here for interesting issue, actually I have post it in the forum as a problem to be solved, if some experts can give me some advises.
void watchdogSetup(void) {
WDTCSR |= (1<<WDCE) | (1<<WDE);
WDTCSR = (1<<WDIE) | (1<<WDE) | (0<<WDP3) |
(1<<WDP2) | (2<<WDP1) | (1<<WDP0);
}
ISR(WDT_vect) {// Watchdog timer interrupt.
}