Arduino 代码异常 - LCD 出现多个“if"语句失败 [英] Arduino code anomalies - LCD fails with multiple 'if' statements

查看:64
本文介绍了Arduino 代码异常 - LCD 出现多个“if"语句失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一些代码和奇怪的异常有疑问.此代码放置在 Digispark 上.Digispark 的代码大小限制为 6,010 字节.使用嵌套的 if 语句时,文本不会输出到 LCD 屏幕(请参阅下面的链接).通过分别注释掉每一组,我可以让它再次工作.

I have a question about some code and weird anomalies. This code is placed on a Digispark. The Digispark has a code size limit of 6,010 bytes. When using nested if statements, text is not outputted to the LCD screen (see links below). By commenting out each set seperately I am able to get it working again.

基本液晶功能:

  1. LCD 输出内部啤酒温度和环境空气温度.http://imgur.com/S0rYvaa
  2. LCD 清除
  3. LCD 输出目标温度和加热器(继电器)状态 http://imgur.com/OtFXG1K

变量是浮点型.

float inside_temp;
float outside_temp;
float target = 74.00;

//inside_temp and outside_temp are values from 2 ds18b20's
inside_temp = 70.70;
outside_temp = 70.81;

LCD 在使用此代码时工作,并像这样注释掉.编译后的大小为 5,928 字节.

The LCD works when using this code with it commented out like this. The compiled size is 5,928 bytes.

if(inside_temp < target){
    //Create a limit so heater isn't crazy hot as 5 gallons takes a while to change temperature.
    // float limit = target + 1;
    // if(outside_temp > limit){
    //     digitalWrite(RELAY_PIN, LOW);
    //     lcd.print("OFF");
    // }
    // else{
    digitalWrite(RELAY_PIN, HIGH);
    lcd.print("ON");
    // }
  }
  else{
      digitalWrite(RELAY_PIN, LOW);
      lcd.print("OFF");
  }

LCD 也可以使用此代码.编译后的大小为 5,590 字节.

The LCD also works with this code. The compiled size is 5,590 bytes.

// if(inside_temp < target){
    //Create a limit so the heater isn't crazy hot as 5 gallons takes a while to change temperature.
    float limit = target + 1;
    if(outside_temp > limit){
        digitalWrite(RELAY_PIN, LOW);
        lcd.print("OFF");
    }
    else{
       digitalWrite(RELAY_PIN, HIGH);
       lcd.print("ON");
    }
    // }
    // else{
    //     digitalWrite(RELAY_PIN, LOW);
    //     lcd.print("OFF");
    // }

LCD 在取消注释时不起作用.编译后的大小为 5,992 字节.它所做的只是坐在那里,打开背光,没有文字.http://imgur.com/xPAzY0N,DdGdYoI

LCD does NOT work when uncommented. The compiled size is 5,992 bytes. All it does is sit there with the backlit on and no text. http://imgur.com/xPAzY0N,DdGdYoI

if(inside_temp < target){
    //create a limit so heater isn't crazy hot as 5 gallons takes a while to change temperature.
    float limit = target + 1;
    if(outside_temp > limit){
        digitalWrite(RELAY_PIN, LOW);
        lcd.print("OFF");
    }
    else{
        digitalWrite(RELAY_PIN, HIGH);
        lcd.print("ON");
    }
}
else{
    digitalWrite(RELAY_PIN, LOW);
    lcd.print("OFF");
}

我不明白为什么会发生这种情况.这是因为我太接近最大尺寸限制了吗?我不能这样构造代码吗?

I don't understand why this happens. Is this occuring because I'm getting too close to maximum size limit? Can I not structure code like this?

推荐答案

我认为如果无法在本地进行测试,很难以结论性的方式回答这个问题.

I think it's pretty hard to answer this in a conclusive manner, without being able to test it locally.

虽然听起来很可疑,当代码大小接近最大值时它会中断.另一方面,如果超出某个限制,它似乎表明工具中存在一个错误,即它不会破坏硬".

It does sound very suspicious though, that it breaks when the code size approaches the maximum. On the other hand, it seems to indicate a bug in the tools that it doesn't break "hard", if some limit was exceeded.

关于如何减少代码大小的一些技巧:

Some tips on how to reduce the code size:

  • 不要使用 float,因为 CPU 必须模拟它.定点格式应该适用于温度.
  • 分解对 digitalWrite()lcd.print() 的函数调用,因为函数调用会生成大量代码.
  • Don't use float since the CPU must emulate it. A fixed-point format should be fine for temperatures.
  • Factor out the function calls to digitalWrite() and lcd.print(), since function calls generate quite a lot of code.

分解这些调用的一种方法是执行以下操作:

One way of factoring out those calls is doing something like this:

uint8_t relay_pin = LOW;
const char *lcd_text = "OFF";
if(inside_temp < target) {
    float limit = target + 1;
    if(outside_temp > limit) {
    }
    else {
      relay_pin = HIGH;
      lcd_text = "ON";
    }
}
digitalWrite(RELAY_PIN, relay_pin);
lcd.print(lcd_text);

这使用了我们希望始终更新 LCD 和继电器的事实,因此我们始终可以调用这些函数.然后我们使用变量来保存所需的值,因为赋值通常比函数调用更便宜(在代码大小方面,这里).

This uses the fact that we want to always update the LCD and relay, so we can always call the functions. Then we use variables to hold the desired values, since assignments are usually cheaper (in terms of code size, here) than function calls.

这篇关于Arduino 代码异常 - LCD 出现多个“if"语句失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆