【问题标题】:If statement not working LCD and Joystick如果语句不起作用 LCD 和操纵杆
【发布时间】:2021-03-19 06:45:36
【问题描述】:

现在,我正在尝试用控制火柴人的操纵杆在液晶显示器上制作火柴人。如果操纵杆是 HIGH,那么火柴人应该变成说“你好!”的文字。 当我尝试这样做时,它只显示火柴人而不是其他任何东西。我尝试移动操纵杆,但仍然没有。怎么回事?

代码:

#include <LiquidCrystal.h>

int state = digitalRead(8);

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

byte customChar[] = {
  B01110,
  B01110,
  B01110,
  B00100,
  B01110,
  B10101,
  B00100,
  B01010
};


void setup() {
  
   lcd.begin(16, 2);
  lcd.createChar(0, customChar);
  lcd.home();
  lcd.write(static_cast<uint8_t>(0));
}

void loop() {
  for(int position = 0; position < 13; position++) {
  lcd.scrollDisplayRight();
  delay(150);

  
  
    if (state == HIGH) {

    lcd.clear();
    lcd.print("Hello!");
    }
  }
}

接线经过仔细检查,我确信它是正确的。

【问题讨论】:

  • 你只在你的程序中初始化或赋值给state一次

标签: c++ arduino arduino-uno arduino-ide


【解决方案1】:

您需要在循环中读取状态变量的值。 例如,您可以输入:
void loop() {
   for(int position = 0; position < 13; position++) {
      lcd.scrollDisplayRight();
      delay(150);
   }

   state = digitalRead(8);

   if (state == HIGH) {
     lcd.clear();
     lcd.print("Hello!");
   }
}

这应该可行。试试看!

【讨论】:

  • 还是不行……或许还有别的解决办法?
  • @HamTheCoder try state == True in the if
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
相关资源
最近更新 更多