〰️ میانگین متحرک

پتانسیومتر نویزی روی 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);
}

چطور اجرایش کنم؟

  1. شبیه‌ساز تینکرکد فارسی را باز کن و «مود آردوینو» را انتخاب کن.
  2. از پنل «نمونه‌ها»، «میانگین متحرک» را بارگذاری کن — مدار و کد با هم می‌آیند.
  3. دکمه‌ی اجرا ▶ را بزن و خروجی Serial و رفتار قطعات را تماشا کن.
  4. حالا کد را تغییر بده — عددها، پین‌ها، ترتیب — و دوباره اجرا کن!
این پروژه را رایگان در شبیه‌ساز اجرا کن 🤖