Arduino电位器时间控制

Arduino potentiometer time control

本文关键字:控制 时间 电位器 Arduino      更新时间:2023-10-16

我想通过电位器设置一个时间间隔。我想在1到6秒之间选择。我该怎么做呢?

到目前为止我有这个。如果我使用电位器,光就会由亮变暗。

(我使用Arduino Uno和我在c++编程)。

const byte pot = 0;
int potWert=0;
potWert = analogRead(pot);  
analogWrite(led,potWert/4);
Serial.println((byte)potWert); //just for output on the serial monitor

analoread读取范围0到1023您需要将其缩放到您的时间间隔

你说你想要的范围是1到6秒(不是0到6?)要使其适当地缩放到锅中,您需要将1023除以6(如果需要范围为0到6,则为7)。

1023 / 6 = 170.5

因此你需要使用:

analogWrite(led,potWert/170.5);

假设你想要精确到10毫秒单位

 1023 / 600 = 1.705

:

analogWrite(led,potWert/1.705);

这是一种困难的方法,使代码难以阅读。使用map语句。下面是Arduino参考中的一个示例:

例子
/* Map an analog value to 8 bits (0 to 255) */
void setup() {}
void loop()
{
  int val = analogRead(0);
  val = map(val, 0, 1023, 0, 255);
  analogWrite(9, val);
}