简体   繁体   中英

Arduino code anomalies - LCD fails with multiple 'if' statements

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.

Basic LCD functions:

  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

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;

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");
  }

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 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:

  • 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);

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.

Don't forget the small size of RAM, as the Attiny85 only has 512 bytes of SRAM, compared to the 328's 2K. You may just be running out of RAM. I learned that when it runs out, it just kind of sits there.

I suggest reading the readme from this library to get the FreeRAM. It mentions how the ".print" can consume both RAM and ROM.

I always now use

Serial.print(F("HELLO")); 

versus

Serial.print("HELLO"); 

as it saves RAM, and this should be true for lcd.print. Where I always put a

Serial.println(freeMemory(), DEC);  // print how much RAM is available.

in the beginning of the code, and pay attention. Noting that there needs to be room to run the actual code and recurse into it.

unwind's example of factoring the .print out to only one instance, is using a variable. Which actually does similar as the F() (older getPSTR()) in that it is no longer a const string. So it actually use less RAM too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM