这段代码似乎卡在了第一个 while 循环中

This code seems to get stuck on the first while loop

我的 arduino 代码似乎卡在了第一个 while 循环中。它有什么问题? 它应该根据从 1V 到 5V 的电压水平分别打开绿色、黄色和红色 LED。

0.01V - 2.9V 是绿色 LED
3.0V - 3.9V 为黄色 LED
4.0V - 5.0V 为红色 LED

int readPin = A1;
int readVal;
float Voltage;
int green = 8;
int yellow = 9;
int red = 10;
int delayT = 100;


 void setup() {
  // put your setup code here, to run once:
 pinMode(A1, INPUT);
 pinMode(green, OUTPUT);
 pinMode(yellow, OUTPUT);
 pinMode(red, OUTPUT);
 }

 void loop() {
  // put your main code here, to run repeatedly:
 readVal = analogRead(readPin);
 Voltage = (readVal/1023.) * 5.;
 

while (Voltage >= 0.01 && Voltage < 3.0){
 digitalWrite(green, HIGH);
 Voltage = (readVal/1023.) * 5.;
 }
 digitalWrite(green, LOW);

while (Voltage >= 3.0 && Voltage < 4.0){
 digitalWrite(yellow, HIGH);
 Voltage = (readVal/1023.) * 5;
 }
 digitalWrite(yellow, LOW);

while (Voltage >= 4.0 && Voltage <= 5.0){
 digitalWrite(red, HIGH);
 Voltage = (readVal/1023.) * 5;
 }
 digitalWrite(red, LOW);

delay(delayT);

 }

在您的 while 循环中,您没有更新 readVal 的值,因此它在 while 循环的每个循环中都保持不变。

考虑在 while 循环中添加 readVal = analogRead(readPin);

另外..你可能真的不需要 while 循环,只需执行 if 语句来触发每个条件:

void loop() {
 readVal = analogRead(readPin);
 Voltage = (readVal/1023.) * 5.;
 

if(Voltage >= 4.0 && Voltage <= 5.0){
 digitalWrite(red, HIGH);
 digitalWrite(green, LOW);
 digitalWrite(yellow, LOW);
 }
else if(Voltage >= 3.0 && Voltage < 4.0){
 digitalWrite(yellow, HIGH);
 digitalWrite(green, LOW);
 digitalWrite(read, LOW);
 }
else if (Voltage >= 0.01 && Voltage < 3.0){
 digitalWrite(green, HIGH);
 digitalWrite(red, LOW);
 digitalWrite(yellow, LOW);
 }



delay(delayT);

 }

甚至更好,这样您就不会如此频繁地编写 digitalWrite,添加一个简单的条件来检查您是否真的需要进行更改: (注意没有 运行 这段代码,但应该可以..)

enum Colors {OFF, RED, YELLOW, GREEN};

int currentColor = Colors::OFF;  

void loop() {
 readVal = analogRead(readPin);
 Voltage = (readVal/1023.) * 5.;
 
 int expectedColor = Colors::OFF;

// go through our voltage logic to figure out the expected color
if(Voltage >= 4.0 && Voltage <= 5.0){
 expectedColor  = Colors::RED;
 }
else if(Voltage >= 3.0 && Voltage < 4.0){
 expectedColor  = Colors::YELLOW;
 }
else if (Voltage >= 0.01 && Voltage < 3.0){
 expectedColor  = Colors::GREEN;
 }

// we only want to call digitalWrite if the currentColor is not the
// color we expect, and we want to write the color we do expect
// (which is expectedColor.)

if (expectedColor != currentColor) {

 currentColor = expectedColor;

 digitalWrite(red, currentColor == Colors::RED ? HIGH : LOW);
 digitalWrite(green, currentColor == Colors::GREEN? HIGH : LOW);
 digitalWrite(yellow, currentColor == Colors::YELLOW ? HIGH : LOW);

}

delay(delayT);

 }

解释:

enum Colors {OFF, RED, YELLOW, GREEN};

我们可以使用枚举作为 LED 的一组可能状态 枚举有一个名称(例如颜色),默认情况下, 状态是整数类型,所以 OFF=0,RED=1,YELLOW=2,GREEN=3, 依此类推,您可以根据需要不断添加新颜色。 您可以使用 Colors::RED 或 Colors::YELLOW 等访问枚举。

int currentColor = Colors::OFF;

我们从关闭状态开始。请注意,arduino 并不是真的 知道 OFF 是什么,这是我们定义的不是 RED 的东西, 黄色或绿色。

int expectedColor = Colors::OFF;

初始化第二个变量来比较当前循环周期的 稍后使用 currentColor 的预期颜色

digitalWrite(red, currentColor == Colors::RED ? HIGH : LOW);

这里唯一棘手的部分是三元运算符 三元运算符是 if 语句的快捷方式 三元运算符是这样的: 健康)状况 ?如果为真值:如果为假值 对于红色,我们问“是 currentColor == RED 吗?”如果属实, 然后我们将它设置为高。如果为假,我们将其设置为假。 如果电压应使 currentColor 变为红色,则对于 绿色运算符我们问这个问题“是 currentColor == GREEN 吗?”, 答案是否定的,currentColor 是红色的,所以我们写 LOW 给 green pin