Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.67 KB | None | 0 0
  1. #include <DHT.h>
  2. #include <LiquidCrystal.h>
  3. #include <DS3231.h>
  4. #include <Wire.h>
  5.  
  6. //Zdefiniowanie Pinów
  7.  
  8. #define PIR 2
  9. #define DHT11_PIN 13
  10. #define przycisk1 A5
  11. #define czerwony 5
  12. #define zielony 4
  13. #define niebieski 3
  14. #define podswietlenie_LCD 6
  15.  
  16. //Wywołanie obiektów
  17.  
  18. DHT dht;
  19. LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
  20. DS3231 rtc(SDA, SCL);
  21.  
  22. Time t;
  23.  
  24. int temp = 00, wilg = 00, tryb = 0, detekcja = 0;
  25. unsigned long odliczanie1, odliczanie2, odliczanie3;
  26.  
  27. //Utworzenie symboli wyświetlacza LCD (kropla, termometr)
  28.  
  29. byte termometr[] = {
  30.   B00100,
  31.   B01010,
  32.   B01010,
  33.   B01010,
  34.   B01110,
  35.   B11111,
  36.   B11111,
  37.   B01110
  38. };
  39.  
  40. byte kropla[] = {
  41.   B00000,
  42.   B00100,
  43.   B00100,
  44.   B01110,
  45.   B11111,
  46.   B11111,
  47.   B01110,
  48.   B00000
  49. };
  50.  
  51. //Obsługa wyświetlacza
  52.  
  53. void LCD() {
  54.  
  55.  
  56.   switch (tryb) {
  57.  
  58.     case 0:
  59.       digitalWrite(podswietlenie_LCD, LOW);
  60.       lcd.clear();
  61.       lcd.begin(16, 2);
  62.       lcd.setCursor(0, 0);
  63.       lcd.print("");
  64.       analogWrite(czerwony, 0);
  65.       analogWrite(zielony, 0);
  66.       analogWrite(niebieski, 0);
  67.  
  68.       Serial.print("Tryb: "); Serial.println(tryb);
  69.       break;
  70.  
  71.     case 1:
  72.       czujnik_DHT();
  73.       dioda_RGB();
  74.  
  75.       digitalWrite(podswietlenie_LCD, HIGH);
  76.  
  77.       lcd.clear();
  78.       lcd.begin(16, 2);
  79.       lcd.setCursor(0, 0);
  80.       lcd.print(rtc.getTimeStr());
  81.       lcd.setCursor(0, 1);
  82.       lcd.print(rtc.getDateStr());
  83.  
  84.       Serial.print("Tryb: "); Serial.println(tryb);
  85.       break;
  86.  
  87.     case 2:
  88.       czujnik_DHT();
  89.       dioda_RGB();
  90.  
  91.  
  92.       digitalWrite(podswietlenie_LCD, HIGH);
  93.  
  94.       lcd.begin(16, 2);
  95.       lcd.setCursor(0, 0);
  96.       lcd.write(1);
  97.       lcd.setCursor(0, 1);
  98.       lcd.write(2);
  99.       lcd.setCursor(14, 1);
  100.       lcd.print("%");
  101.  
  102.       // Kontrola pomiaru czujnika
  103.  
  104.  
  105.       lcd.setCursor(13, 0);
  106.       lcd.print(temp);
  107.  
  108.       lcd.setCursor(12, 1);
  109.       lcd.print(wilg);
  110.  
  111.  
  112.       Serial.print("Tryb: "); Serial.println(tryb);
  113.       break;
  114.  
  115.   /*  case 3:
  116.       czujnik_DHT();
  117.       dioda_RGB();
  118.  
  119.       digitalWrite(podswietlenie_LCD, HIGH);
  120.  
  121.       lcd.clear();
  122.       lcd.begin(16, 2);
  123.       lcd.setCursor(0, 0);
  124.       lcd.print(rtc.getDateStr());
  125.       lcd.print(" : ");
  126.       lcd.println(detekcja);
  127.  
  128.       Serial.print("Tryb: "); Serial.println(tryb);*/
  129.       break;
  130.  
  131.  
  132.   }
  133.  
  134.  
  135. }
  136.  
  137. //Odczyt temperatury oraz wilgotności
  138.  
  139. void czujnik_DHT() {
  140.  
  141.   int t, w;
  142.   t = dht.getHumidity();
  143.   w = dht.getTemperature();
  144.  
  145.   if (dht.getStatusString() == "OK") {
  146.     wilg = dht.getHumidity();
  147.     temp = dht.getTemperature();
  148.   }
  149. }
  150.  
  151. //obsługa diody RGB
  152.  
  153. void dioda_RGB() {
  154.  
  155.   if (temp < 12)
  156.   {
  157.     analogWrite(czerwony, 0);
  158.     analogWrite(zielony, 0);
  159.     analogWrite(niebieski, 255);
  160.   } else if (temp >= 10 && temp < 18) {
  161.     analogWrite(czerwony, 0);
  162.     analogWrite(zielony, 128);
  163.     analogWrite(niebieski, 255);
  164.   } else if (temp >= 18 && temp < 24) {
  165.     analogWrite(czerwony, 192);
  166.     analogWrite(zielony, 64);
  167.     analogWrite(niebieski, 0);
  168.   } else if (temp >= 24) {
  169.     analogWrite(czerwony, 255);
  170.     analogWrite(zielony, 0);
  171.     analogWrite(niebieski, 0);
  172.   }
  173.  
  174.  
  175. }
  176.  
  177.  
  178.  
  179. void setup() {
  180.   Serial.begin(9600);
  181.  
  182.   Wire.begin();
  183.   rtc.begin();
  184.  
  185.   dht.setup(DHT11_PIN);
  186.  
  187.   lcd.createChar(1, termometr);
  188.   lcd.createChar(2, kropla);
  189.  
  190.   pinMode(przycisk1, INPUT_PULLUP);
  191.   pinMode(czerwony, OUTPUT);
  192.   pinMode(niebieski, OUTPUT);
  193.   pinMode(zielony, OUTPUT);
  194.   pinMode(PIR, INPUT);
  195.   pinMode(podswietlenie_LCD, OUTPUT);
  196.  
  197.   LCD();
  198. //Jednorazowe ustawianie daty (moduł RTC)
  199.  
  200.   //rtc.setTime(20, 46, 00);
  201.   //rtc.setDate(17, 3, 2020);
  202.   //rtc.setDOW(TUESDAY);
  203. }
  204.  
  205.  
  206. void loop() {
  207.  
  208.   // Aktywacja przez czujnik ruchu
  209.  
  210.   if (digitalRead(PIR) == HIGH) {
  211.  
  212.     odliczanie1 = millis();
  213.  
  214.  
  215.     Serial.println("!Aktywacja czujnika ruchu!");
  216.  
  217.     if (digitalRead(przycisk1) == LOW) {
  218.  
  219.       odliczanie1 = millis();
  220.  
  221.  
  222.       if (tryb < 3) {
  223.  
  224.         tryb++;
  225.  
  226.       } else if (tryb == 3) {
  227.         tryb = 1;
  228.  
  229.       }
  230.  
  231.       Serial.println("!Wcisniecie przycisku!");
  232.  
  233.     } else if (digitalRead(przycisk1) == HIGH && tryb == 0) {
  234.  
  235.       tryb = 1;
  236.  
  237.     }
  238.     LCD();
  239.     delay(100);
  240.   }
  241.   //Aktywacja przez przycisk
  242.  
  243.   if (digitalRead(przycisk1) == LOW) {
  244.  
  245.     odliczanie1 = millis();
  246.  
  247.     if (tryb < 3) {
  248.  
  249.       tryb++;
  250.  
  251.     } else if (tryb == 3) {
  252.       tryb = 1;
  253.     }
  254.     LCD();
  255.  
  256.     Serial.println("!Wcisniecie przycisku!");
  257.   }
  258.  
  259.   //Wyłączenie podświetlenia
  260.  
  261.   odliczanie2 = millis();
  262.  
  263.   if (odliczanie1 >= odliczanie2 - 10000 && odliczanie1 <= odliczanie2 - 9000) {
  264.  
  265.     tryb = 0;
  266.     LCD();
  267.     delay(100);
  268.  
  269.   }
  270.  
  271.   LCD();
  272.   Serial.println(temp);
  273.   delay(500);
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement