22. April 2018

Arduino-Sketch II

#include <LiquidCrystal.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
#include <Wire.h>
const int analogpin = A0;  // Analog input pin
float  adcvalue = 1.1;
#include "Adafruit_BMP085.h"
Adafruit_BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

const int windPin = 3;          // anschluss des reedkontaktes
const float windFactor = 2.4;   // umrechnungsfaktor von umdrehungen in geschwindigkeit
const int measureTime = 3;      // messzeitraum in sekunden
volatile unsigned int windCounter = 0;  //interner zaehler fŸr umdrehungen
float windSpeed = 0.0;

//initialisieren der variablen fŸr messwerte und die zeitmessung
unsigned long time = 0;

//interrupt service routine fŸr das zaehlen der umdrehungen
void countWind() {
   windCounter ++;
}

// lcd anzeige
void show_values() {
  //erste Zeile – anzahl der gezaehlten umdrehungen im messzeitraum
  lcd.setCursor(0,0);
  lcd.print("Umdrehungen  ");
  lcd.print(windCounter);
  lcd.print("    ");
  //zweite zeile – ermittelte geschwindigkeit
  lcd.setCursor(0,1);
  lcd.print("Wert= ");
  lcd.print(windSpeed);
  lcd.print(" km/h  ");
  delay(5000);
}

void setup() {
  bmp.begin();
  lcd.begin(16, 2);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  lcd.setCursor(0,0);
  lcd.print("Aussen: ");
   lcd.print(t/10,1);
   lcd.print(" ");
  lcd.print('\xDF');
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Wind: ");
  lcd.print("12 km/h");
  delay(5000);
  //starten des messzeitraums
  Serial.print("starting wind-counter for ");
  Serial.print(measureTime);
  Serial.println(" seconds");
  //zaehler auf 0 stellen
  windCounter = 0;
  time = millis();
  //zaehl-interrupt aktivieren
  attachInterrupt(1,countWind,RISING);
  //abwarten des messzeitraums
  delay(1000 * measureTime);
  //zaehl-interrupt deaktivieren
  detachInterrupt(1);
  //zeit bestimmen
  time = (millis() – time) / 1000;
 
  //debug ausgaben
  Serial.print("Time elapsed : ");
  Serial.println(time);
  Serial.print("Wind counts = ");
  Serial.println(windCounter);
  //berechnen der geschwindigkeit
  windSpeed = (float)windCounter / (float)measureTime * windFactor;
  Serial.print("Wind Speed = ");
  Serial.print(windSpeed);
  Serial.println(" km/h");
  //lcs ausgabe aktualisieren
  show_values(); 
}

void loop() {
  int x;
  x = analogRead (0);
  lcd.setCursor(10,1);
  if (x < 100) {
   lcd.clear();
    lcd.setCursor(0,0);
  
    lcd.print("Innen: ");
    lcd.print (bmp.readTemperature()-1);
    lcd.print(" ");
    lcd.print('\xDF');
    lcd.print("C");
    lcd.setCursor(0,1);
    lcd.print("Baro: ");
    lcd.print (bmp.readPressure()/91.99,1 );
    lcd.print(" hPa");
    delay(5000);
  }
  else if (x < 200) {
    lcd.print (bmp.readTemperature()-1);
  }
  else if (x < 400){
    lcd.print ("Down  ");
  }
  else if (x < 600){
    lcd.print ("Left  ");
  }
  else if (x < 800){
    lcd.print ("Select");
  }
}