Arduino的code异常 - LCD失败多“如果”语句 [英] Arduino code anomalies - LCD fails with multiple 'if' statements

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

问题描述

我有一些code和怪异的异常问题。这code被放置在一个 Digispark 。该Digispark有6,010字节的code尺寸的限制。当使用嵌套如果报表,文本不会输出到液晶屏(见下面的链接)。通过注释掉每一套单独我能得到它的工作了。

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.

基本LCD功能:


  1. LCD输出内部温度的啤酒和环境空气温度。 http://imgur.com/S0rYvaa

  2. LCD清零

  3. LCD输出的目标温度和加热(继电器)状态 http://imgur.com/OtFXG1K

  1. LCD outputs internal beer temp and ambient air temp. http://imgur.com/S0rYvaa
  2. LCD clears
  3. LCD outputs target temp and heater (relay) status http://imgur.com/OtFXG1K

变量是float类型的。

Variables are of float type.

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;

使用这个code,当它注释掉这样的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");
  }

液晶还与该code。编译大小为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不取消注释时工作。编译大小为5992字节。它所做的就是与背光和没有文字坐在那里。 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");
}

我不明白为什么会这样。这是发生的历史,因为我得到太接近最大大小限制?我不能构建code这样?

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?

推荐答案

我认为这是pretty很难在一个确凿的方式来回答这个问题,而不能在本地进行测试。

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

这听起来很可疑,虽然,它可在code大小接近的最大突破。另一方面,这似乎表明,它不会破坏硬的工具的一个错误,如果有的限制超出

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.

这是如何减少code尺寸的一些提示:

Some tips on how to reduce the code size:


  • 请不要使用浮动,因为CPU必须效仿。定点格式应该是罚款的温度。

  • 分解出函数调用 digitalWrite() lcd.print(),因为函数调用生成相当很多code的。

  • 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和继电器的事实,所以我们可以随时调用的函数。然后我们使用变量来保存所需的值,因为作业通常较便宜(在code尺寸而言,这里)比函数调用。

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的code异常 - LCD失败多“如果”语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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