YannickF

WetterstationYannickFuchs

Mar 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <Adafruit_Sensor.h>
  4. #include <Adafruit_BMP280.h>
  5. #include <AM2320.h>
  6. #include <OneWire.h>
  7. #include <DallasTemperature.h>
  8. #include <LiquidCrystal.h>
  9.  
  10. //LiquidCrystall
  11. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  12. //DS18B20
  13. #define ONE_WIRE_BUS 2
  14.  
  15. OneWire oneWire(ONE_WIRE_BUS);
  16. DallasTemperature sensors(&oneWire);
  17.  
  18. #define RIGHT  0
  19. #define UP     1
  20. #define DOWN   2
  21. #define LEFT   3
  22. #define SELECT 4
  23. #define NONE   5
  24.  
  25. AM2320 th;
  26. Adafruit_BMP280 bme; // I2C
  27.  
  28. int temp, hum, pres;
  29. int vent =24;
  30. int lcd_key     = 0;
  31. int adc_key_in  = 0;
  32. long previousMillis =0;
  33. int interval = 750;
  34. bool boolTemp=true, boolHum=true, boolPres=true;
  35.  
  36. int read_LCD_buttons(){            
  37.     adc_key_in = analogRead(0);
  38.    
  39.     if (adc_key_in > 1000) return NONE;
  40.     if (adc_key_in < 50)   return RIGHT;
  41.     if (adc_key_in < 150)  return UP;
  42.     if (adc_key_in < 300)  return DOWN;
  43.     if (adc_key_in < 450)  return LEFT;
  44.     if (adc_key_in < 700)  return SELECT;
  45.    
  46.     return NONE;
  47. }
  48.  
  49. void setup() {
  50.   lcd.begin(16,2);
  51.   pinMode(11, OUTPUT);
  52.   Serial.begin(9600);
  53.   Serial.println(F("BMP280 test"));
  54.  
  55.   if (!bme.begin(0x76)) {  
  56.     Serial.println("Could not find a valid BMP280 sensor, check wiring!");
  57.     while (1);
  58.   }
  59. }
  60.  
  61. void loop() {
  62.   unsigned long currentMillis = millis();
  63.  
  64.   //Knöpfe
  65.   lcdkey();
  66.  
  67.   //DS18B20
  68.     sensors.requestTemperatures();
  69.     temp = sensors.getTempCByIndex(0);
  70.     if(temp== -127){
  71.       temp = th.t;
  72.     }
  73.     Serial.print("Temp: ");
  74.     Serial.print(temp);
  75.     Serial.print("*C");
  76.     Serial.println();
  77.    
  78.   //AM2320B
  79.     switch(th.Read()) {
  80.     case 2:
  81.       Serial.println("CRC failed");
  82.       break;
  83.     case 1:
  84.       Serial.println("Sensor offline");
  85.       break;
  86.     case 0:
  87.       hum = th.h;
  88.       Serial.print("hum: ");
  89.       Serial.print(hum);
  90.       Serial.println();
  91.       break;
  92.       }
  93.   //BMP280
  94.     pres=bme.readPressure();
  95.     Serial.print("Pres: ");
  96.     Serial.print(pres);
  97.     Serial.println(" Pa");
  98.  
  99.   //DisplayAusgabe
  100.     lcd.setCursor(0,1);
  101.     if(boolTemp==true){
  102.     lcd.print("temp: ");
  103.     lcd.print(temp);
  104.     lcd.print("*C  ");
  105.     }
  106.     if(boolHum==true){
  107.     lcd.print("hum: ");
  108.     lcd.print(hum);
  109.     lcd.print("%  ");
  110.     }
  111.     if(boolPres==true){
  112.     lcd.print("pres: ");
  113.     lcd.print(pres);
  114.     lcd.print("Pa  ");
  115.     }
  116.  
  117.     lcd.setCursor(0,0);
  118.     lcd.print("Vent ab: ");
  119.     lcd.print(vent);
  120.     lcd.print(" C");
  121.  
  122.     /*if(millis()%500==0){
  123.       if(positionCounter<18){
  124.         lcd.scrollDisplayLeft();
  125.         positionCounter++;
  126.       }else{
  127.         positionCounter=0;
  128.       }
  129.     }*/
  130.  
  131.    int subMillis = currentMillis - previousMillis;
  132.    Serial.println(subMillis);
  133.    
  134.    if(subMillis > interval){
  135.     previousMillis = currentMillis;
  136.     for (int positionCounter = 0; positionCounter < 1; positionCounter) {
  137.       // scrolled eins nach links
  138.       lcd.scrollDisplayLeft();
  139.       positionCounter++;
  140.     }
  141.    }
  142.    
  143.     if(temp<vent){
  144.       digitalWrite(11, LOW);
  145.     }
  146.     if(temp>=vent){
  147.       digitalWrite(11, HIGH);
  148.     }
  149. }
  150.  
  151. void lcdkey(){
  152.   lcd_key = read_LCD_buttons();
  153.  
  154.   switch(lcd_key){
  155.     case UP:{
  156.       vent++;
  157.       break;
  158.     }
  159.     case DOWN:{
  160.       vent--;
  161.       break;
  162.     }
  163.   }
  164.  
  165.   lcd.setCursor(0,0);
  166.   lcd.print("Vent ab: ");
  167.   lcd.print(vent);
  168.   lcd.print(" C");
  169. }
Advertisement
Add Comment
Please, Sign In to add comment