〰️ میانگین متحرک
پتانسیومتر نویزی روی A0 و یک خروجی صاف.
سطح: چالشی ⭐⭐⭐ · مود: آردوینو (سهبعدی + کد)
قطعات لازم
کد کامل پروژه
const int N = 10; // how many samples to average
int samples[N]; // the last N readings
int idx = 0; // where the next one goes
long total = 0; // running sum of the window
void setup() {
Serial.begin(9600);
}
void loop() {
// the knob plus fake noise, like a real cheap sensor
int raw = analogRead(A0) + random(-40, 41);
raw = constrain(raw, 0, 1023);
total = total - samples[idx]; // drop the oldest
samples[idx] = raw; // store the newest
total = total + raw; // add it to the sum
idx = (idx + 1) % N; // move on, wrap at N
float smooth = total / (float)N;
Serial.print("raw:");
Serial.print(raw);
Serial.print(",smooth:");
Serial.println(smooth);
delay(50);
}
چطور اجرایش کنم؟
- شبیهساز تینکرکد فارسی را باز کن و «مود آردوینو» را انتخاب کن.
- از پنل «نمونهها»، «میانگین متحرک» را بارگذاری کن — مدار و کد با هم میآیند.
- دکمهی اجرا ▶ را بزن و خروجی Serial و رفتار قطعات را تماشا کن.
- حالا کد را تغییر بده — عددها، پینها، ترتیب — و دوباره اجرا کن!