DeepSeek 写Arduino 代码

#include <Servo.h>

Servo myServo; // 创建舵机对象

int potPin = A0; // 电位器连接到A0引脚
int servoPin = 9; // 舵机信号线连接到9号引脚
int lastAngle = 0; // 记录上一次的有效角度
const int numReadings = 10; // 移动平均滤波的采样次数
int readings[numReadings]; // 存储采样值的数组
int readIndex = 0; // 当前采样值的索引
int total = 0; // 采样值的总和

void setup() {
myServo.attach(servoPin); // 将舵机对象连接到指定的引脚
Serial.begin(9600); // 初始化串口通信,用于调试

// 初始化滤波数组
for (int i = 0; i < numReadings; i++) {
readings[i] = 0;
}
}

void loop() {
// 读取电位器的值
int potValue = analogRead(potPin);

// 移动平均滤波处理
total = total - readings[readIndex]; // 减去最早的采样值
readings[readIndex] = potValue; // 存储当前采样值
total = total + readings[readIndex]; // 加上当前采样值
readIndex = (readIndex + 1) % numReadings; // 更新索引

// 计算平均值
int averageValue = total / numReadings;

// 将滤波后的值映射到舵机角度(0-180度)
int angle = map(averageValue, 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值