Facebook
From Przemek, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 247
  1. // Program odczytuje temperaturę z czujnika
  2.  
  3. #include <OneWire.h>
  4. #include <DS18B20.h>
  5.  
  6.  
  7.  
  8. #include <LiquidCrystal_I2C.h> // dolaczenie pobranej biblioteki I2C dla LCD
  9.  
  10. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Ustawienie adresu ukladu na 0x27
  11.  
  12.  
  13. //////////////////////wyjścia i wejścia/////////////////////////
  14. // Numer pinu do którego podłaczasz czujnik
  15. #define ONEWIRE_PIN 7
  16. int DATA = 8;
  17. int CLK = 9;
  18. int latch = 13;
  19. int D[] = {1, 4, 5, 6, 3};
  20. int DP = 2;
  21. int zielona = 11;
  22. int czerwona = 12;
  23. int wentylator  = 10;
  24. int SW1 = A3;
  25. int SW2 = A2;
  26. int SW3 = A1;
  27. int POT = A0;
  28. int CYFRA[] = {1, 159, 34, 6, 140, 84, 64, 15, 16, 4};
  29. ///////////////////////////////////////////////////////////////////
  30.  
  31.  
  32. ////////////zmienne//////////////
  33.  
  34. float temperature = 0;
  35. int Rtemp = 0;
  36. int tempzadana = 0;
  37.  
  38. int warhisterezy = 40;
  39.  
  40. int cyfra0 = 0;
  41. int tempRZ[] = {0, 0, 0};
  42. int tempZA[] = {0, 0, 0};
  43.  
  44. int i = 0;
  45. int drganiestykow = 150;
  46. int tempzadanaLCD = 0;
  47.  
  48. int coilepomiar = 20;
  49. int pomiarbyl = coilepomiar + 1;
  50. int nowa = 11;
  51.  
  52.  
  53. // Adres czujnika
  54. byte address[8] = {0x28, 0xFF, 0x82, 0xCF, 0x64, 0x15, 0x1, 0xD5};
  55.  
  56. OneWire onewire(ONEWIRE_PIN);
  57. DS18B20 sensors(&onewire);
  58.  
  59.  
  60. void setup() {
  61.   while (!Serial);
  62.   Serial.begin(9600);
  63.  
  64.   lcd.begin(16, 2);  // Inicjalizacja LCD 2x16
  65.  
  66.   lcd.backlight(); // zalaczenie podwietlenia
  67.  
  68.   sensors.begin(9);
  69.   sensors.request(address);
  70.   pinMode(wentylator, OUTPUT);
  71.   pinMode(DATA, OUTPUT);
  72.   pinMode(CLK, OUTPUT);
  73.   pinMode(latch, OUTPUT);
  74.   pinMode(D[1], OUTPUT);
  75.   pinMode(D[2], OUTPUT);
  76.   pinMode(D[3], OUTPUT);
  77.   pinMode(D[4], OUTPUT);
  78.   pinMode(DP, OUTPUT);
  79.   pinMode(zielona, OUTPUT);
  80.   pinMode(czerwona, OUTPUT);
  81.   pinMode(SW1, INPUT);
  82.   pinMode(SW2, INPUT);
  83.   pinMode(SW3, INPUT);
  84.   pinMode(POT, INPUT);
  85. }
  86.  
  87.  
  88. //////////////ODCZYT TEMPERATURY //////////////
  89. void odczyttemp() {
  90.   if (sensors.available())
  91.   {
  92.  
  93.     if (pomiarbyl > coilepomiar)
  94.     {
  95.       temperature = (sensors.readTemperature(address)) * 10;
  96.       //Rtemp = temperature;
  97.       Rtemp = 123;
  98.       sensors.request(address);
  99.  
  100.       pomiarbyl = 0;
  101.     }
  102.   }
  103.   pomiarbyl++;
  104.   //Serial.println(pomiarbyl);
  105. }
  106.  
  107. ////////////// USTAWIENIE TEMPERATURY //////////////
  108. void ustawienietemp() {
  109.   int odczytSW1 = analogRead(SW1);
  110.   int odczytSW2 = analogRead(SW2);
  111.   if (odczytSW1 > 800) {
  112.     delay(drganiestykow);
  113.     tempzadana = tempzadana + 5;
  114.   }
  115.   if (odczytSW2 > 800) {
  116.     delay(drganiestykow);
  117.     tempzadana = tempzadana - 5;
  118.   }
  119.   tempzadanaLCD = tempzadana;
  120.   //if(tempzadana%10<5)
  121.   //tempzadanaLCD = tempzadana - tempzadana%10;
  122.   //else
  123.   //tempzadanaLCD = tempzadana +(10-tempzadana%10);
  124.  
  125.  
  126.  
  127. }
  128.  
  129. ////////////// URUCHOMIENIE WENTYLATORA //////////////
  130. void uruchomieniewent() {
  131.   if (tempzadana > Rtemp) {
  132.     digitalWrite(wentylator, HIGH);
  133.   }
  134.   if (tempzadana < Rtemp - warhisterezy) {
  135.     digitalWrite(wentylator, LOW);
  136.   }
  137.  
  138. }
  139.  
  140. ////////////// WYŚWIETLENIE TEMPERATURY //////////////
  141. void wyswietlanietemp() {
  142.  
  143.   ///////////////wyodrębnienie cyf////////////////
  144.  
  145.  
  146.  
  147.   tempZA[0] = Rtemp % 100;
  148.   tempZA[0] = (Rtemp - tempZA[0]) / 100;
  149.   tempZA[1] = Rtemp % 10;
  150.   tempZA[1] = (Rtemp - (tempZA[0] * 100) - tempZA[1]) / 10;
  151.   tempZA[2] = Rtemp - (tempZA[0] * 100) - (tempZA[1] * 10);
  152.  
  153.   tempRZ[0] = tempzadanaLCD % 100;
  154.   tempRZ[0] = (tempzadanaLCD - tempRZ[0]) / 100;
  155.   tempRZ[1] = tempzadanaLCD % 10;
  156.   tempRZ[1] = (tempzadanaLCD - (tempRZ[0] * 100) - tempRZ[1]) / 10;
  157.   tempRZ[2] = tempzadanaLCD - (tempRZ[0] * 100) - (tempRZ[1] * 10);
  158.  
  159.   //////////////////wyswietlenie na 7 seg/////////////
  160.   int czas = 5;
  161.   digitalWrite(D[1], LOW);
  162.   digitalWrite(D[2], LOW);
  163.   digitalWrite(D[3], LOW);
  164.   digitalWrite(D[4], LOW);
  165.  
  166.   digitalWrite(latch, LOW);
  167.   shiftOut(DATA, CLK, LSBFIRST, CYFRA[tempRZ[0]]);
  168.   digitalWrite(latch, HIGH);
  169.   digitalWrite(D[1], LOW);
  170.   digitalWrite(D[2], HIGH);
  171.   digitalWrite(D[3], LOW);
  172.   digitalWrite(D[4], LOW);
  173.  
  174.   delay(czas);
  175.  
  176.   digitalWrite(D[1], LOW);
  177.   digitalWrite(D[2], LOW);
  178.   digitalWrite(D[3], LOW);
  179.   digitalWrite(D[4], LOW);
  180.  
  181.   digitalWrite(latch, LOW);
  182.   shiftOut(DATA, CLK, LSBFIRST, CYFRA[tempRZ[1]]);
  183.   digitalWrite(latch, HIGH);
  184.   digitalWrite(D[1], LOW);
  185.   digitalWrite(D[2], LOW);
  186.   digitalWrite(D[3], HIGH);
  187.   digitalWrite(D[4], LOW);
  188.   delay(czas);
  189.  
  190.  
  191.   digitalWrite(D[1], LOW);
  192.   digitalWrite(D[2], LOW);
  193.   digitalWrite(D[3], LOW);
  194.   digitalWrite(D[4], LOW);
  195.  
  196.   digitalWrite(latch, LOW);
  197.   shiftOut(DATA, CLK, LSBFIRST, CYFRA[tempRZ[2]]);
  198.   digitalWrite(latch, HIGH);
  199.  
  200.   digitalWrite(D[1], LOW);
  201.   digitalWrite(D[2], LOW);
  202.   digitalWrite(D[3], LOW);
  203.   digitalWrite(D[4], HIGH);
  204.   delay(czas);
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.   ////////////////////wyświetlanie na lcd//////////////
  212.  
  213.   if ( nowa > 40) {
  214.     lcd.setCursor(0, 0);
  215.     lcd.print("tem zad");
  216.  
  217.     lcd.setCursor(10, 0);
  218.     lcd.print(tempZA[0]);
  219.     lcd.print(tempZA[1]);
  220.     lcd.print(".");
  221.     lcd.print(tempZA[2]);
  222.     lcd.setCursor(14, 0);
  223.     lcd.print((char)223);
  224.     lcd.print("C");
  225.  
  226.     lcd.setCursor(0, 1);
  227.     lcd.print("tem fakty");
  228.     lcd.setCursor(10, 1);
  229.     lcd.print(tempRZ[0]);
  230.     lcd.print(tempRZ[1]);
  231.     lcd.print(".");
  232.     lcd.print(tempRZ[2]);
  233.     lcd.setCursor(14, 1);
  234.     lcd.print((char)223);
  235.     lcd.print("C");
  236.     nowa = 1;
  237.   }
  238.   nowa = nowa + 1;
  239.   Serial.println(nowa);
  240. }
  241.  
  242. //////////////GŁÓWNA FUNKCJA //////////////
  243. void loop() {
  244.  
  245.  
  246.   odczyttemp();
  247.  
  248.   ustawienietemp();
  249.  
  250.   uruchomieniewent();
  251.  
  252.   wyswietlanietemp();
  253.  
  254.  
  255.  
  256. }