Arduino电位器时间控制

时间:2013-05-31 08:10:40

标签: c++ time arduino

我想通过电位计设置时间间隔。我想从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

2 个答案:

答案 0 :(得分:5)

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

你说你希望范围1到6秒(不是0到6?)。为了使其适当地缩放到底池,你需要将1023除以6(如果你需要0到6的范围,则为7)。所以:

1023 / 6 = 170.5

因此您需要使用:

analogWrite(led,potWert/170.5);

假设您希望它精确到十毫秒单位

 1023 / 600 = 1.705

因此:

analogWrite(led,potWert/1.705);

答案 1 :(得分:0)

这是困难的方法,并使代码难以阅读。使用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);
}