chrisdaloa

Arduino test 1

Oct 21st, 2017
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1.  
  2.  
  3. //www.elegoo.com
  4. //2016.12.9
  5.  
  6. /*
  7.   LiquidCrystal Library - Hello World
  8.  
  9.  Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  10.  library works with all LCD displays that are compatible with the
  11.  Hitachi HD44780 driver. There are many of them out there, and you
  12.  can usually tell them by the 16-pin interface.
  13.  
  14.  This sketch prints "Hello World!" to the LCD
  15.  and shows the time.
  16.  
  17.   The circuit:
  18.  * LCD RS pin to digital pin 7
  19.  * LCD Enable pin to digital pin 8
  20.  * LCD D4 pin to digital pin 9
  21.  * LCD D5 pin to digital pin 10
  22.  * LCD D6 pin to digital pin 11
  23.  * LCD D7 pin to digital pin 12
  24.  * LCD R/W pin to ground
  25.  * LCD VSS pin to ground
  26.  * LCD VCC pin to 5V
  27.  * 10K resistor:
  28.  * ends to +5V and ground
  29.  * wiper to LCD VO pin (pin 3)
  30.  
  31.  Library originally added 18 Apr 2008
  32.  by David A. Mellis
  33.  library modified 5 Jul 2009
  34.  by Limor Fried (http://www.ladyada.net)
  35.  example added 9 Jul 2009
  36.  by Tom Igoe
  37.  modified 22 Nov 2010
  38.  by Tom Igoe
  39.  
  40.  This example code is in the public domain.
  41.  
  42.  http://www.arduino.cc/en/Tutorial/LiquidCrystal
  43.  */
  44.  
  45. // include the library code:
  46. #include <LiquidCrystal.h>
  47. #include <DHT.h>
  48.  
  49.  
  50. //definisco il DHT11
  51. #define DHTPIN 2
  52. #define DHTTYPE DHT11
  53. DHT dht(DHTPIN, DHTTYPE);
  54.  
  55. // initialize the library with the numbers of the interface pins
  56. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
  57.  
  58. //definisco i pin del led RGB
  59. #define BLUE 3
  60. #define GREEN 5
  61. #define RED 6
  62.  
  63. // definisco il pin del buzzer
  64. int buzzer = 4;
  65.  
  66. //definisco le variabili di temperature e umidità
  67. int t = dht.readTemperature();
  68. int h = dht.readHumidity();
  69.  
  70. //definisco le variabili che uso per memorizzare i valori precedenti
  71. int to = t;
  72. int ho = h;
  73.  
  74. void setup() {
  75.     // preimposto il contenuto del LCD
  76.     lcd.begin(16, 2);
  77.     lcd.print("Temperatura");
  78.     lcd.setCursor(0,1);
  79.     lcd.print("Umidita'");
  80.  
  81.     //imposto il buzzer su output
  82.     pinMode(buzzer,OUTPUT);
  83.  
  84.     //imposto il led rbg su output
  85.     pinMode(RED,OUTPUT);
  86.     pinMode(GREEN,OUTPUT);
  87.     pinMode(BLUE,OUTPUT);
  88.  
  89.     //imposto il led rgb su spento
  90.     digitalWrite(RED,LOW);
  91.     digitalWrite(GREEN,LOW);
  92.     digitalWrite(BLUE,LOW);
  93.  
  94. }
  95.  
  96. void loop() {
  97.     //leggo temperatura e umidità  
  98.     t = dht.readTemperature();
  99.     h = dht.readHumidity();
  100.  
  101.     // scrivo temperatura e umidità sul display
  102.     lcd.setCursor(13, 0);
  103.     lcd.print(t);
  104.     lcd.setCursor(15, 0);
  105.     lcd.print("C");
  106.  
  107.     lcd.setCursor(13, 1);
  108.     lcd.print(h);
  109.     lcd.setCursor(15, 1);
  110.     lcd.print("%");
  111.  
  112.     if (to != t) {  //se cambia la temperatura buz di 1000
  113.         tone(buzzer,1000,200);    
  114.         if (to <= t){  //se la temperatura è aumentata o è rimasta invariata accendo il led rgb verde
  115.             digitalWrite(RED,LOW);
  116.             digitalWrite(GREEN,HIGH);
  117.             digitalWrite(BLUE,LOW);
  118.         }else{ //se la temperatura è scesa accendo il led rgb rosso
  119.             digitalWrite(RED,HIGH);
  120.             digitalWrite(GREEN,LOW);
  121.             digitalWrite(BLUE,LOW);
  122.         }
  123.     }else{
  124.         if (ho > h+4 || ho < h-4){ //se non è variata la temperatura ma è variata l'umidità di almeno 5 punti percentuali suono 2000 e accendo il led blu
  125.             tone(buzzer,2000,200);    
  126.             digitalWrite(RED,HIGH);
  127.             digitalWrite(GREEN,HIGH);
  128.             digitalWrite(BLUE,LOW);
  129.         }
  130.       }
  131.  
  132.     //salvo i valori precedenti
  133.     to = t;
  134.     ho = h;
  135.  
  136.     //attendo 5 secondi
  137.     delay(5000);
  138. }
Add Comment
Please, Sign In to add comment