Hi, leo:
First of all, thank you for developing and releasing such a great library.
I'm a beginner of Arduino and trying to build a LED clock using your swRTC library.
My LED clock is operated with Atmega8, and a 4-digit LED display is drived by MAX7219.
I also connected a DHT11 sensor for temperature and humidity report.
My sketch is running well in this hardware combination except one thing -- the deltaT value.
I found that the negative value of deltaT will be turned into positive spontaneously after calling rtc.setDeltaT() function.
I'm not sure if I made a stupid mistake in my sketch. Maybe you can give me some advice. Thank you!
#include <swRTC.h>
#include <LedControl.h>
#include <dht11.h>
#define DHT11PIN 2
#define SW_TimeSet_Pin 3
#define SW_H_Pin 4
#define SW_M_Pin 5
#define SW_S_Pin 6
#define SW_DeltaTSet_Pin 7
#define LedDataPIN 9
#define LedClkPIN 10
#define LedCsPIN 11
#define MAX7219Num 1
#define Analog_CdS_Pin 0
swRTC rtc;
LedControl lc=LedControl(LedDataPIN,LedClkPIN,LedCsPIN,MAX7219Num);
dht11 DHT11;
byte HUR,hour12F,MIN,SEC,TenHURDigit,HURDigit,TenMINDigit,MINDigit,TenSECDigit,SECDigit;
byte HunDTDigit,TenDTDigit,DTDigit;
byte ReadT,ReadH,TenTempDigit,TempDigit,TenHumidDigit,HumidDigit;
byte CdS;
int DeltaT;
char TenHURChar;
boolean set_status = false;
boolean set_DeltaT = false;
boolean Blink = true;
boolean Bol_HUR = false;
boolean Bol_MIN = false;
boolean Bol_SEC = false;
void setup() {
// Initial time value
rtc.stopRTC(); //stop the RTC
rtc.setTime(12,0,0); //set the time here
rtc.setDate(23,4,2013); //set the date here
rtc.startRTC(); //start the RTC
lc.shutdown(0,false);
lc.clearDisplay(0);
pinMode(SW_TimeSet_Pin,INPUT);
pinMode(SW_H_Pin,INPUT);
pinMode(SW_M_Pin,INPUT);
pinMode(SW_S_Pin,INPUT);
pinMode(SW_DeltaTSet_Pin,INPUT);
}
void loop() {
CdS = analogRead(Analog_CdS_Pin)/102;
lc.setIntensity(0,CdS);
HUR = rtc.getHours();
MIN = rtc.getMinutes();
LED_Display_Hour_Minute();
Blink = !Blink;
delay(200);
while (digitalRead(SW_S_Pin)){ // display second
MIN = rtc.getMinutes();
SEC = rtc.getSeconds();
LED_Display_Second();
}
while (digitalRead(SW_M_Pin)){ // display temperature
DHT11.read(DHT11PIN);
ReadT = DHT11.temperature;
LED_Display_temperature();
delay(1000);
}
while (digitalRead(SW_H_Pin)){ // display humidity
DHT11.read(DHT11PIN);
ReadH = DHT11.humidity;
LED_Display_humidity();
delay(1000);
}
// Manual time adjust
while (digitalRead(SW_TimeSet_Pin)) {
set_status = true;
LED_Display_Hour_Minute();
while (digitalRead(SW_M_Pin)) {
Bol_MIN = true;
MIN = MIN++;
if (MIN >59) MIN =0;
LED_Display_Hour_Minute();
delay(100);
}
while (digitalRead(SW_H_Pin)) {
Bol_HUR = true;
HUR = HUR++;
if (HUR >23) HUR =0;
LED_Display_Hour_Minute();
delay(200);
}
while (digitalRead(SW_S_Pin)) {
Bol_SEC = true;
SEC = 0;
LED_Display_Second();
delay(300);
}
// DeltaT adjust
while (digitalRead(SW_DeltaTSet_Pin)) {
DeltaT = rtc.getDeltaT();
LED_Display_DeltaT();
while (digitalRead(SW_H_Pin)) {
set_DeltaT = true;
DeltaT = DeltaT+10;
if (DeltaT > 8400) DeltaT = 0;
LED_Display_DeltaT();
delay(150);
}
while (digitalRead(SW_M_Pin)) {
set_DeltaT = true;
DeltaT = DeltaT-10;
if (DeltaT < -8400) DeltaT = 0;
LED_Display_DeltaT();
delay(150);
}
while (digitalRead(SW_S_Pin)) {
set_DeltaT = true;
DeltaT = 0;
LED_Display_DeltaT();
delay(300);
}
if (set_DeltaT){
rtc.setDeltaT(DeltaT);
set_DeltaT = false;
}
}
}
if (Bol_HUR | Bol_MIN | Bol_SEC){
rtc.stopRTC(); //stop the RTC
rtc.setTime(HUR,MIN,SEC); //set the time here
rtc.startRTC();
set_status = false;
Bol_MIN = false;
Bol_HUR = false;
Bol_SEC = false;
}
else if (set_status) set_status = false;
}
//////////////////////////////////////////////////////////
void LED_Display_Hour_Minute(){ //LED Clock Display
// Transform 24H to 12H
if(HUR>12) hour12F = HUR-12;
else if(HUR == 0) hour12F = 12;
else hour12F = HUR;
// Let the Ten-Digit of Hour not to display '0' on LED
if(hour12F > 9) TenHURChar = '1';
else TenHURChar = ' ';
//
TenHURDigit = hour12F/10;
HURDigit = hour12F % 10;
TenMINDigit = MIN/10;
MINDigit = MIN % 10;
lc.setChar(0,0,TenHURChar,set_status);
lc.setDigit(0,1,HURDigit,set_status | Blink); // F2481AH CB
lc.setDigit(0,2,TenMINDigit,set_status | Blink); // F2481AH CB
lc.setDigit(0,3,MINDigit,set_status);
if (HUR <12) lc.setLed(0,0,6,true); // AM sign
else lc.setLed(0,0,5,true); // PM sign
delay(300);
}
void LED_Display_Second(){
TenMINDigit = MIN/10;
MINDigit = MIN % 10;
TenSECDigit = SEC/10;
SECDigit = SEC % 10;
lc.setDigit(0,0,TenMINDigit,false);
lc.setDigit(0,1,MINDigit,true); // F2481AH CB on
lc.setDigit(0,2,TenSECDigit,true); // F2481AH CB on
lc.setDigit(0,3,SECDigit,false);
}
void LED_Display_DeltaT(){
HunDTDigit = abs(DeltaT / 1000);
TenDTDigit = abs(DeltaT /100 % 10);
DTDigit = abs(DeltaT % 100 /10);
lc.setDigit(0,1,HunDTDigit,false);// F2481AH CB off
lc.setDigit(0,2,TenDTDigit,false);// F2481AH CB off
lc.setDigit(0,3,DTDigit,true);
if (DeltaT < 0)
lc.setChar(0,0,'-',true);
else
lc.setChar(0,0,' ',true);
}
void LED_Display_temperature(){
TenTempDigit = ReadT/10;
TempDigit = ReadT % 10;
if (ReadT<0) lc.setChar(0,0,'-',false);
else lc.setChar(0,0,' ',false);
lc.setDigit(0,1,TenTempDigit,false);
lc.setDigit(0,2,TempDigit,false);
lc.setChar(0,3,'c',false);
}
void LED_Display_humidity(){
TenHumidDigit = ReadH/10;
HumidDigit = ReadH % 10;
lc.setChar(0,0,' ',false);
lc.setDigit(0,1,TenHumidDigit,false);
lc.setDigit(0,2,HumidDigit,false);
lc.setChar(0,3,'h',false);
}