Variable name length affects compiled program size?

When I change the length of a variable name, the compiled sketch size changes. I thought the compiler was supposed to strip all the human-readable names and substitute them with something more efficient.

I'm using Arduino IDE 2.0.

Changing bool isLongDetected = false; to bool isLongButtonPressDetected = false; increased program storage space usage by 14 bytes.

That depends on the MCU and debug-level used.

It should make no difference. Post some compilable code that demonstrates the problem.

If you use find/replace to switch between isLongDetected and isLongButtonPressDetected in the following code, the variation in compiled program size occurs when compiling for Arduino Nano or LGT8F328P.

// Pin numbers and long/short press duration settings
const int BUTTON_PIN_SET = 3;
const int BUTTON_PIN_PLUS = 8;
const int BUTTON_PIN_MINUS = 12; // the number of the pushbutton pin
const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
const int LONG_PRESS_TIME  = 1000; // 1000 milliseconds

// Variables for long/short press detection
int lastStateSet = LOW;  // the previous state from the input pin
int currentStateSet;     // the current reading from the input pin
unsigned long pressedTimeSet = 0;
unsigned long releasedTimeSet = 0;
bool isPressingSet = false;
bool isShortDetectedSet = false;
bool isLongButtonPressDetectedSet = false;
int lastStateMinus = LOW;  // the previous state from the input pin
int currentStateMinus;     // the current reading from the input pin
unsigned long pressedTimeMinus = 0;
unsigned long releasedTimeMinus = 0;
bool isPressingMinus = false;
bool isShortDetectedMinus = false;
bool isLongButtonPressDetectedMinus = false;
int lastStatePlus = LOW;  // the previous state from the input pin
int currentStatePlus;     // the current reading from the input pin
unsigned long pressedTimePlus = 0;
unsigned long releasedTimePlus = 0;
bool isPressingPlus = false;
bool isShortDetectedPlus = false;
bool isLongButtonPressDetected = false;

// Variables for the counters
unsigned char counters[4]; // 0: front, 1: back, 2: arms, 3: legs
bool counterAdjustEnable = false;
unsigned char counterArrayIndexPosition;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN_SET, INPUT_PULLUP);
  pinMode(BUTTON_PIN_MINUS, INPUT_PULLUP);
  pinMode(BUTTON_PIN_PLUS, INPUT_PULLUP);
  pinMode(BUTTON_PIN_MINUS - 2, OUTPUT); // these four lines are just to give convenient ground pins
  digitalWrite(BUTTON_PIN_MINUS - 2, LOW); // these four lines are just to give convenient ground pins
  pinMode(BUTTON_PIN_PLUS - 2, OUTPUT); // these four lines are just to give convenient ground pins
  digitalWrite(BUTTON_PIN_PLUS - 2, LOW); // these four lines are just to give convenient ground pins
}

void loop() {
  buttonPressDetection(BUTTON_PIN_SET, lastStateSet, currentStateSet, pressedTimeSet, releasedTimeSet, isPressingSet, isShortDetectedSet, isLongButtonPressDetectedSet);
  buttonPressDetection(BUTTON_PIN_MINUS, lastStateMinus, currentStateMinus, pressedTimeMinus, releasedTimeMinus, isPressingMinus, isShortDetectedMinus, isLongButtonPressDetectedMinus);
  buttonPressDetection(BUTTON_PIN_PLUS, lastStatePlus, currentStatePlus, pressedTimePlus, releasedTimePlus, isPressingPlus, isShortDetectedPlus, isLongButtonPressDetected);
  counterTask();

  // This bit is for debugging - something is messed up and these serial prints show it - a year later, I can't see anything messed up - maybe this was fixed
  static unsigned long currentMillis;
  static unsigned long previousMillis = 0;

  currentMillis = millis();
  if (currentMillis - previousMillis > 1000) {
    Serial.print("debug:");
    Serial.print(counters[0]);
    Serial.print(",");
    Serial.print(counters[1]);
    Serial.print(",");
    Serial.print(counters[2]);
    Serial.print(",");
    Serial.print(counters[3]);
    Serial.print(",");
    Serial.print("counterArrayIndexPosition:");
    Serial.print(counterArrayIndexPosition);
    Serial.print(",");
    Serial.print("counterAdjustEnable:");
    Serial.print(counterAdjustEnable);
    Serial.print(",");
    Serial.print("isShortDetectedMinus:");
    Serial.print(isShortDetectedMinus);
    Serial.print(",");
    Serial.print("isLongButtonPressDetectedMinus:");
    Serial.println(isLongButtonPressDetectedMinus);
    Serial.print(",");
    Serial.print("isShortDetectedPlus:");
    Serial.print(isShortDetectedPlus);
    Serial.print(",");
    Serial.print("isLongButtonPressDetected:");
    Serial.println(isLongButtonPressDetected);

    previousMillis = currentMillis;
  }
}

void counterTask() {
  static unsigned char counterCheck = counters[counterArrayIndexPosition]; // this will check for changes
  static unsigned long previousMillis = 0;
  static unsigned long currentMillis;

  // if counters are allowed to be adjusted and Plus button is short-pressed, increment counter at selected array position
  if (isShortDetectedPlus == true && counterAdjustEnable == true) {
    counters[counterArrayIndexPosition]++;
    isShortDetectedPlus = false;
  }

  // if counters are allowed to be adjusted and Minus button is short-pressed, decrement counter at selected array position
  if (isShortDetectedMinus == true && counterAdjustEnable == true) {
    counters[counterArrayIndexPosition]--;
    isShortDetectedMinus = false;
  }

  // if Set button is short-pressed, change position of index in array of counters and disable counterAdjustEnable flag
  if (isShortDetectedSet == true && counterAdjustEnable == true) {
    counterAdjustEnable = false;
    isShortDetectedSet = false;
  }
  else if (isShortDetectedSet == true) {
    counterArrayIndexPosition = (counterArrayIndexPosition + 1) % 4; // Cycle through the array positions
    isShortDetectedSet = false;
  }

  // if counters are allowed to be adjusted and Plus button is long-pressed, increment counter at selected array position every 150 ms
  if (isLongButtonPressDetected == true && counterAdjustEnable == true) {
    currentMillis = millis();
    if (currentMillis - previousMillis < 150) return;
    counters[counterArrayIndexPosition]++;
  }

  // if counters are allowed to be adjusted and Minus button is long-pressed, decrement counter at selected array position every 150 ms
  if (isLongButtonPressDetectedMinus == true && counterAdjustEnable == true) {
    currentMillis = millis();
    if (currentMillis - previousMillis < 150) return;
    counters[counterArrayIndexPosition]--;
  }
  previousMillis = currentMillis;

  // if Set button is long-pressed, enable counter adjustment
  if (isLongButtonPressDetectedSet == true) {
    counterAdjustEnable = true;
  }

  // check whether counter has changed since last function call and print to serial if it has
  if (counterCheck != counters[counterArrayIndexPosition]) {
    Serial.println(counters[counterArrayIndexPosition]);
  }
  counterCheck = counters[counterArrayIndexPosition];
}

void buttonPressDetection(const int BUTTON_PIN, int &lastState, int &currentState, unsigned long &pressedTime, unsigned long &releasedTime, bool &isPressing, bool &isShortDetected, bool &isLongButtonPressDetected) {
  // read the state of the switch/button:
  currentState = digitalRead(BUTTON_PIN);

  if (lastState == HIGH && currentState == LOW) {       // button is pressed
    pressedTime = millis();
    isPressing = true;
    isLongButtonPressDetected = false;
  } else if (lastState == LOW && currentState == HIGH) { // button is released
    isPressing = false;
    releasedTime = millis();

    long pressDuration = releasedTime - pressedTime;

    if ( 10 < pressDuration && pressDuration < SHORT_PRESS_TIME ) {
      Serial.println("A short press is detected");
      isShortDetected = true;
    }
    isLongButtonPressDetected = false;
  }

  if (isPressing == true && isLongButtonPressDetected == false) {
    long pressDuration = millis() - pressedTime;

    if ( pressDuration > LONG_PRESS_TIME ) {
      Serial.println("A long press is detected");
      isLongButtonPressDetected = true;
    }
  }

  // save the last state
  lastState = currentState;
}

Which version of Arduino IDE? What processor board? Which version of the board support core?

This string: Serial.print("isLongButtonPressDetected:"); uses more space than this: Serial.print("isLongDetected:");.

1 Like

Thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.