📈 نمودار زنده روی OLED
پتانسیومتر روی A0 به شکل یک خط متحرک روی OLED کشیده میشود.
سطح: متوسط ⭐⭐ · مود: آردوینو (سهبعدی + کد)
قطعات لازم
کد کامل پروژه
// Live graph: the knob on A0 drawn as a line that scrolls across the OLED.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
int points[128]; // one y value for every column of pixels
int count = 0; // how many points we have so far
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
int value = analogRead(A0); // 0..1023
int y = map(value, 0, 1023, 63, 12); // big value = high on screen
if (count < 128) {
points[count] = y; // still filling the screen
count++;
} else {
for (int i = 0; i < 127; i++) { // shift everything one step left
points[i] = points[i + 1];
}
points[127] = y; // newest point on the right
}
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("A0 = ");
display.print(value);
for (int x = 1; x < count; x++) { // join each point to the one before
display.drawLine(x - 1, points[x - 1], x, points[x], SSD1306_WHITE);
}
display.display();
delay(50);
}
چطور اجرایش کنم؟
- شبیهساز تینکرکد فارسی را باز کن و «مود آردوینو» را انتخاب کن.
- از پنل «نمونهها»، «نمودار زنده روی OLED» را بارگذاری کن — مدار و کد با هم میآیند.
- دکمهی اجرا ▶ را بزن و خروجی Serial و رفتار قطعات را تماشا کن.
- حالا کد را تغییر بده — عددها، پینها، ترتیب — و دوباره اجرا کن!