#include <Servo.h>
#include <LiquidCrystal.h>
// Inisialisasi komponen
Servo myServo;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Sesuaikan dengan pin LCD kamu
// Array untuk menyimpan nilai analog
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void setup() {
myServo.attach(9); // attaches the servo on pin 9 to the servo object
lcd.begin(16, 2);
}
void loop() {
// Baca nilai analog dari potentiometer
int sensorValue = analogRead(A0);
// Simpan nilai ke dalam array
total = total - readings[index];
readings[index] = sensorValue;
total = total + readings[index];
index = index + 1;
if (index >= numReadings) {
index = 0;
}
average = total / numReadings;
// Atur posisi servo berdasarkan nilai rata-rata, dengan rentang 0-180 derajat
myServo.write(map(average, 0, 1023, 0, 180));
// Tampilkan nilai rata-rata di LCD
lcd.setCursor(0, 0);
lcd.print("Nilai: ");
lcd.print(average);
delay(100);
}