gpt4 book ai didi

c++ - Arduino电位器时间控制

转载 作者:行者123 更新时间:2023-11-30 01:22:11 24 4
gpt4 key购买 nike

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

最佳答案

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);

关于c++ - Arduino电位器时间控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16852695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com