// Kitaplık Yöneticisi'nden kurulacaklar:
// "Adafruit SSD1306" ve "Adafruit GFX"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// HC-SR04'ün pinleri
const int TRIG_PIN = 7;
const int ECHO_PIN = 8;
// Grafiğin tavanı (cm): ötesi en üstte
const int MAX_CM = 100;
// Grafik alanı: ekranın alttaki 40 satırı
const int GRAPH_TOP = 24;
const int GRAPH_H = 40;
// Ölçümler arası süre (ms)
const unsigned long MEASURE_EVERY = 100;
// 128x64 ekran; I2C adresi çoğunda 0x3C
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
// Son 128 ölçüm, her ekran sütununa bir
byte history[128];
int newest = 0;
unsigned long lastMeasure = 0;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED bulunamadı");
// Ekran yoksa burada dur
while (true) {}
}
oled.setTextColor(SSD1306_WHITE);
}
// Mesafe (cm); yankı gelmezse -1
int measureCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long echo = pulseIn(ECHO_PIN, HIGH, 30000);
if (echo == 0) {
return -1;
}
return echo / 58;
}
// Üstte mesafe, altta son ölçümler
void draw(int cm) {
oled.clearDisplay();
oled.setTextSize(2);
oled.setCursor(0, 0);
if (cm < 0) {
oled.print("-- cm");
} else {
oled.print(cm);
oled.print(" cm");
}
// En eski ölçüm solda, en yenisi sağda
for (int x = 0; x < 128; x++) {
int h = history[(newest + 1 + x) % 128];
int bottom = GRAPH_TOP + GRAPH_H - 1;
oled.drawFastVLine(x, bottom - h, h + 1, SSD1306_WHITE);
}
oled.display();
}
void loop() {
if (millis() - lastMeasure < MEASURE_EVERY) {
return;
}
lastMeasure = millis();
int cm = measureCm();
// Ölçülemeyen ya da çok uzak: en üstte
int shown = (cm < 0 || cm > MAX_CM) ? MAX_CM : cm;
newest = (newest + 1) % 128;
history[newest] = map(shown, 0, MAX_CM, 0, GRAPH_H - 1);
draw(cm);
if (cm < 0) {
Serial.println("Mesafe: ölçülemedi");
} else {
Serial.print("Mesafe: ");
Serial.print(cm);
Serial.println(" cm");
}
}
Loading
ssd1306
ssd1306