Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <IRremote.h>
- #include <ThreeWire.h>
- #include <RtcDS1302.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <SimpleDHT.h>
- #define DHT_PIN 4
- SimpleDHT11 dht11(DHT_PIN);
- float temperature = 0;
- float humidity = 0;
- int result_code = 0;
- #define COLUMNS_NUMBERS 16
- #define ROWS_NUMBERS 2
- #define LCD_ADDRESS 0x27
- #define RST_PIN 10
- #define DAT_PIN 11
- #define CLK_PIN 13
- ThreeWire my_connection(DAT_PIN, CLK_PIN, RST_PIN);
- RtcDS1302<ThreeWire> my_clock(my_connection);
- LiquidCrystal_I2C lcd(LCD_ADDRESS, COLUMNS_NUMBERS, ROWS_NUMBERS);
- RtcDateTime current_time;
- IRrecv irrecv(2);
- void setup() {
- Serial.begin(9600);
- irrecv.enableIRIn();
- lcd.init();
- delay(1000);
- lcd.backlight();
- Serial.begin(9600);
- my_clock.Begin();
- if (my_clock.GetIsWriteProtected()) {
- Serial.println("Clock was write protected. Write mode is enabled now.");
- my_clock.SetIsWriteProtected(false);
- }
- if (!my_clock.GetIsRunning()) {
- Serial.println("Clock is not running. Starting...");
- my_clock.SetIsRunning(true);
- }
- RtcDateTime compile_time = RtcDateTime(__DATE__, __TIME__);
- my_clock.SetDateTime(compile_time);
- if (!my_clock.IsDateTimeValid()) {
- RtcDateTime compile_time = RtcDateTime(__DATE__, __TIME__);
- Serial.println("Time is incorrect");
- Serial.println("Setting new date and time");
- printDateTime(compile_time, lcd);
- my_clock.SetDateTime(compile_time);
- }
- }
- int mode = 1;
- void loop() {
- if (irrecv.decode()) {
- switch (irrecv.decodedIRData.decodedRawData) {
- case 4077715200:
- Serial.println("got button 1");
- mode = 0;
- break;
- case 3877175040:
- Serial.println("got button 2");
- mode = 1;
- break;
- case 2707357440:
- Serial.println("got button 3");
- mode = 2;
- break;
- }
- irrecv.resume();
- }
- if (mode == 0) {
- Serial.println("current mode 0");
- current_time = my_clock.GetDateTime();
- if (current_time.IsValid()) {
- printDateTime(current_time, lcd);
- } else {
- Serial.println("Clock is invalid. Something is wrong");
- }
- delay(1000);
- } else if (mode == 1) {
- Serial.println("current mode 1");
- result_code = dht11.read2(&temperature, &humidity, NULL);
- if (result_code != SimpleDHTErrSuccess) {
- Serial.print("Read DHT failed. Error: ");
- Serial.println(result_code);
- delay(5000);
- return;
- }
- Serial.print("Temperature: ");
- Serial.print(temperature);
- Serial.print(" C, Humidity: ");
- Serial.print(humidity);
- Serial.println(" RH");
- // printTemperature(temperature, humidity, lcd);
- delay(5000);
- } else if (mode == 2) {
- Serial.println("current mode 2");
- delay(1000);
- }
- }
- void printDateTime(const RtcDateTime& dt, const LiquidCrystal_I2C& lcd) {
- char datestring[17], timestring[17];
- snprintf_P(datestring, sizeof(datestring) / sizeof(datestring[0]),
- PSTR("Date: %02u/%02u/%04u"),
- dt.Day(),
- dt.Month(),
- dt.Year() );
- snprintf_P(timestring, sizeof(timestring) / sizeof(timestring[0]),
- PSTR("Time: %02u:%02u:%02u"),
- dt.Hour(),
- dt.Minute(),
- dt.Second() );
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(datestring);
- lcd.setCursor(0, 1);
- lcd.print(timestring);
- }
- void printTemperature(float temp, float humi, const LiquidCrystal_I2C& lcd) {
- char temp_str[17] = "Temp: ";
- char humi_str[17] = "Humi: ";
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(temp_str);
- lcd.setCursor(0, 1);
- lcd.print(humi_str);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement