void setup() {
Serial.begin(9600); // 初始化串口通信
}
void loop() {
static float angle = 0; // 角度变量
static const float step = 0.1; // 角度增量
// 计算正弦值(范围0-1)
float sinValue = (sin(angle) + 1) / 2; // 调整到0-1范围
// 输出到串口绘图仪
Serial.println(sinValue * 100); // 放大到0-100范围
angle += step; // 增加角度
if(angle >= 2*PI) angle = 0; // 重置角度
delay(50); // 控制波形刷新速度
}
运行结果
