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.