// Kitaplık Yöneticisi'nden kurulacaklar: // "Adafruit BME280 Library" (sorduğu ek // kitaplıklarla) ve "LiquidCrystal I2C" #include #include #include // Sayfa değiştiren buton const int BUTTON_PIN = 2; // İki ölçüm arasındaki süre (ms) const unsigned long READ_EVERY = 2000; // Bulunduğun yerin deniz seviyesindeki // basıncı (hPa): hava durumundaki // basıncı yazarsan rakım daha doğru çıkar const float SEA_LEVEL_HPA = 1013.25; // I2C adresi çoğunda 0x27, bazısında 0x3F LiquidCrystal_I2C lcd(0x27, 16, 2); Adafruit_BME280 bme; // Sayfalar: sıcaklık, nem, basınç, rakım const int PAGES = 4; int page = 0; float temperature = 0; float humidity = 0; float pressure = 0; float altitude = 0; unsigned long lastRead = 0; bool lastButton = HIGH; unsigned long lastChange = 0; // Bir satırı yazar, kalanını boşlukla // siler (ekran titremez) void printLine(int row, const char *text) { lcd.setCursor(0, row); int n = 0; while (text[n] && n < 16) lcd.print(text[n++]); while (n++ < 16) lcd.print(' '); } // Sensörün kimliği: BME280'de 0x60, // BMP280'de 0x58, hiç yoksa 0 byte chipId(byte address) { Wire.beginTransmission(address); Wire.write(0xD0); if (Wire.endTransmission() != 0) return 0; Wire.requestFrom((int)address, 1); return Wire.available() ? Wire.read() : 0; } // Sensör bulunamayınca nedenini yazar void showSensorError() { byte id = chipId(0x76); if (id == 0) id = chipId(0x77); if (id == 0x58) { printLine(0, "Bu bir BMP280"); printLine(1, "Nem olcmez"); Serial.println("Sensör BMP280: nem ölçmez, BME280 gerekir."); } else { printLine(0, "Sensor yok"); printLine(1, "Kablolara bak"); Serial.println("BME280 bulunamadı: SDA, SCL, VIN, GND kablolarına bak."); } } // Modüle göre adres 0x76 ya da 0x77 bool startSensor() { return bme.begin(0x76) || bme.begin(0x77); } void readSensor() { bme.takeForcedMeasurement(); temperature = bme.readTemperature(); humidity = bme.readHumidity(); pressure = bme.readPressure() / 100.0; // Yükseldikçe basınç düşer (alçakta // her ~8 m'de 1 hPa); rakım buradan altitude = 44330.0 * (1.0 - pow(pressure / SEA_LEVEL_HPA, 0.1903)); Serial.print("Sıcaklık: "); Serial.print(temperature, 1); Serial.print(" °C Nem: %"); Serial.print(humidity, 0); Serial.print(" Basınç: "); Serial.print(pressure, 1); Serial.print(" hPa Rakım: "); Serial.print(altitude, 0); Serial.println(" m"); } void showPage() { // Üst satır: sayfanın adı ve numarası const char *titles[PAGES] = {"Sicaklik", "Nem", "Basinc", "Rakim"}; char top[17]; snprintf(top, sizeof(top), "%-12s%d/%d", titles[page], page + 1, PAGES); printLine(0, top); // Alt satır: değer ve birimi char value[10]; char bottom[17]; if (page == 0) { dtostrf(temperature, 1, 1, value); // \xDF ekranın derece işareti snprintf(bottom, sizeof(bottom), "%s \xDF" "C", value); } else if (page == 1) { dtostrf(humidity, 1, 0, value); snprintf(bottom, sizeof(bottom), "%%%s", value); } else if (page == 2) { dtostrf(pressure, 1, 1, value); snprintf(bottom, sizeof(bottom), "%s hPa", value); } else { dtostrf(altitude, 1, 0, value); snprintf(bottom, sizeof(bottom), "yaklasik %s m", value); } printLine(1, bottom); } void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); lcd.init(); lcd.backlight(); // Sensör yoksa bulunana dek dener; // kabloyu düzeltince kendisi başlar while (!startSensor()) { showSensorError(); delay(2000); } // Hava istasyonu ayarı: istenince bir // kez ölçer, arada uyur; ısınmaz, // sıcaklığı yüksek göstermez bme.setSampling(Adafruit_BME280::MODE_FORCED, Adafruit_BME280::SAMPLING_X1, Adafruit_BME280::SAMPLING_X1, Adafruit_BME280::SAMPLING_X1, Adafruit_BME280::FILTER_OFF); readSensor(); showPage(); lastRead = millis(); } void loop() { // Butona basınca sonraki sayfa bool button = digitalRead(BUTTON_PIN); if (button != lastButton && millis() - lastChange > 30) { lastChange = millis(); lastButton = button; if (button == LOW) { page = (page + 1) % PAGES; showPage(); } } if (millis() - lastRead >= READ_EVERY) { lastRead = millis(); readSensor(); showPage(); } }