Am schimbat afisajul ceasului ce ia infirmatiile de pe un server NTP, dar are reglaje si de pe o pagina locala web cum am prezentat intr-un articol precedent, din unul cu 4 cifre si driver TM1737 intr-unul cu 8 cifre si driver MAX7219.
Schema deriva din cea gasita in articolul IOT ESP32-8266 & Crypto earning, doar ca i-am adaugat senzorul de temperatura/umiditate DHT22 (AM2302):
Ca si precedentele versiuni recente de ceas NTP, la initializarea ceasului ni se afiseaza un AP care persista daca ceasu nu are configurata reteaua wi-fi si parola acesteia (se foloseste facilitatea librariei WiFiManager),
iar daca datele sunt deja introduse, va apare adresa, de exemplu 192.168.3.109:
apoi se intra in modul de ceas, in format de 24 sau 12 ore (functie de alegerea din pagina locala web)
Daca este activata afisarea informatiilor suplimentare, in minutele pare, apare de la secunda 55 data si anul:
iar in minutele impare, dintre secundele 50-55, temperatura
Am observat ca lapierderea tensiunii sau prima alimentare, afisajul nu se aprinde decat la apasarea butonului de RESET de pe pleca WeMos D1, asa ca am modificat un pic schema si acum alimentez afisajul si senzorul DHT22 (AM2302) cu 5V, nu cu 3,3V:
Dupa ce am vazut articolul Arduino Astronomical Clock (or Arduino Pond Pump Controller!) in care se folosea un encoder rotativ pentru reglajul orei si a datei si am reprodus experimetul pentru a inteelge modul de functionare, m-am gandit sa adaptez si eu sketch-ul la afisarea pe ecranul cu 8 cifre LED. Schema de conectare folosita este:
Ciclic pe ecran se prezinta informatiile: - ora:
- data:
- temperatura si umiditatea din camera:
Daca apas scurt pe butonul encoder-ului va apare pentru scurt timp ID-ul meu:
Daca apas mai mult (3 secunde minim), se trece in modul de reglaj al datelor (se roteste in sens trigometric pentru scadere valoare, respectiv in sensul acelor de ceasornic pentru crestere valoare, dupa care se apasa iar butonul pentru a trece la parametrul urmator): - anul:
- luna:
- ziua:
- ora:
- minutul:
Deoarece nu am putut face litera "m" am folosit intai "E", apoi "H", care se apropie mai mult, cred eu:
// adapted sketch by niq_ro from http://arduinotehniq.blogspot.com// and http://nicuflorica.blogspot.ro// version 1.0 in 6.11.2014, Craiova - Romanaia// version 2.0 in 16.12.2014// source for LEDControl: http://embedded-lab.com/blog/?p=6862
#include "LedControl.h"/* Now we need a LedControl to work with. ***** These pin numbers will probably not work with your hardware ***** pin 12 is connected to the DataIn pin 11 is connected to the CLK pin 10 is connected to LOAD We have only a single MAX72XX. */LedControl lc=LedControl(12,11,10,1);
// Example testing sketch for various DHT humidity/temperature sensors// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN A1 // what pin we're connected to A1// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302)//#define DHTTYPE DHT21 // DHT 21 (AM2301)// if is just sensor:// Connect pin 1 (on the left) of the sensor to +5V// Connect pin 2 of the sensor to whatever your DHTPIN is// Connect pin 4 (on the right) of the sensor to GROUND// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
// declaration for type of valueint t, h;
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib// original sketck from http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/// add part with SQW=1Hz from http://tronixstuff.wordpress.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/// Dawn & Dusk controller. http://andydoz.blogspot.ro/2014_08_01_archive.html// 16th August 2014 - (C) A.G.Doswell 2014
#include <Wire.h>
#include "RTClib.h"// from https://github.com/adafruit/RTClib
#include <LiquidCrystal.h>
#include <Encoder.h> // from http://www.pjrc.com/teensy/td_libs_Encoder.htmlRTC_DS1307RTC; // Tells the RTC library that we're using a DS1307 RTCEncoder knob(2, 3); //encoder connected to pins 2 and 3 (and ground)//the variables provide the holding values for the set clock routineint setyeartemp;
int setmonthtemp;
int setdaytemp;
int sethourstemp;
int setminstemp;
int setsecs = 0;
int maxday; // maximum number of days in the given monthint TimeOut = 10;
int TimeOutCounter;
// These variables are for the push button routineint buttonstate = 0; //flag to see if the button has been pressed, used internal on the subroutine onlyint pushlengthset = 3000; // value for a long push in mSint pushlength = pushlengthset; // set default pushlengthint pushstart = 0;// sets default push value for the button going lowint pushstop = 0;// sets the default value for when the button goes back highint knobval; // value for the rotation of the knobboolean buttonflag = false; // default value for the button flagvoidsetup()
{
// Initialize MAX7219 device
lc.shutdown(0,false); // Enable display
lc.setIntensity(0,3); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear display register // Initialize HTD sensor
dht.begin();
Wire.begin();
RTC.begin();
// if (! RTC.isrunning()) {//Serial.println("RTC is NOT running!");// following line sets the RTC to the date & time this sketch was compiledRTC.adjust(DateTime(__DATE__, __TIME__));
// }// part code from http://tronixstuff.wordpress.com/Wire.beginTransmission(0x68);
Wire.write(0x07); // move pointer to SQW address//Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square waveWire.write(0x13); // sends 0x13 (hex) 00010011 (binary) to control register - turns on square wave at 32kHzWire.endTransmission();
// end part code from http://tronixstuff.wordpress.com/pinMode(A0,INPUT);//push button on encoder connected to A0 (and GND)digitalWrite(A0,HIGH); //Pull A0 high/* Serial.begin(9600); Serial.println("test for niq_ro"); Serial.println("------------------");*/
}
voidloop()
{
// Reading temperature or humidity takes about 250 milliseconds!// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
// t = dht.readTemperature(); /* test part // test humidity valueh = 37;// test temperature valuet = 19;*///
lc.clearDisplay(0); // Clear display register for(int j=0; j<300; j++){
temperatura (t);
umiditate (h);
pushlength = pushlengthset;
pushlength = getpushlength ();
delay (10);
if (pushlength <pushlengthset) {
ShortPush ();
}
//This runs the setclock routine if the knob is pushed for a long timeif (pushlength >pushlengthset) {
lc.clearDisplay(0); // Clear display register DateTimenow = RTC.now();
setyeartemp=now.year(),DEC;
setmonthtemp=now.month(),DEC;
setdaytemp=now.day(),DEC;
sethourstemp=now.hour(),DEC;
setminstemp=now.minute(),DEC;
setclock();
pushlength = pushlengthset;
};
}
//delay(3000);
lc.clearDisplay(0); // Clear display register for(int j=0; j<300; j++){
DateTimenow = RTC.now();
int ora0 = now.hour();
int minut0 = now.minute();
int second0 = now.second();
/*// serial monitor Serial.print(now.hour(), DEC); Serial.print(":"); Serial.print(now.minute(), DEC); Serial.print(":"); Serial.print(now.second(), DEC); Serial.print(" -> "); Serial.print(ora1); Serial.println(":"); Serial.print(minut1); Serial.print(":"); Serial.print(second1); Serial.println("------------------");*/
ora (ora0, minut0, second0);
pushlength = pushlengthset;
pushlength = getpushlength ();
delay (10);
if (pushlength <pushlengthset) {
ShortPush ();
}
//This runs the setclock routine if the knob is pushed for a long timeif (pushlength >pushlengthset) {
lc.clearDisplay(0); // Clear display register DateTimenow = RTC.now();
setyeartemp=now.year(),DEC;
setmonthtemp=now.month(),DEC;
setdaytemp=now.day(),DEC;
sethourstemp=now.hour(),DEC;
setminstemp=now.minute(),DEC;
setclock();
pushlength = pushlengthset;
};
//delay (900);
}
t = dht.readTemperature(); // read data for temperature
lc.clearDisplay(0); // Clear display register for(int j=0; j<300; j++){
DateTimenow = RTC.now();
int ziua0 = now.day();
int luna0 = now.month();
int anul0 = now.year();
/*// serial monitor Serial.print(now.day(), DEC); Serial.print(":"); Serial.print(now.month(), DEC); Serial.print(":"); Serial.print(now.year(), DEC); Serial.print(" -> "); Serial.print(ziua0); Serial.println("."); Serial.print(luna0); Serial.print("."); Serial.print(anul0); Serial.println("------------------");*/
data (ziua0, luna0, anul0);
pushlength = pushlengthset;
pushlength = getpushlength ();
delay (10);
if (pushlength <pushlengthset) {
ShortPush ();
}
//This runs the setclock routine if the knob is pushed for a long timeif (pushlength >pushlengthset) {
lc.clearDisplay(0); // Clear display register DateTimenow = RTC.now();
setyeartemp=now.year(),DEC;
setmonthtemp=now.month(),DEC;
setdaytemp=now.day(),DEC;
sethourstemp=now.hour(),DEC;
setminstemp=now.minute(),DEC;
setclock();
pushlength = pushlengthset;
};
//delay (900);
}
}
void umiditate (int umidit)
{
int zu = int(umidit/10); // determin cifra zecilorint uu = umidit - 10*zu; // determin cifra unitatilor
lc.setDigit(0,2,zu, false); // afisez un 5 pe coloana 2
lc.setDigit(0,1,uu, false); // afisez un 0 pe coloana 1
lc.setRow(0,0,B0110111); // afisez litera "H"
}
void temperatura (int temper)
{
int zt = int(temper/10); // determin cifra zecilorint ut = temper - 10*zt; // determin cifra unitatilor
lc.setDigit(0,7,zt, false); // afisez un 5 pe coloana 7
lc.setDigit(0,6,ut, false); // afisez un 0 pe coloana 1
lc.setRow(0,5,B1100011); // afisez un semn de grad pe coloana 5
lc.setRow(0,4,B1001110); // afisez un C pe coloana 4
}
void ora (int ora1, int minut1, int second1)
{
// hourint zo = int(ora1/10); // determin cifra zecilorint uo = ora1 - 10*zo; // determin cifra unitatilorif (zo >= 1) lc.setDigit(0,7,zo, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uo, false); // afisez unitatile de ore pe coloana 6
lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// minutesint zm = int(minut1/10); // determin cifra zecilorint um = minut1 - 10*zm; // determin cifra unitatilor
lc.setDigit(0,4,zm, false); // afisez zecile de minute pe coloana 4
lc.setDigit(0,3,um, false); // afisez unitatile de minute pe coloana 3
lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// secondsint zs = int(second1/10); // determin cifra zecilorint us = second1 - 10*zs; // determin cifra unitatilor
lc.setDigit(0,1,zs, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,us, false); // afisez unitatile de minute pe coloana 0 (dreapta)
}
void data (int ziua1, int luna1, int anul1)
{
// ziua (day)int zz = int(ziua1/10); // determin cifra zecilorint uz = ziua1 - 10*zz; // determin cifra unitatilorif (zz >= 1) lc.setDigit(0,7,zz, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uz, true); // afisez unitatile de ore pe coloana 6//lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// luna (mounth)int zl = int(luna1/10); // determin cifra zecilorint ul = luna1 - 10*zl; // determin cifra unitatilor
lc.setDigit(0,5,zl, false); // afisez zecile de minute pe coloana 5
lc.setDigit(0,4,ul, true); // afisez unitatile de minute pe coloana 4//lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// anul (year)int ma = int(anul1/1000); // determin cifra miilorint rsa = anul1 - 1000*ma; // determin restul sutelorint sa = int(rsa/100); // determin cifra sutelorint rza = rsa - 100*sa; // determin restul zecilorint za = int(rza/10); // determin cifra zecilorint ua = rza - 10*za; // determin cifra anilor
lc.setDigit(0,3,ma, false); // afisez zecile de secunde pe coloana 3
lc.setDigit(0,2,sa, false); // afisez unitatile de minute pe coloana 2
lc.setDigit(0,1,za, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,ua, false); // afisez unitatile de minute pe coloana 0 (dreapta)
}
//sets the clockvoid setclock (){
setyear ();
lc.clearDisplay(0); // Clear display register
setmonth ();
lc.clearDisplay(0); // Clear display register
setday ();
lc.clearDisplay(0); // Clear display register
sethours ();
lc.clearDisplay(0); // Clear display register
setmins ();
lc.clearDisplay(0); // Clear display register RTC.adjust(DateTime(setyeartemp,setmonthtemp,setdaytemp,sethourstemp,setminstemp,setsecs));
delay (1000);
}
// subroutine to return the length of the button push.int getpushlength () {
buttonstate = digitalRead(A0);
if(buttonstate == LOW && buttonflag==false) {
pushstart = millis();
buttonflag = true;
};
if (buttonstate == HIGH && buttonflag==true) {
pushstop = millis ();
pushlength = pushstop - pushstart;
buttonflag = false;
};
return pushlength;
}
// The following subroutines set the individual clock parametersint setyear () {
//lcd.clear();//lc.clearDisplay(0); // Clear display register // lcd.print ("Set Year");
lc.setRow(0,7,B0111011); // afisez litera "y" pe coloane 7 (in stanga de tot)
lc.setRow(0,6,B1101111); // afisez litera "e" pe coloane 6
lc.setRow(0,5,B1111101); // afisez litera "a" pe coloane 5
lc.setRow(0,4,B0000101); // afisez litera "r" pe coloane 4
pushlength = pushlengthset;
pushlength = getpushlength ();
if (pushlength != pushlengthset) {
return setyeartemp;
}
//lc.clearDisplay(0); // Clear display register
knob.write(0);
delay (50);
knobval=knob.read();
if (knobval < -1) { //bit of software de-bounce
knobval = -1;
}
if (knobval > 1) {
knobval = 1;
}
setyeartemp=setyeartemp + knobval;
if (setyeartemp < 2014) { //Year can't be older than currently, it's not a time machine.
setyeartemp = 2014;
}
// lcd.print (setyeartemp);int zua = setyeartemp - 2000; // aflu ultimele 2 cifre din anint za = zua / 10; // aflu cifra zecilor din anint ua = zua - 10*za; // aflu cifra unitatilor din an
lc.setDigit(0,1,za, false); // afisez cifra zecilor din an pe coloana 1
lc.setDigit(0,0,ua, false); // afisez cifra unitatilor din an pe coloana 0 (dreapta de tot)
setyear();
}
int setmonth () {
//lcd.clear();//lcd.print ("Set Month");
lc.setRow(0,7,B0110111); // afisez litera "H" (in loc de "m") pe coloane 7 (in stanga de tot)//lc.setRow(0,7,B1001111); // afisez litera "E" (in loc de "m") pe coloane 7 (in stanga de tot)
lc.setRow(0,6,B0011101); // afisez litera "o" pe coloane 6
lc.setRow(0,5,B0010101); // afisez litera "n" pe coloane 5
lc.setRow(0,4,B0001111); // afisez litera "t" pe coloane 4
lc.setRow(0,3,B0010111); // afisez litera "h" pe coloane 3
pushlength = pushlengthset;
pushlength = getpushlength ();
if (pushlength != pushlengthset) {
return setmonthtemp;
}
// lcd.setCursor (0,1);
knob.write(0);
delay (50);
knobval=knob.read();
if (knobval < -1) {
knobval = -1;
}
if (knobval > 1) {
knobval = 1;
}
setmonthtemp=setmonthtemp + knobval;
if (setmonthtemp < 1) {// month must be between 1 and 12
setmonthtemp = 1;
}
if (setmonthtemp > 12) {
setmonthtemp=12;
}
// lcd.print (setmonthtemp);// lcd.print(" ");int zl = setmonthtemp / 10; // aflu cifra zecilor din lunaint ul = setmonthtemp - 10*zl; // aflu cifra unitatilor din luna
lc.setDigit(0,1,zl, false); // afisez cifra zecilor din luna pe coloana 1
lc.setDigit(0,0,ul, false); // afisez cifra unitatilor din luna pe coloana 0 (dreapta de tot)
setmonth();
}
int setday () {
if (setmonthtemp == 4 || setmonthtemp == 5 || setmonthtemp == 9 || setmonthtemp == 11) { //30 days hath September, April June and November
maxday = 30;
}
else {
maxday = 31; //... all the others have 31
}
if (setmonthtemp ==2 && setyeartemp % 4 ==0) { //... Except February alone, and that has 28 days clear, and 29 in a leap year.
maxday = 29;
}
if (setmonthtemp ==2 && setyeartemp % 4 !=0) {
maxday = 28;
}
//lcd.clear(); // lcd.print ("Set Day");
lc.setRow(0,7,B0111101); // afisez litera "d" pe coloane 7
lc.setRow(0,6,B1111101); // afisez litera "a" pe coloane 6
lc.setRow(0,5,B0111011); // afisez litera "y" pe coloane 5
pushlength = pushlengthset;
pushlength = getpushlength ();
if (pushlength != pushlengthset) {
return setdaytemp;
}
// lcd.setCursor (0,1);
knob.write(0);
delay (50);
knobval=knob.read();
if (knobval < -1) {
knobval = -1;
}
if (knobval > 1) {
knobval = 1;
}
setdaytemp=setdaytemp+ knobval;
if (setdaytemp < 1) {
setdaytemp = 1;
}
if (setdaytemp > maxday) {
setdaytemp = maxday;
}
// lcd.print (setdaytemp);// lcd.print(" "); int zz = setdaytemp / 10; // aflu cifra zecilor din ziint uz = setdaytemp - 10*zz; // aflu cifra unitatilor din zi
lc.setDigit(0,1,zz, false); // afisez cifra zecilor din zi pe coloana 1
lc.setDigit(0,0,uz, false); // afisez cifra unitatilor din zi pe coloana 0 (dreapta de tot)
setday();
}
int sethours () {
//lcd.clear();// lcd.setCursor (0,0);// lcd.print ("Set Hours");
lc.setRow(0,7,B0010111); // afisez litera "h" pe coloane 7
lc.setRow(0,6,B0011101); // afisez litera "o" pe coloane 6
lc.setRow(0,5,B0011100); // afisez litera "u" pe coloane 5
lc.setRow(0,4,B0000101); // afisez litera "r" pe coloane 4
pushlength = pushlengthset;
pushlength = getpushlength ();
if (pushlength != pushlengthset) {
return sethourstemp;
}
// lcd.setCursor (0,1);
knob.write(0);
delay (50);
knobval=knob.read();
if (knobval < -1) {
knobval = -1;
}
if (knobval > 1) {
knobval = 1;
}
sethourstemp=sethourstemp + knobval;
if (sethourstemp < 1) {
sethourstemp = 1;
}
if (sethourstemp > 23) {
sethourstemp=23;
}
// lcd.print (sethourstemp);// lcd.print(" "); int zo = sethourstemp / 10; // aflu cifra zecilor de oreint uo = sethourstemp - 10*zo; // aflu cifra unitatilor de ore
lc.setDigit(0,1,zo, false); // afisez cifra zecilor de ora pe coloana 1
lc.setDigit(0,0,uo, false); // afisez cifra unitatilor de ora pe coloana 0 (dreapta de tot)
sethours();
}
int setmins () {
//lcd.clear();// lcd.setCursor (0,0);// lcd.print ("Set Mins");
lc.setRow(0,7,B0110111); // afisez litera "H" (in loc de "m") pe coloane 7 (in stanga de tot)//lc.setRow(0,7,B1001111); // afisez litera "E" (in loc de "m") pe coloane 7 (in stanga de tot)
lc.setRow(0,6,B0010000); // afisez litera "i" pe coloane 6
lc.setRow(0,5,B0010101); // afisez litera "n" pe coloane 5
pushlength = pushlengthset;
pushlength = getpushlength ();
if (pushlength != pushlengthset) {
return setminstemp;
}
// lcd.setCursor (0,1);
knob.write(0);
delay (50);
knobval=knob.read();
if (knobval < -1) {
knobval = -1;
}
if (knobval > 1) {
knobval = 1;
}
setminstemp=setminstemp + knobval;
if (setminstemp < 0) {
setminstemp = 0;
}
if (setminstemp > 59) {
setminstemp=59;
}
// lcd.print (setminstemp);// lcd.print(" ");int zm = setminstemp / 10; // aflu cifra zecilor de minuteint um = setminstemp - 10*zm; // aflu cifra unitatilor de minute
lc.setDigit(0,1,zm, false); // afisez cifra zecilor de minute pe coloana 1
lc.setDigit(0,0,um, false); // afisez cifra unitatilor de minute pe coloana 0 (dreapta de tot)
setmins();
}
void ShortPush () {
//This displays show something
lc.clearDisplay(0); // Clear display registerfor (long Counter = 0; Counter < 10 ; Counter ++) { //returns to the main loop if it's been run 604 times // lcd.setCursor (2,0);// lcd.print ("Hi, niq_ro!");
lc.setRow(0,6,B1100010); // afisez litera "n" pe coloane 6
lc.setRow(0,5,B0100000); // afisez litera "i" pe coloane 5
lc.setRow(0,4,B1110011); // afisez litera "q" pe coloane 4
lc.setRow(0,3,B0000001); // afisez semnul "-" pe coloane 3
lc.setRow(0,2,B1000010); // afisez litera "r" pe coloane 2
lc.setRow(0,1,B1100011); // afisez litera "o" pe coloane 1delay(200);
}
lc.clearDisplay(0); // Clear display register
}
11.01.2014 Am schimbat putin la afisarea anului si minutelor:
In articolele precedente (partea I si partea a II-a), am realizat un ceas cu reglaj manual al orei si cu informatii despre temperatura si umiditate.
Temperatura si umiditatea sunt masurate de senzorul DHT22 (AM2302), cu o precizie destul de mare: +0,50C, respectiv +2% umiditate (%RH).
Partea de ceas este realizata cu integratul DS1307, care are o mica baterie pentru a functiona si cand este intrerupta alimentarea ceasului propriu-zis.
Partea de afisare este realizata pe afisoare led cu 8 cifre a cate 7 segmente, comandate de un integrat MAX7219.
Initial, timpii de afisare erau de 3 secunde pentru temperatura si umiditate si 10 secunde pentru ora, dar am constatat ca acest mod este prea "static", asa ca am schimbat la 3 secunde fiecare afisare; era si o mica problema la afisarea cifrelor multiplu de 10, cand aparea pentru scurt timp litera "A", cand modificam manual orele si minutele. Schema de conectare este identica:
Sketch-ul modificat este:
// adapted sketch by niq_ro from http://arduinotehniq.blogspot.com// and http://nicuflorica.blogspot.ro// version 1.0.b in 10.11.2014, Craiova - Romanaia// source for LEDControl: http://embedded-lab.com/blog/?p=6862
#include "LedControl.h"/* Now we need a LedControl to work with. ***** These pin numbers will probably not work with your hardware ***** pin 12 is connected to the DataIn pin 11 is connected to the CLK pin 10 is connected to LOAD We have only a single MAX72XX. */LedControl lc=LedControl(12,11,10,1);
// Example testing sketch for various DHT humidity/temperature sensors// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 7 // what pin we're connected to D7// Uncomment whatever type you're using!//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)//#define DHTTYPE DHT21 // DHT 21 (AM2301)// if is just sensor:// Connect pin 1 (on the left) of the sensor to +5V// Connect pin 2 of the sensor to whatever your DHTPIN is// Connect pin 4 (on the right) of the sensor to GROUND// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
// decralaration for type of valuefloat t1;
int t, h;
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib// original sketck from http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/// add part with SQW=1Hz from http://tronixstuff.wordpress.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/
#include <Wire.h>
#include "RTClib.h"RTC_DS1307 RTC;
byte SW0 = A0;
byte SW1 = A1;
byte SW2 = A2;
// use for hexa in zecimal conversionint zh1, uh1, ore1;
int zm1, um1, miniti1;
voidsetup()
{
// Initialize MAX7219 device
lc.shutdown(0,false); // Enable display
lc.setIntensity(0,11); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear display register // Initialize HTD sensor
dht.begin();
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
//Serial.println("RTC is NOT running!");// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
// part code from http://tronixstuff.wordpress.com/Wire.beginTransmission(0x68);
Wire.write(0x07); // move pointer to SQW addressWire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square waveWire.endTransmission();
// end part code from http://tronixstuff.wordpress.com/
}
pinMode(SW0, INPUT); // for this use a slide switchpinMode(SW1, INPUT); // N.O. push button switchpinMode(SW2, INPUT); // N.O. push button switchdigitalWrite(SW0, HIGH); // pull-ups ondigitalWrite(SW1, HIGH);
digitalWrite(SW2, HIGH);
/* Serial.begin(9600); Serial.println("test for niq_ro"); Serial.println("------------------");*/
}
voidloop()
{
// Reading temperature or humidity takes about 250 milliseconds!// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t1 = dht.readTemperature();
t1 = 10*t1;
t = t1;
/* test part // test humidity valueh = 37;// test temperature valuet = 19;*///
lc.clearDisplay(0); // Clear display register
temperatura (t);
umiditate (h);
delay(3000);
lc.clearDisplay(0); // Clear display register for(int j=0; j<6; j++){
DateTimenow = RTC.now();
int ora0 = now.hour();
int minut0 = now.minute();
int second0 = now.second();
/*// serial monitor Serial.print(now.hour(), DEC); Serial.print(":"); Serial.print(now.minute(), DEC); Serial.print(":"); Serial.print(now.second(), DEC); Serial.print(" -> "); Serial.print(ora1); Serial.println(":"); Serial.print(minut1); Serial.print(":"); Serial.print(second1); Serial.println("------------------");*/
ora (ora0, minut0, second0);
if (!(digitalRead(SW0))) set_time(); // hold the switch to set timedelay (500);
}
}
void umiditate (int umidit)
{
int zu = int(umidit/10); // determin cifra zecilorint uu = umidit - 10*zu; // determin cifra unitatilor
lc.setDigit(0,2,zu, false); // afisez un 5 pe coloana 2
lc.setDigit(0,1,uu, false); // afisez un 0 pe coloana 1
lc.setRow(0,0,B0110111); // afisez litera "H"
}
void temperatura (int temper)
{
int st = int(temper/100); // determin cifra sutelor
temper = temper - 100*st; // restul numarului de la zeci si unitati int zt = int(temper/10); // determin cifra zecilorint ut = temper - 10*zt; // determin cifra unitatilor
lc.setDigit(0,7,st, false); // afisez un 5 pe coloana 7
lc.setDigit(0,6,zt, true); // afisez un 0 pe coloana 1 si virgula
lc.setDigit(0,5,ut, false); // afisez un 5 pe coloana 7//lc.setRow(0,5,B1100011); // afisez un semn de grad pe coloana 5
lc.setRow(0,4,B1001110); // afisez un C pe coloana 4
}
void ora (int ora1, int minut1, int second1)
{
// hourint zo = int(ora1/10); // determin cifra zecilorint uo = ora1 - 10*zo; // determin cifra unitatilorif (zo >= 1) lc.setDigit(0,7,zo, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uo, false); // afisez unitatile de ore pe coloana 6
lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// minutesint zm = int(minut1/10); // determin cifra zecilorint um = minut1 - 10*zm; // determin cifra unitatilor
lc.setDigit(0,4,zm, false); // afisez zecile de minute pe coloana 4
lc.setDigit(0,3,um, false); // afisez unitatile de minute pe coloana 3
lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// secondsint zs = int(second1/10); // determin cifra zecilorint us = second1 - 10*zs; // determin cifra unitatilor
lc.setDigit(0,1,zs, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,us, false); // afisez unitatile de minute pe coloana 0 (dreapta)
}
void set_time() {
byte minutes1 = 0;
byte hours1 = 0;
byte minutes = 0;
byte hours = 0;
while (!digitalRead(SW0)) // set time switch must be released to exit
{
minutes1=minutes;
hours1=hours;
while (!digitalRead(SW1)) // set minutes
{
minutes++;
if ((minutes & 0x0f) > 9) minutes = minutes + 6;
if (minutes > 0x59) minutes = 0;
// converting hexa in zecimal:
zh1 = hours / 16;
uh1 = hours - 16 * zh1 ;
ore1 = 10 * zh1 + uh1;
zm1 = minutes / 16;
um1 = minutes - 16 * zm1 ;
miniti1 = 10 * zm1 + um1;
lc.clearDisplay(0); // Clear display register // hour//int zo = int(ora1/10); // determin cifra zecilor//int uo = ora1 - 10*zo; // determin cifra unitatilorif (zh1 >= 1) lc.setDigit(0,7,zh1, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uh1, false); // afisez unitatile de ore pe coloana 6
lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// minutes//int zm = int(minut1/10); // determin cifra zecilor//int um = minut1 - 10*zm; // determin cifra unitatilor
lc.setDigit(0,4,zm1, false); // afisez zecile de minute pe coloana 4
lc.setDigit(0,3,um1, false); // afisez unitatile de minute pe coloana 3
lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// seconds//int zs = int(second1/10); // determin cifra zecilor//int us = second1 - 10*zs; // determin cifra unitatilor
lc.setDigit(0,1,0, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,0, false); // afisez unitatile de minute pe coloana 0 (dreapta) Serial.print("Minutes = ");
if (minutes >= 9) Serial.print("0");
Serial.println(minutes, HEX);
delay(250);
}
while (!digitalRead(SW2)) // set hours
{
hours++;
if ((hours & 0x0f) > 9) hours = hours + 6;
if (hours > 0x23) hours = 0;
// converting hexa in zecimal:
zh1 = hours / 16;
uh1 = hours - 16 * zh1 ;
ore1 = 10 * zh1 + uh1;
zm1 = minutes / 16;
um1 = minutes - 16 * zm1 ;
miniti1 = 10 * zm1 + um1;
lc.clearDisplay(0); // Clear display register // hour//int zo = int(ora1/10); // determin cifra zecilor//int uo = ora1 - 10*zo; // determin cifra unitatilorif (zh1 >= 1) lc.setDigit(0,7,zh1, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uh1, false); // afisez unitatile de ore pe coloana 6
lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// minutes//int zm = int(minut1/10); // determin cifra zecilor//int um = minut1 - 10*zm; // determin cifra unitatilor
lc.setDigit(0,4,zm1, false); // afisez zecile de minute pe coloana 4
lc.setDigit(0,3,um1, false); // afisez unitatile de minute pe coloana 3
lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// seconds//int zs = int(second1/10); // determin cifra zecilor//int us = second1 - 10*zs; // determin cifra unitatilor
lc.setDigit(0,1,0, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,0, false); // afisez unitatile de minute pe coloana 0 (dreapta) Serial.print("Hours = ");
if (hours <= 9) Serial.print("0");
Serial.println(hours, HEX);
delay(250);
}
Wire.beginTransmission(0x68); // activate DS1307Wire.write(0); // where to beginWire.write(0x00); //secondsWire.write(minutes); //minutesWire.write(0x80 | hours); //hours (24hr time)Wire.write(0x06); // Day 01-07Wire.write(0x01); // Date 0-31Wire.write(0x05); // month 0-12Wire.write(0x09); // Year 00-99Wire.write(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7. Wire.endTransmission();
// converting hexa in zecimal:
zh1 = hours / 16;
uh1 = hours - 16 * zh1 ;
ore1 = 10 * zh1 + uh1;
zm1 = minutes / 16;
um1 = minutes - 16 * zm1 ;
miniti1 = 10 * zm1 + um1;
lc.clearDisplay(0); // Clear display register // hour//int zo = int(ora1/10); // determin cifra zecilor//int uo = ora1 - 10*zo; // determin cifra unitatilorif (zh1 >= 1) lc.setDigit(0,7,zh1, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uh1, false); // afisez unitatile de ore pe coloana 6
lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// minutes//int zm = int(minut1/10); // determin cifra zecilor//int um = minut1 - 10*zm; // determin cifra unitatilor
lc.setDigit(0,4,zm1, false); // afisez zecile de minute pe coloana 4
lc.setDigit(0,3,um1, false); // afisez unitatile de minute pe coloana 3
lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// seconds//int zs = int(second1/10); // determin cifra zecilor//int us = second1 - 10*zs; // determin cifra unitatilor
lc.setDigit(0,1,0, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,0, false); // afisez unitatile de minute pe coloana 0 (dreapta)
}
}
Am mai constatat ca noaptea intensitatea luminoasa poate deranja, asa ca am pus un buton fara retinere (ca si celelalte) pentru a regla contrastul, deoarece libraria LedControl permite controlarea contrastului in 16 trepte.
Schema devine:
iar sketch-ul este:
// adapted sketch by niq_ro from http://arduinotehniq.blogspot.com// and http://nicuflorica.blogspot.ro// version 1.0.c in 10.11.2014, Craiova - Romanaia// source for LEDControl: http://embedded-lab.com/blog/?p=6862
#include "LedControl.h"/* Now we need a LedControl to work with. ***** These pin numbers will probably not work with your hardware ***** pin 12 is connected to the DataIn pin 11 is connected to the CLK pin 10 is connected to LOAD We have only a single MAX72XX. */LedControl lc=LedControl(12,11,10,1);
// Example testing sketch for various DHT humidity/temperature sensors// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 7 // what pin we're connected to D7// Uncomment whatever type you're using!//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)//#define DHTTYPE DHT21 // DHT 21 (AM2301)// if is just sensor:// Connect pin 1 (on the left) of the sensor to +5V// Connect pin 2 of the sensor to whatever your DHTPIN is// Connect pin 4 (on the right) of the sensor to GROUND// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
// decralaration for type of valuefloat t1;
int t, h;
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib// original sketck from http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/// add part with SQW=1Hz from http://tronixstuff.wordpress.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/
#include <Wire.h>
#include "RTClib.h"RTC_DS1307 RTC;
byte SW0 = A0;
byte SW1 = A1;
byte SW2 = A2;
byte SW3 = A3; // for contrast// use for hexa in zecimal conversionint zh1, uh1, ore1;
int zm1, um1, miniti1;
// contrast controlint contr = 11;
voidsetup()
{
// Initialize MAX7219 device
lc.shutdown(0,false); // Enable display
lc.setIntensity(0,contr); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear display register // Initialize HTD sensor
dht.begin();
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
//Serial.println("RTC is NOT running!");// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
// part code from http://tronixstuff.wordpress.com/Wire.beginTransmission(0x68);
Wire.write(0x07); // move pointer to SQW addressWire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square waveWire.endTransmission();
// end part code from http://tronixstuff.wordpress.com/
}
pinMode(SW0, INPUT); // for this use a slide switchpinMode(SW1, INPUT); // N.O. push button switchpinMode(SW2, INPUT); // N.O. push button switchpinMode(SW3, INPUT); // N.O. push button switchdigitalWrite(SW0, HIGH); // pull-ups ondigitalWrite(SW1, HIGH);
digitalWrite(SW2, HIGH);
digitalWrite(SW3, HIGH);
/* Serial.begin(9600); Serial.println("test for niq_ro"); Serial.println("------------------");*/
}
voidloop()
{
// Reading temperature or humidity takes about 250 milliseconds!// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t1 = dht.readTemperature();
t1 = 10*t1;
t = t1;
/* test part // test humidity valueh = 37;// test temperature valuet = 19;*///
lc.clearDisplay(0); // Clear display register for(int j=0; j<6; j++){
temperatura (t);
umiditate (h);
//delay(3000);if (!(digitalRead(SW3))) {
contr++;
if (contr==16) contr=1;
lc.setIntensity(0,contr); // Set brightness level (0 is min, 15 is max) delay (50);
}
delay(500);
}
lc.clearDisplay(0); // Clear display register for(int j=0; j<6; j++){
DateTimenow = RTC.now();
int ora0 = now.hour();
int minut0 = now.minute();
int second0 = now.second();
/*// serial monitor Serial.print(now.hour(), DEC); Serial.print(":"); Serial.print(now.minute(), DEC); Serial.print(":"); Serial.print(now.second(), DEC); Serial.print(" -> "); Serial.print(ora1); Serial.println(":"); Serial.print(minut1); Serial.print(":"); Serial.print(second1); Serial.println("------------------");*/
ora (ora0, minut0, second0);
if (!(digitalRead(SW0))) set_time(); // hold the switch to set timeif (!(digitalRead(SW3))) {
contr++;
if (contr==16) contr=1;
lc.setIntensity(0,contr); // Set brightness level (0 is min, 15 is max) delay (50);
}
delay (500);
}
}
void umiditate (int umidit)
{
int zu = int(umidit/10); // determin cifra zecilorint uu = umidit - 10*zu; // determin cifra unitatilor
lc.setDigit(0,2,zu, false); // afisez un 5 pe coloana 2
lc.setDigit(0,1,uu, false); // afisez un 0 pe coloana 1
lc.setRow(0,0,B0110111); // afisez litera "H"
}
void temperatura (int temper)
{
int st = int(temper/100); // determin cifra sutelor
temper = temper - 100*st; // restul numarului de la zeci si unitati int zt = int(temper/10); // determin cifra zecilorint ut = temper - 10*zt; // determin cifra unitatilor
lc.setDigit(0,7,st, false); // afisez un 5 pe coloana 7
lc.setDigit(0,6,zt, true); // afisez un 0 pe coloana 1 si virgula
lc.setDigit(0,5,ut, false); // afisez un 5 pe coloana 7//lc.setRow(0,5,B1100011); // afisez un semn de grad pe coloana 5
lc.setRow(0,4,B1001110); // afisez un C pe coloana 4
}
void ora (int ora1, int minut1, int second1)
{
// hourint zo = int(ora1/10); // determin cifra zecilorint uo = ora1 - 10*zo; // determin cifra unitatilorif (zo >= 1) lc.setDigit(0,7,zo, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uo, false); // afisez unitatile de ore pe coloana 6
lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// minutesint zm = int(minut1/10); // determin cifra zecilorint um = minut1 - 10*zm; // determin cifra unitatilor
lc.setDigit(0,4,zm, false); // afisez zecile de minute pe coloana 4
lc.setDigit(0,3,um, false); // afisez unitatile de minute pe coloana 3
lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// secondsint zs = int(second1/10); // determin cifra zecilorint us = second1 - 10*zs; // determin cifra unitatilor
lc.setDigit(0,1,zs, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,us, false); // afisez unitatile de minute pe coloana 0 (dreapta)
}
void set_time() {
byte minutes1 = 0;
byte hours1 = 0;
byte minutes = 0;
byte hours = 0;
while (!digitalRead(SW0)) // set time switch must be released to exit
{
minutes1=minutes;
hours1=hours;
while (!digitalRead(SW1)) // set minutes
{
minutes++;
if ((minutes & 0x0f) > 9) minutes = minutes + 6;
if (minutes > 0x59) minutes = 0;
// converting hexa in zecimal:
zh1 = hours / 16;
uh1 = hours - 16 * zh1 ;
ore1 = 10 * zh1 + uh1;
zm1 = minutes / 16;
um1 = minutes - 16 * zm1 ;
miniti1 = 10 * zm1 + um1;
lc.clearDisplay(0); // Clear display register // hour//int zo = int(ora1/10); // determin cifra zecilor//int uo = ora1 - 10*zo; // determin cifra unitatilorif (zh1 >= 1) lc.setDigit(0,7,zh1, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uh1, false); // afisez unitatile de ore pe coloana 6
lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// minutes//int zm = int(minut1/10); // determin cifra zecilor//int um = minut1 - 10*zm; // determin cifra unitatilor
lc.setDigit(0,4,zm1, false); // afisez zecile de minute pe coloana 4
lc.setDigit(0,3,um1, false); // afisez unitatile de minute pe coloana 3
lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// seconds//int zs = int(second1/10); // determin cifra zecilor//int us = second1 - 10*zs; // determin cifra unitatilor
lc.setDigit(0,1,0, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,0, false); // afisez unitatile de minute pe coloana 0 (dreapta) Serial.print("Minutes = ");
if (minutes >= 9) Serial.print("0");
Serial.println(minutes, HEX);
delay(250);
}
while (!digitalRead(SW2)) // set hours
{
hours++;
if ((hours & 0x0f) > 9) hours = hours + 6;
if (hours > 0x23) hours = 0;
// converting hexa in zecimal:
zh1 = hours / 16;
uh1 = hours - 16 * zh1 ;
ore1 = 10 * zh1 + uh1;
zm1 = minutes / 16;
um1 = minutes - 16 * zm1 ;
miniti1 = 10 * zm1 + um1;
lc.clearDisplay(0); // Clear display register // hour//int zo = int(ora1/10); // determin cifra zecilor//int uo = ora1 - 10*zo; // determin cifra unitatilorif (zh1 >= 1) lc.setDigit(0,7,zh1, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uh1, false); // afisez unitatile de ore pe coloana 6
lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// minutes//int zm = int(minut1/10); // determin cifra zecilor//int um = minut1 - 10*zm; // determin cifra unitatilor
lc.setDigit(0,4,zm1, false); // afisez zecile de minute pe coloana 4
lc.setDigit(0,3,um1, false); // afisez unitatile de minute pe coloana 3
lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// seconds//int zs = int(second1/10); // determin cifra zecilor//int us = second1 - 10*zs; // determin cifra unitatilor
lc.setDigit(0,1,0, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,0, false); // afisez unitatile de minute pe coloana 0 (dreapta) Serial.print("Hours = ");
if (hours <= 9) Serial.print("0");
Serial.println(hours, HEX);
delay(250);
}
Wire.beginTransmission(0x68); // activate DS1307Wire.write(0); // where to beginWire.write(0x00); //secondsWire.write(minutes); //minutesWire.write(0x80 | hours); //hours (24hr time)Wire.write(0x06); // Day 01-07Wire.write(0x01); // Date 0-31Wire.write(0x05); // month 0-12Wire.write(0x09); // Year 00-99Wire.write(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7. Wire.endTransmission();
// converting hexa in zecimal:
zh1 = hours / 16;
uh1 = hours - 16 * zh1 ;
ore1 = 10 * zh1 + uh1;
zm1 = minutes / 16;
um1 = minutes - 16 * zm1 ;
miniti1 = 10 * zm1 + um1;
lc.clearDisplay(0); // Clear display register // hour//int zo = int(ora1/10); // determin cifra zecilor//int uo = ora1 - 10*zo; // determin cifra unitatilorif (zh1 >= 1) lc.setDigit(0,7,zh1, false); // afisez zecile de ore pe coloana 7 (in stanga)
lc.setDigit(0,6,uh1, false); // afisez unitatile de ore pe coloana 6
lc.setRow(0,5,B0000001); // afisez o liniuta pe coloana 5// minutes//int zm = int(minut1/10); // determin cifra zecilor//int um = minut1 - 10*zm; // determin cifra unitatilor
lc.setDigit(0,4,zm1, false); // afisez zecile de minute pe coloana 4
lc.setDigit(0,3,um1, false); // afisez unitatile de minute pe coloana 3
lc.setRow(0,2,B0000001); // afisez o liniuta pe coloana 2// seconds//int zs = int(second1/10); // determin cifra zecilor//int us = second1 - 10*zs; // determin cifra unitatilor
lc.setDigit(0,1,0, false); // afisez zecile de secunde pe coloana 1
lc.setDigit(0,0,0, false); // afisez unitatile de minute pe coloana 0 (dreapta)
}
}
15.06.2015 Am primit de la Janos Baricz, care are meritul de a ma convinge sa fac acest ceas cu indicator de temperatura si umiditate, cateva poze cu montajul de ceas: