Advertisement
Lorenzos

Serra 0.01 Beta (LCD)

Mar 18th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Blynk
  2. #define BLYNK_PRINT DebugSerial
  3. #include <SoftwareSerial.h>
  4. SoftwareSerial DebugSerial(0, 1); // RX, TX
  5. char auth[] = "5b343a8730db451b810a0916fcc8317e";
  6. #include <BlynkSimpleStream.h>
  7. BlynkTimer timer;
  8. #define BLYNK_PRINT Serial
  9.  
  10. WidgetTerminal terminal(V4);
  11.  
  12. // DHT
  13. #include <DHT.h>
  14. #include "DHT.h"
  15. #define DHTPIN 8
  16. #define DHTTYPE DHT11
  17.  
  18. // Trasmettitore IR
  19. #include <IRremote.h>
  20. IRsend irsend;
  21.  
  22. // LCD
  23. #include <LiquidCrystal.h>
  24. const int rs = 12, en = 11, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
  25. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  26.  
  27.   // Carattere °
  28.     byte simbologradi[8] =
  29.     {
  30.       B00111,
  31.       B00101,
  32.       B00111,
  33.       B00000,
  34.       B00000,
  35.       B00000,
  36.       B00000,
  37.       B00000,
  38.     };
  39.  
  40.   // Carattere %
  41.     byte simbolopercentuale[9] =
  42.     {
  43.       B00000,
  44.       B00000,
  45.       B11001,
  46.       B11010,
  47.       B00100,
  48.       B01011,
  49.       B10011
  50.     };
  51.  
  52.   // Carattere :)
  53.     byte simbolosmile[10] =
  54.     {
  55.       B00000,
  56.       B01010,
  57.       B00000,
  58.       B10001,
  59.       B01110,
  60.       B00000,
  61.       B00000
  62.     };
  63.  
  64.   // Carattere :(
  65.     byte simbolosad[11] =
  66.     {
  67.       B00000,
  68.       B01010,
  69.       B00000,
  70.       B01110,
  71.       B10001,
  72.       B00000,
  73.       B00000
  74.     };
  75.  
  76.   // Carattere :|
  77.     byte simboloneut[12] =
  78.     {
  79.       B00000,
  80.       B01010,
  81.       B00000,
  82.       B00000,
  83.       B11111,
  84.       B00000,
  85.       B00000
  86.     };
  87.  
  88. // Contatore pagine
  89. int pageCounter = 1;
  90.  
  91. // Igrometro
  92. int dryValueRaw = 800;  // Valore raw Igrometro -> Umidità 0
  93. int wetValueRaw = 480;  // Valore raw Igrometro -> Umidità 100
  94.  
  95. int pDryValue =   0;    // Valore percentuale umidità 0
  96. int pWetValue = 100;    // Valore percentuale umidità 100
  97.  
  98. int statoMessErrore = 0;
  99.  
  100. // DHT11
  101. DHT dht (DHTPIN, DHTTYPE);  // Pin DHT e tipo DHT
  102.  
  103. // Pulsante e Debounce
  104. int pinPulsanteDown = 10;
  105. int pinPulsanteUp   = 9;
  106.  
  107. int pinPulsanteLed  = 13;
  108.  
  109. // Debounce Pulsante Led
  110. int statoPulsanteLed = LOW;
  111. int attesaDebounce = 50;
  112. int ultimaLetturaPulsanteLed = LOW;
  113. unsigned long ultimoTempoDebounce = 0;
  114.  
  115.  
  116. bool current_up = LOW;
  117. bool last_up = LOW;
  118. bool last_down = LOW;
  119. bool current_down = LOW;
  120.  
  121. // Illuminazione
  122. int statoLed = 0;
  123.  
  124.  
  125.  
  126. // Blynk Data
  127.    #define PIN_Temp V0
  128.    
  129.     /*BLYNK_READ(PIN_Temp)
  130.     {
  131.       Blynk.virtualWrite(PIN_Temp, millis() / 1000);
  132.     }*/
  133.  
  134. void sendSensor()
  135. {
  136.   int h = dht.readHumidity();
  137.   int t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  138.  
  139.   int ur = analogRead(V0);
  140.  
  141.   int u = map(ur, dryValueRaw, wetValueRaw, pDryValue, pWetValue);
  142.  
  143.   Blynk.virtualWrite(V1, h);
  144.   Blynk.virtualWrite(V0, t);
  145.   Blynk.virtualWrite(V2, u);
  146. }
  147.  
  148. BLYNK_WRITE(V3){
  149.   int pinValue = param.asInt();
  150.  
  151.   if (pinValue = 1){
  152.     irsend.sendNEC(0xFF02FD, 32);
  153.     statoLed = !statoLed;
  154.   }
  155.   else{
  156.     irsend.sendNEC(0xFF02FD, 32);
  157.     statoLed = !statoLed;
  158.   }
  159.  
  160. }
  161.  
  162. void setup() {
  163.   Serial.begin(9600);   // Inizia la comunicazione seriale con il computer
  164.   lcd.begin(16,2);      // Inizializza lcd, 16 caratteri x 2 righe
  165.   lcd.createChar(0, simbologradi);
  166.   lcd.createChar(1, simbolopercentuale);
  167.   lcd.createChar(2, simbolosmile);
  168.   lcd.createChar(3, simbolosad);
  169.   lcd.createChar(4, simboloneut);
  170.  
  171.   pinMode(pinPulsanteDown,INPUT);
  172.   pinMode(pinPulsanteUp,  INPUT);
  173.  
  174.   Blynk.begin(Serial, auth);
  175.   dht.begin();
  176.   timer.setInterval(1000L, sendSensor);
  177.  
  178.     terminal.clear();
  179.     for(int i = 0; i < 5; i++){
  180.       terminal.println((" "));
  181.     }
  182.     terminal.println(("Benvenuto nella tua Serrina!"));
  183.     terminal.println(("-------------------------------"));
  184.  
  185. }
  186.  
  187. // Debounce
  188.   bool debounce(bool last, int pin)
  189.   {
  190.     bool current = digitalRead(pin);
  191.     if(last != current){
  192.       delay(5);
  193.       current = digitalRead(pin);
  194.     }
  195.     return current;
  196.   }
  197.  
  198. void loop() {
  199.   Blynk.run();
  200.   timer.run();
  201.    
  202.  // Umidità terreno
  203.   int rawUmidita = analogRead(0);                                                                 // Legge l'umidità
  204.   int percentualeUmidita = map(rawUmidita, dryValueRaw, wetValueRaw, pDryValue, pWetValue);       // Converte l'umidità in percentuale
  205.  
  206.  // DHT 11 - Temperatura e umidità
  207.   int temperatura = dht.readTemperature();
  208.   int umidita = dht.readHumidity();
  209.  
  210.  // Fotocellula
  211.   int luminosita = analogRead(1);
  212.  
  213.   /*if(luminosita < 550 and statoLed == 0){
  214.     irsend.sendNEC(0xFF02FD, 32);
  215.     statoLed = !statoLed;
  216.    
  217.   }*/
  218.  
  219.   int letturaPulsanteLed = digitalRead(pinPulsanteLed);
  220.  
  221.   if(letturaPulsanteLed != ultimaLetturaPulsanteLed){
  222.     ultimoTempoDebounce = millis();
  223.   }
  224.  
  225.   if((millis() - ultimoTempoDebounce) > attesaDebounce){
  226.     if(letturaPulsanteLed != statoPulsanteLed and letturaPulsanteLed == HIGH){
  227.       irsend.sendNEC(0xFF02FD, 32);
  228.       statoLed = !statoLed;
  229.       delay(100);
  230.     }
  231.     statoPulsanteLed = letturaPulsanteLed;
  232.   }
  233.  
  234.   ultimaLetturaPulsanteLed = letturaPulsanteLed;
  235.  
  236.  // Pagine LCD
  237.   current_up   = debounce(last_up, pinPulsanteUp);
  238.   current_down = debounce(last_down, pinPulsanteDown);
  239.  
  240.   //Page Up
  241.     if (last_up == LOW && current_up == HIGH){  
  242.         lcd.clear();                            
  243.       if(pageCounter <4){                      
  244.         pageCounter= pageCounter +1;            
  245.       }
  246.         else{
  247.           pageCounter= 4;  
  248.         }
  249.      }
  250.  
  251.     last_up = current_up;
  252.  
  253.   //Page Down
  254.     if (last_down == LOW && current_down == HIGH){  
  255.         lcd.clear();                                  
  256.       if(pageCounter >1){                          
  257.         pageCounter= pageCounter -1;                
  258.       }
  259.         else{
  260.           pageCounter= 1;  
  261.         }
  262.       }
  263.    
  264.     last_down = current_down;
  265.  
  266.  //------- Switch pagine---//
  267.   switch (pageCounter) {
  268.  
  269.    // Pagina 1 - Temperatura Aria
  270.     case 1:{
  271.       lcd.setCursor(14,1);
  272.       lcd.print("p1");                
  273.       lcd.setCursor(0,0);
  274.       lcd.print("Temperatura Aria");
  275.       lcd.setCursor(0,1);
  276.       lcd.print(temperatura);
  277.       lcd.setCursor(2,1);
  278.       lcd.write(byte(0));
  279.       lcd.print("C");
  280.     }
  281.     break;
  282.  
  283.   //Pagina 2 - Umidità Aria
  284.     case 2: {
  285.       lcd.setCursor(14,1);
  286.       lcd.print("p2");
  287.       lcd.setCursor(0,0);
  288.       lcd.print("Umidita' Aria");
  289.       lcd.setCursor(0,1);
  290.       lcd.print(umidita);
  291.       lcd.setCursor(2,1);
  292.       lcd.write(byte(1));
  293.     }
  294.     break;
  295.  
  296.   //Pagina 3 - Umidità terreno
  297.     case 3: {  
  298.       lcd.setCursor(0,0);
  299.       lcd.print("Umidita' Terreno");
  300.      
  301.       lcd.setCursor(14,1);
  302.       lcd.print("p3");
  303.  
  304.       if(percentualeUmidita < 30){
  305.         lcd.setCursor(4,1);
  306.         lcd.write(byte(3));
  307.       }
  308.       else if(percentualeUmidita < 50){
  309.         lcd.setCursor(4,1);
  310.         lcd.write(byte(4));
  311.       }
  312.       else if(percentualeUmidita < 85){
  313.         lcd.setCursor(4,1);
  314.         lcd.write(byte(2));
  315.       }
  316.       else{
  317.         lcd.setCursor(4,1);
  318.         lcd.write(byte(4));
  319.       }
  320.      
  321.       if(percentualeUmidita < 10){
  322.         lcd.setCursor(0,1);
  323.         lcd.print(percentualeUmidita);
  324.         lcd.setCursor(1,1);
  325.         lcd.write(byte(1));
  326.         lcd.setCursor(2,1);
  327.         lcd.print(" ");
  328.       }
  329.       else if(percentualeUmidita < 0){
  330.         lcd.setCursor(0,1);
  331.         lcd.print(percentualeUmidita);
  332.         lcd.setCursor(2,1);
  333.         lcd.write(byte(1));
  334.       }
  335.       else {
  336.       lcd.setCursor(0,1);
  337.       lcd.print(percentualeUmidita);
  338.       lcd.setCursor(2,1);
  339.       lcd.write(byte(1));
  340.       }
  341.     }
  342.     break;
  343.  
  344.   //Pagina 4 - Intensità luce
  345.     case 4: {
  346.       lcd.setCursor(14,1);
  347.       lcd.print("p4");
  348.       if(luminosita <1000){
  349.         lcd.setCursor(0,0);
  350.         lcd.print("Luminosita' amb.");
  351.         lcd.setCursor(0,1);
  352.         lcd.print(luminosita);
  353.         lcd.setCursor(3,1);
  354.         lcd.print(" ");
  355.       }
  356.       else {
  357.       lcd.setCursor(0,0);
  358.       lcd.print("Luminosita' amb.");
  359.       lcd.setCursor(0,1);
  360.       lcd.print(luminosita);
  361.       }      
  362.     }
  363.     break;
  364.  
  365.    delay(1000);
  366.   }
  367.  
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement