液晶屏清晰但不会回来

时间:2019-01-21 02:57:32

标签: arduino

#include <IRremote.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int TempPin = A0;
float realTemp = 0;
int FakeTemp = 0;
int target = 20;
int IRPin = 11;
IRrecv irrecv(IRPin);
decode_results results;

void setup() {
    // put your setup code here, to run once:
    lcd.begin(16, 2);
    Serial.begin(9600);
    while (! Serial);
    irrecv.enableIRIn(); //enables ir receiver
}

void loop() {
    FakeTemp = analogRead(TempPin);
    realTemp = (5.0 * FakeTemp * 1000.0) / (1024 * 10); // math for the temperature
    Serial.println(realTemp);
    lcd.print("Temperature");
    lcd.setCursor(0, 13);
    lcd.print(realTemp); //prints the temperature
    delay(3000);
    lcd.clear();

    if (irrecv.decode(16754775)) { //input from ir remote
        lcd.clear();
        lcd.print("Target=");
        lcd.print(target + 1);
        delay(3000);
        irrecv.resume();
        lcd.clear();
        target = target + 1;
        // adds to target
    }

    if (irrecv.decode(16769055)) { //this is were everything goes wrong
        lcd.clear();
        lcd.print("Target=");
        lcd.print(target - 1);
        delay(3000);
        irrecv.resume();
        lcd.clear();
        target = target - 1;
    }
}

我已经在这里呆了很长时间了,当我添加一个if语句时,我不知道出什么问题了,它将在LCD上显示一次温度。但是后来我再也不会这样做了,我可以在串行监视器中看到它仍然在抓取温度,但不会在LCD上打印。

1 个答案:

答案 0 :(得分:-1)

#include <IRremote.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int TempPin = A0;
float realTemp = 0;
int FakeTemp = 0;
int target = 20;
int IRPin = 11;
IRrecv irrecv(IRPin);
decode_results results;

void setup() {
    // put your setup code here, to run once:
    lcd.begin(16, 2);
    Serial.begin(9600);
    while (! Serial);
    irrecv.enableIRIn(); //enables ir receiver
}

void loop() {
    FakeTemp = analogRead(TempPin);
    realTemp = (5.0 * FakeTemp * 1000.0) / (1024 * 10); // math for the temperature
    Serial.println(realTemp);
    lcd.print("Temperature");
    lcd.setCursor(0, 13);
    lcd.print(realTemp); //prints the temperature
    delay(3000);
    //lcd.clear();

    if (irrecv.decode(16754775)) { //input from ir remote
        lcd.clear();
        lcd.print("Target=");
        lcd.print(target + 1);
        delay(3000);
        irrecv.resume();
        //lcd.clear();
        target = target + 1;
        // adds to target
    }

    if (irrecv.decode(16769055)) { //this is were everything goes wrong
        lcd.clear();
        lcd.print("Target=");
        lcd.print(target - 1);
        delay(3000);
        irrecv.resume();
        //lcd.clear();
        target = target - 1;
    }
}

您可以尝试使用此代码吗?我没有用过ir模块。但是我认为只有当新的ir消息进入arduino代码时,if语句中的代码才能运行。因此,我认为if语句和主循环中的第二个lcd.clear命令是问题的主要原因。