Salut à tous
Voila je viens de recevoir mon tiny RTC, j'utilise pour le moment un arduino nano avec divers capteurs, j'ai pu faire fonctionner l'horloge mais IMPOSSIBLE d' avoir l'heure ET la date...
Voici mon sketch, si quelqu'un peut m'aider à integrer l'heure et date sur mon LCD c'est pas de refus.
J'ai fouillé partout sur le net et à chaque fois il y a un truc qui va pas (comme la mise en memoire de la date en cas de coupure d'alim)
#define DHTPIN 3 // sonde dht (t/rh)
#define DHTTYPE DHT22 //#define DHTTYPE DHT11 (capteur DHT11)
#include <OneWire.h> //capteur t
#include <LiquidCrystal.h> //lcd
#include <DHT.h> //capteur dht
DHT dht(DHTPIN, DHTTYPE);
float h = 0;
float t = 0;
OneWire ds(4); //capteur t
LiquidCrystal lcd(5, 6, 7, 8, 9, 10, 11);
int backLight = 12; // pin 13 will control the backlight
void setup(void) {
Serial.begin(9600); //capteur t
dht.begin(); //capteur dht
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on.
lcd.begin(20,4); // columns, rows.
}
void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
}
Serial.print("ROM =");
for( i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
Serial.print(" Data = ");
Serial.print(present,HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
// convert the data to actual temperature
unsigned int raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
lcd.begin(20,4); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
lcd.setCursor(0,0); // set cursor to column 0, row 0 (the first row)
lcd.print("Heure Date"); // change this text to whatever you like. keep it clean.
lcd.setCursor(0,1); // set cursor to column 0, row 1
lcd.print(t = dht.readTemperature());
lcd.setCursor(6,1); // set cursor to column 0, row 3
lcd.print("*C");
lcd.setCursor(0,2); // set cursor to column 0, row 2
lcd.print(h = dht.readHumidity());
lcd.setCursor(6,2); // set cursor to column 0, row 3
lcd.print("%RH");
lcd.setCursor(0,3); // set cursor to column 0, row 3
lcd.print(celsius = (float)raw / 16.0);
lcd.setCursor(6,3); // set cursor to column 0, row 3
lcd.print("*C");
lcd.setCursor(12,3); // set cursor to column 0, row 3
lcd.print("temp ext");
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(t) || isnan(h)){
Serial.println( "Lecture impossible !");
}else{
Serial.print("Humidite :");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature :");
Serial.print(t);
Serial.println(" *C");
}
delay(5000);
}
Merci d'avance