Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Teensy LC code size

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    4

    Teensy LC code size

    Hi,

    I am quite new to Teensy and this is my first project with one.
    I have started writing a simple light controller but am dismayed to find that after writing less than half of my code, I am completely and utterly out of space.

    I can't see why my code would be using so much space. I am not using serial, nor am I using any strings.

    I am using 3 libraries but they seem like they should be small and simple.

    Can anyone lend me any ideas of where I should be looking to trim space? What tools do I have to see where all my space is going?

    Here is the build error:

    Code:
    c:/program files (x86)/arduino/hardware/tools/arm/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/bin/ld.exe: region `FLASH' overflowed by 114472 bytes
    Here is my complete code:

    Code:
    #include <RGBLed.h>
    #include <EasyButton.h>
    #include <StateMachine.h>
    
    ////////////////////////////
    // Adjust parameters here:
    ////////////////////////////
    
    const unsigned long startMins = 30;
    const unsigned long endMins = 180;
    const unsigned long fadeupTimeSecs = 10;
    const unsigned long fadedownTimeMins = 30;
    
    ////////////////////////////
    
    const unsigned long rangeMins = endMins - startMins;
    const unsigned long fadeupTimeMillis = fadeupTimeSecs * 1000;
    
    #define RED_PIN     16
    #define GREEN_PIN   17
    #define BLUE_PIN    20
    #define LED_PIN     13
    #define SSR_PIN     23
    #define SWTest_PIN  14
    #define SWNext_PIN  15
    #define Day_PIN     21
    
    RGBLed LED(RED_PIN, GREEN_PIN, BLUE_PIN, RGBLed::COMMON_CATHODE);
    EasyButton nextButton(SWNext_PIN, 40, false, true);
    EasyButton testButton(SWTest_PIN, 40, false, true);
    StateMachine machine = StateMachine();
    
    State* S0_daytime =   machine.addState(&daytime_0);
    State* S1_fadeUp =    machine.addState(&fadeUp_1);
    State* S2_lightsOn =  machine.addState(&lightsOn_2);
    State* S3_fadeDown =  machine.addState(&fadeDown_3);
    State* S4_nighttime = machine.addState(&nighttime_4);
    
    unsigned long fadeupTimeStamp = 0;
    
    
    ////// Functions to process each of the states //////
    
    void daytime_0(){
      if(machine.executeOnce){
        LED.setColor(RGBLed::RED);
      }
      if (readDaylight() == LOW) {
        machine.transitionTo(S1_fadeUp);
      }
    }
    
    void fadeUp_1(){
      if(machine.executeOnce){
        fadeupTimeStamp = millis();
        digitalWrite(SSR_PIN, LOW); // Turn on SSR
        LED.setColor(RGBLed::YELLOW);
      }
      unsigned long dur = millis() - fadeupTimeStamp;
      float fadeVal = float(dur)/float(fadeupTimeMillis);
      if (fadeVal >= 1.0) {
        machine.transitionTo(S2_lightsOn);
      } else {
        int lampVal =  int(pow(fadeVal, 2.4) * 1024);
        analogWrite(A12, lampVal);
      }
    }
    
    void lightsOn_2(){
      if(machine.executeOnce){
        LED.setColor(RGBLed::GREEN);
      }
    }
    
    void fadeDown_3(){
      if(machine.executeOnce){
        LED.setColor(RGBLed::CYAN);
      }
    }
    
    void nighttime_4(){
      if(machine.executeOnce){
        LED.setColor(RGBLed::BLUE);
      }
    }
    
    /////// End of state functions  /////////
    
    void nextState() {
      machine.transitionTo((machine.currentState + 1) % 5);
    }
    
    int readPot() {
      int potVal = analogRead(A8);
      float amt = 1.0 - (float(potVal) / 1023.0);
      int mins = int((rangeMins * amt)) + startMins;
      return mins;
    }
    
    bool readDaylight(){
      return !(digitalRead(Day_PIN) == LOW);
    }
    
     
    void setup() {
      
      pinMode(SSR_PIN, OUTPUT);
      pinMode(LED_PIN, OUTPUT);
      pinMode(Day_PIN, INPUT);
        
      digitalWrite(LED_PIN, HIGH);
      digitalWrite(SSR_PIN, HIGH);
    
      LED.setColor(RGBLed::RED);
      nextButton.begin();
      testButton.begin();
    
      nextButton.onPressed(nextState);
    }
    
    void loop() {
      machine.run();
      nextButton.read();
    }

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,086
    This is not over by some trivial fixable amount : "`FLASH' overflowed by 114472 bytes"

    Perhaps try taking out each of the libraries one by one and see if one is responsible for all the overflow, leaving enough room to proceed with an alternate version of that library?

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    4
    Right.
    That's why I was so confused, and figured something must be horribly wrong.
    Thanks for the suggestion. I will give it a try.

    [edit]
    Turns out to be the statemachine.h
    after removing that, I am using 6% of flash.
    I wonder why it is so gigantic

  4. #4
    Junior Member
    Join Date
    Sep 2021
    Posts
    4
    problem solved.

    The statemachine.h library uses another library; linkedlist.h
    Inside that library folder is a tests.cpp file with a bunch of tests. I don't see how it is getting compiled in, but it is. Renaming it to tests.cpp.bak makes everything work fine. Now my entire project uses 9% of space.

    Thanks for the tip!

  5. #5
    Quote Originally Posted by Dubbie View Post
    StateMachine uses library LinkedList. Inside that library folder is a tests.cpp file with a bunch of tests. I don't see how it is getting compiled in, but it is. Renaming it to tests.cpp.bak makes everything work fine. Now my entire project uses 9% of space.
    If the library is a single folder, all C/CPP/H files in the folder will be included when the library is compiled. You can separate the library source code from the tests and examples by creating a sub-folder named "src" for the library source files, and a sub-folder named "examples" or "tests" for the other stuff. Arduino has a library specification that defines all of this.

  6. #6
    Junior Member
    Join Date
    Sep 2021
    Posts
    4
    Today I learned something.

    Thanks!

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,086
    Great news - and Welcome - and you are welcome.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •