Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <IRremote.h>
  2.  
  3. #include <ThreeWire.h>
  4. #include <RtcDS1302.h>
  5.  
  6. #include <Wire.h>
  7. #include <LiquidCrystal_I2C.h>
  8.  
  9. #include <SimpleDHT.h>
  10.  
  11. #define DHT_PIN 4
  12.  
  13. SimpleDHT11 dht11(DHT_PIN);
  14.  
  15. float temperature = 0;
  16. float humidity = 0;
  17. int result_code = 0;
  18.  
  19.  
  20. #define COLUMNS_NUMBERS 16
  21. #define ROWS_NUMBERS 2
  22. #define LCD_ADDRESS 0x27
  23.  
  24. #define RST_PIN 10
  25. #define DAT_PIN 11
  26. #define CLK_PIN 13
  27.  
  28. ThreeWire my_connection(DAT_PIN, CLK_PIN, RST_PIN);
  29. RtcDS1302<ThreeWire> my_clock(my_connection);
  30.  
  31. LiquidCrystal_I2C lcd(LCD_ADDRESS, COLUMNS_NUMBERS, ROWS_NUMBERS);
  32.  
  33.  
  34. RtcDateTime current_time;
  35.  
  36. IRrecv irrecv(2);
  37.  
  38. void setup() {
  39.   Serial.begin(9600);
  40.   irrecv.enableIRIn();
  41.  
  42.   lcd.init();
  43.  
  44.   delay(1000);
  45.  
  46.   lcd.backlight();
  47.  
  48.   Serial.begin(9600);
  49.   my_clock.Begin();
  50.  
  51.   if (my_clock.GetIsWriteProtected()) {
  52.     Serial.println("Clock was write protected. Write mode is enabled now.");
  53.     my_clock.SetIsWriteProtected(false);
  54.   }
  55.  
  56.   if (!my_clock.GetIsRunning()) {
  57.     Serial.println("Clock is not running. Starting...");
  58.     my_clock.SetIsRunning(true);
  59.   }
  60.  
  61.   RtcDateTime compile_time = RtcDateTime(__DATE__, __TIME__);
  62.   my_clock.SetDateTime(compile_time);
  63.   if (!my_clock.IsDateTimeValid()) {
  64.     RtcDateTime compile_time = RtcDateTime(__DATE__, __TIME__);
  65.  
  66.     Serial.println("Time is incorrect");
  67.     Serial.println("Setting new date and time");
  68.     printDateTime(compile_time, lcd);
  69.  
  70.     my_clock.SetDateTime(compile_time);
  71.   }
  72. }
  73.  
  74. int mode = 1;
  75.  
  76.  
  77.  
  78. void loop() {
  79.   if (irrecv.decode()) {
  80.     switch (irrecv.decodedIRData.decodedRawData) {
  81.       case 4077715200:
  82.         Serial.println("got button 1");
  83.         mode = 0;
  84.         break;
  85.       case 3877175040:
  86.         Serial.println("got button 2");
  87.         mode = 1;
  88.         break;
  89.       case 2707357440:
  90.         Serial.println("got button 3");
  91.         mode = 2;
  92.         break;
  93.     }
  94.    
  95.     irrecv.resume();
  96.   }
  97.  
  98.   if (mode == 0) {
  99.    
  100.     Serial.println("current mode 0");
  101.     current_time = my_clock.GetDateTime();
  102.  
  103.     if (current_time.IsValid()) {
  104.       printDateTime(current_time, lcd);
  105.     } else {
  106.       Serial.println("Clock is invalid. Something is wrong");
  107.     }
  108.  
  109.     delay(1000);
  110.    
  111.   } else if (mode == 1) {
  112.    
  113.     Serial.println("current mode 1");
  114.  
  115.     result_code = dht11.read2(&temperature, &humidity, NULL);
  116.  
  117.     if (result_code != SimpleDHTErrSuccess) {
  118.       Serial.print("Read DHT failed. Error: ");
  119.       Serial.println(result_code);
  120.       delay(5000);
  121.       return;
  122.     }
  123.  
  124.     Serial.print("Temperature: ");
  125.     Serial.print(temperature);
  126.     Serial.print(" C, Humidity: ");
  127.     Serial.print(humidity);
  128.     Serial.println(" RH");
  129.  
  130. //    printTemperature(temperature, humidity, lcd);
  131.  
  132.     delay(5000);
  133.    
  134.   } else if (mode == 2) {
  135.    
  136.     Serial.println("current mode 2");
  137.  
  138.     delay(1000);
  139.    
  140.   }
  141.  
  142. }
  143.  
  144. void printDateTime(const RtcDateTime& dt, const LiquidCrystal_I2C& lcd) {
  145.   char datestring[17], timestring[17];
  146.  
  147.   snprintf_P(datestring, sizeof(datestring) / sizeof(datestring[0]),
  148.           PSTR("Date: %02u/%02u/%04u"),
  149.           dt.Day(),
  150.           dt.Month(),
  151.           dt.Year() );
  152.  
  153.  
  154.   snprintf_P(timestring, sizeof(timestring) / sizeof(timestring[0]),
  155.           PSTR("Time: %02u:%02u:%02u"),
  156.           dt.Hour(),
  157.           dt.Minute(),
  158.           dt.Second() );
  159.  
  160.   lcd.clear();
  161.   lcd.setCursor(0, 0);
  162.   lcd.print(datestring);
  163.  
  164.   lcd.setCursor(0, 1);
  165.   lcd.print(timestring);
  166. }
  167.  
  168. void printTemperature(float temp, float humi, const LiquidCrystal_I2C& lcd) {
  169.   char temp_str[17] = "Temp: ";
  170.   char humi_str[17] = "Humi: ";
  171.  
  172.   lcd.clear();
  173.   lcd.setCursor(0, 0);
  174.   lcd.print(temp_str);
  175.  
  176.   lcd.setCursor(0, 1);
  177.   lcd.print(humi_str);
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement