#include #include LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display. ACHTUNG: Adresse kann auch 0x3F sein !!! // Anschlüsse: // GND - GND // VCC - 5V // SDA - ANALOG Pin 4 // SCL - ANALOG pin 5 int sensor = 2; unsigned long currentTime; unsigned long lastTime; unsigned long period; unsigned long time_passed_min; unsigned long pulse_freq; unsigned long oldTime; float velocity; // =========================== // ======= SETUP ========= // =========================== void setup() { pinMode(sensor, INPUT); Serial.begin(9600); attachInterrupt(digitalPinToInterrupt(sensor), pulse, RISING); // Setup Interrupt lcd.begin(); // initialize the lcd lcd.backlight(); lcd.setCursor(0,0); lcd.print("Anemometer"); delay(3000); lcd.setCursor(0,0); lcd.print(" "); lastTime = micros(); time_passed_min = 10000; // miniumum time in µsec passed after the last interrupt oldTime = 0; } // ======================== // ======= LOOP ========= // ======================== void loop () { if (millis() - oldTime > 1000) { lcd.setCursor(0,0); lcd.print("f = "); lcd.print(pulse_freq); lcd.print(" Hz "); if (pulse_freq = 0) { velocity = 0.0; } else { velocity = pulse_freq * 0.2361 + 1,1557; // conversion of pulse-frequency to wind-velocity } lcd.setCursor(0,1); lcd.print("v = "); lcd.print(velocity, 1); lcd.print(" m/s "); oldTime = oldTime + 1000; pulse_freq = 0; } } // ============================= // ======= INTERRUPT ========= // ============================= void pulse () // Interrupt function { currentTime = micros(); if (currentTime - lastTime > time_passed_min) { pulse_freq++; lastTime = currentTime; } }