How to write clean efficient code???

Hey guys, rookie here,
I’m developing this serial command line thingy for my arduino. Like, if I type ON then the LED on pin 13 will turn on. Pretty simple stuff.
Currently I’m using an M0 but I want my program to also fit in the limited 32k flash of the uno.
My program right now is about 12k. I’m worried because I will be adding a lot more commands to my program.
So, how can I write clean efficient code so that I won’t run out of ram or flash memory?
I’ll give you the code and you can take a look at it. Try uploading it on your boards to see if everything’s good.

/*

My Code

*/

#include <Arduino.h>

int led = 13;
String WORDS = "";
int inByte;
String COMMAND = "";
bool canBanner = true;
int pinToWrite;
int i;
//const char compileDate[] = __DATE__ " " __TIME__;

String apps[] = {
  "CALC",
  "SERVO"
  "DWRITE"
  "TIME"
  "BANNER"
};

void banner() {
  Serial.println(" _____     _ _      _____");
  Serial.println("|  |  |___| | |_   |     |___ ___ ___ ___ ___");
  Serial.println("|  |  | . | |  _|  |   --| .'|   |   | . |   |");
  Serial.println(" \\___/|___|_|_|    |_____|__,|_|_|_|_|___|_|_|");
  Serial.println("Volt firmware V.1.0 / Copyright <My Name> 2017");
}

void setup() {
// Turn the Serial Protocol ON
  Serial.begin(1200);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  while (!Serial.available()) {}
    Serial.println();
    banner();
    Serial.flush();
}

void cursorPrompt() {
  if (COMMAND == "") {
    Serial.print("> ");
  }
  else {
  }
}

void getCommandPrintSerial() {
  while (!Serial.available()) {}
  inByte = Serial.read();
  WORDS = (char)inByte;
  Serial.print(WORDS);
  COMMAND = COMMAND + WORDS;
}

void programs() {
  if (COMMAND != "\r") {
    Serial.println("");
  }
  
  if (COMMAND == "ON\r") {
    Serial.println("LED on!");
    digitalWrite(led, HIGH);
  }

  else if (COMMAND == "OFF\r") {
    Serial.println("LED off!");
    digitalWrite(led, LOW);
  }

  else if (COMMAND == "PING\r") {
    Serial.println("PONG!");
  }

  else if (COMMAND == "BANNER\r") {
    canBanner = true;
    banner();
  }

  else if (COMMAND == "FLUSH\r") {
    Serial.println("Serial data cleared!");
    Serial.flush();
  }


  else if (COMMAND == "CALC\r") {
  COMMAND = "";
  Serial.println("---VOLT CALC 1000---");
  Serial.println("1) Addition");
  Serial.println("2) Subtraction");
  Serial.println("3) Multiplication");
  Serial.println("4) Division");
  cursorPrompt();
  while (Serial.available() == 0) {}
    if (inByte == 13) {
      Serial.println("");
      if (COMMAND == "1\r") {
        Serial.println("YAY!");
      }
      else if (COMMAND == "2\r") {
        Serial.println("HEY!");
      }
      else if (COMMAND == "3\r") {
        Serial.println("WOW!");
      }
      else if (COMMAND == "4\r") {
        Serial.println("FART!");
      }
      else {
        Serial.println("ERROR!");
      }
  }
}

  else if (COMMAND == "APPS") {
    for (i=0; i < sizeof(apps - 1); i++) {
      Serial.println(apps[i]);
    }
  }

  else if (COMMAND == "DWRITE " + pinToWrite) {
    digitalWrite(pinToWrite, HIGH);
  }

  else if (COMMAND == "\r") {
    Serial.println("");
  }

/*
  else if (COMMAND == "TIME\r") {
    Serial.println(compileDate);
  }
*/

  else {
    Serial.println("What?");
  }
  COMMAND = "";
}

void loop() {
  cursorPrompt();
  getCommandPrintSerial();
  if (inByte == 13) {
    programs();
  }
}

Any suggestions on reducing vars or writing more concise code?

THANKS!

BTW there are a lot of incomplete stuff in my code such as the APPS command. I also need to work on the DWRITE function and the CALC function. Any help will be greatly appreciated.

  • Don't use String, use char arrays.
  • Use the F() macro for constant string in print statements.
  • Use the defined type uint8_t, uint16_t, uint32_t to control the size of variables.
  • Don't use an int when a uint8_t is sufficient.
  • Make your commands single characters - speeds the code, uses less space and computers don't care about words anyway.

Generally, read other people's code and learn from what they do.

compile it for Uno if you want to know the size for the Uno. set board Uno and use the Verify button.

Sketch uses 4850 bytes (15%) of program storage space. Maximum is 32256 bytes.
Global variables use 686 bytes (33%) of dynamic memory, leaving 1362 bytes for local variables. Maximum is 2048 bytes.

Instead of doing a series of "if (COMMAND == "text")" statements, implement a table of commands and use a case statement off of the index (or via a separate key.) This works whether you use Strings or char arrays...