Simple library question

Hi Folks, I am working on a new application and I'm writing everything from scratch. From my understand library files are used to help organization and simplify high level code?

I had a couple of basic library questions I hope someone can answer for me:

Does using libraries save on memory in any way?
Does using library files help code run faster?

To elaborate a little on these questions, my code presently uses no library files, this means I declare all variables at the start even ones that are only used inside functions. If these functions were library files instead would the microprocessor only need to 'remember' the variables it was currently using instead of all of them if there are no library files? Does this save any memory?

My code needs to run fast, it does some floating point math and if using library files speeds things up I am willing to switch to using them.

Any help on these two questions is appreciated. Thanks.

Both your questions has the answer 'no' in fact, creating classes of your functions will make the program use more memory, and execute a tiny bit slower.
But, and this one is a big but(t);
You will most likely be able to make changes and keep the code maintainable much easier if you at least split the code into different files, and even more so if you author libraries that help you organize your code. Additionally, and this is one of my most valued arguments for OOP, is that using libraries makes your code infinitely much more easy to read. This is my opinion, but its a strong one as such.

I say; make classes if you can afford some efficiency and memory loss (this is most likely irrelevant, especially if you already do float math).

Thanks, that helps a lot.

It probably would make my code a little simpler to use libraries but at this point I want to get everything working as is, once it is working I might go back and split it into libraries if I think I can afford some memory and speed overhead.

:slight_smile:

You can create classes directly in your sketch without it being a library. You can then move your classes out to libraries as you find you need them in other programs or just want to clean it up a bit.

I had written a program a while back that consumes all the memory on the arduino as an example for someone. So don't program with the huge variable counts I am using, but it may serve as an example of how you can define and use multiple classes in the same sketch.

Note: This code creates an instance of a class and runs code in it. When the function ends, the class is cleared from memory. If a variable is defined globally, it never leaves memory, even if not used.

#define APP_NONE 0
#define APP_HOG1 1
#define APP_HOG2 2
#define APP_BREAK_ME 3
#define HOG_FOOD 400

// 3 left out of app count to not break
#define APP_COUNT 2 
#define HOG_DELAY 20

// set to APP_BREAK_ME to see too many hogs created (2 is too many)
// set to APP_HOG1 to see the system create two hogs .. one at a time
//    this method does not blow the RAMs top
int currentApp = APP_HOG1; //APP_BREAK_ME  or APP_HOG1

class Hog1 {
  int _food[HOG_FOOD];
  int _timesFed;
  boolean _isFed;
  int hog1_001;
  int hog1_002;
  int hog1_003;
  int hog1_004;
  
public:
  Hog1(int timesFed) {
    _timesFed = timesFed;
    _isFed = (timesFed > 0);
  };
  Hog1() {
    _timesFed = 0;
    _isFed = false;
  };
  ~Hog1() {
    free(_food);
  };
  boolean loop(){
    _timesFed++;
    if ( _timesFed >= HOG_FOOD ){
      Serial.println("I am a full Hog1");
      return false;
    }
    _food[_timesFed] = 1; // set any value
    Serial.print("Hog1 - fed: ");
    Serial.println(_timesFed, DEC);
    delay(HOG_DELAY);
    return true;
  };
};

class Hog2 {
  int _food[HOG_FOOD];
  int _timesFed;
  boolean _isFed;
  int hog2_001;
  int hog2_002;
  int hog2_003;
  int hog2_004;
  
public:
  Hog2(int timesFed) {
    _timesFed = timesFed;
    _isFed = (timesFed > 0);
  };
  Hog2() {
    _timesFed = 0;
    _isFed = false;
  };
  ~Hog2() {
    free(_food);
  };
  boolean loop(){
    _timesFed++;
    if ( _timesFed >= HOG_FOOD ){
      Serial.println("I am a full Hog2");
      return false;
    }
    _food[_timesFed] = 1000;
    Serial.print("Hog2 - fed: ");
    Serial.println(_timesFed, DEC);
    delay(HOG_DELAY);
    return true;
  };
};

void setup(){
  Serial.begin(9600);
}

void runHog1(){
  Serial.println("Starting runHOG1");
  boolean isRunning = true;
  Hog1 hog = Hog1();
  while (isRunning){
    isRunning = hog.loop();
  }
  Serial.println("Ended runHOG1");
}

void runHog2(){
  Serial.println("Starting runHog2");
  boolean isRunning = true;
  Hog2 hog = Hog2();
  while (isRunning){
    isRunning = hog.loop();
  }
  Serial.println("Ended runHog2");
}

void advanceToNextApp(){
  currentApp++;
  if( currentApp > APP_COUNT )
    currentApp = 1; //or zero or set using external trigger (i.e. serial / button)
 
}

void runTooManyHogs(){
  Serial.println("Starting runTooManyHogs - Feeding Hog1");
  boolean isRunning = true;
  Hog1 hog = Hog1();
  while (isRunning){
    isRunning = hog.loop();
  }
  Serial.println("Done Feeding Hog1, lets feed hog2.");

  isRunning = true;
  Hog2 hog2 = Hog2();
  while (isRunning){
    isRunning = hog2.loop();
  }
  Serial.println("**** Never gets here .. kaboooom");
}

void loop(){
  
  switch (currentApp){
    case APP_NONE : Serial.println("No App");
    break;
    case APP_HOG1 : runHog1();
    break;
    case APP_HOG2 : runHog2();
    break;
    case APP_BREAK_ME : runTooManyHogs();
    break;
  }    
  advanceToNextApp(); 
}