First of all I want to be clear, I'm not very skilled in programming methods, all the C I learned was for various Arduino projects.
That being said, I just finished a project that reads some data from ELM327 via a HC-05 Bluetooth module and prints it to a 3.5 inch screen. All is working as intended, but apparently I have capped my Arduino UNO's memory because the sketch utilizes 99% of it. At the moment this is not very troublesome, but if sometime in the future I want to add more data to be read, I won't be able to do it.
With this in mind, I was looking for some advice on how I could rewrite my code in order to occupy less memory. I tried some stuff, but had little effect. As I said, I'm not familiar with advanced programming techniques.
LE: So I used all of your advice, I got rid of all the fonts, modified the MCUFRIEND library to only work with my display and got rid of some repeating code, and now the sketch got 35% smaller. Thanks to all.
Sketch uses 21216 bytes (65%) of program storage space. Maximum is 32256 bytes. Global variables use 642 bytes (31%) of dynamic memory, leaving 1406 bytes for local variables. Maximum is 2048 bytes.
Here's the (old) code:
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeMonoBold12pt7b.h>
#include <Fonts/FreeMonoBold18pt7b.h>
#include <Fonts/FreeMonoBold24pt7b.h>
SoftwareSerial BTSerial(10, 11);
MCUFRIEND_kbv tft;
#define BLACK 0x0000
#define WHITE 0xFFFF
#define GREY 0x8410
byte inData;
char inChar;
String constructor="";
String deconstructor="";
int A;
int B;
bool busy = false;
void setup()
{
Retry:
Serial.begin(9600);
BTSerial.begin(38400);
delay(1000);
BTSerial.println("0100");
while(BTSerial.available()>0)
{
inData=0;
inChar=0;
inData = BTSerial.read();
inChar=char(inData);
constructor = constructor + inChar;
delay(20);
}
if (constructor.length()>0)
{
Serial.println("Conectat");
}
else {
goto Retry;
Serial.println("retry");
}
delay(5000);
ReadData();
basicConditions();
tft.reset();
uint16_t identifier = tft.readID();
if (identifier == 0xEFEF) identifier = 0x9486;
tft.begin(identifier);
tft.fillScreen(BLACK);
tft.setRotation(1);
tft.setTextColor(WHITE);
tft.fillRect(10, 40, 70, 40, GREY);
tft.fillRect(10, 140, 70, 40, GREY);
tft.fillRect(10, 240, 70, 40, GREY);
tft.fillRect(140, 70, 200, 100, GREY);
tft.fillRect(150, 220, 180, 60, GREY);
tft.fillRect(370, 40, 70, 40, GREY);
tft.fillRect(370, 140, 70, 40, GREY);
tft.fillRect(370, 240, 70, 40, GREY);
tft.setCursor(170,10);
tft.setFont(&FreeMonoBold12pt7b);
tft.print("Dacia 4X4");
tft.setCursor(30,35);
tft.setFont(&FreeMonoBold9pt7b);
tft.print("Cuplu");
tft.setCursor(80,70);
tft.setFont(&FreeMonoBold18pt7b);
tft.print("%");
tft.setCursor(15,120);
tft.setFont(&FreeMonoBold9pt7b);
tft.print("Presiune");
tft.setCursor(15,135);
tft.print("motorina");
tft.setCursor(82,170);
tft.setFont(&FreeMonoBold12pt7b);
tft.print("bar");
tft.setCursor(15,220);
tft.setFont(&FreeMonoBold9pt7b);
tft.print("Presiune");
tft.setCursor(20,235);
tft.print("admisie");
tft.setCursor(82,270);
tft.setFont(&FreeMonoBold12pt7b);
tft.print("bar");
tft.setCursor(395,20);
tft.setFont(&FreeMonoBold9pt7b);
tft.print("Temp.");
tft.setCursor(382,35);
tft.print("Antigel");
tft.setCursor(450,70);
tft.setFont(&FreeMonoBold12pt7b);
tft.print("C");
tft.setCursor(395,120);
tft.setFont(&FreeMonoBold9pt7b);
tft.print("Temp.");
tft.setCursor(382,135);
tft.print("Admisie");
tft.setCursor(450,170);
tft.setFont(&FreeMonoBold12pt7b);
tft.print("C");
tft.setFont(&FreeMonoBold9pt7b);
tft.setCursor(370,235);
tft.print("Debit Aer");
tft.setCursor(445,270);
tft.setFont(&FreeMonoBold9pt7b);
tft.print("g/s");
tft.setFont(&FreeMonoBold12pt7b);
tft.setCursor(150, 62);
tft.print("Turatie (RPM)");
tft.setFont(&FreeMonoBold12pt7b);
tft.setCursor(152, 212);
tft.print("Viteza (KM/H)");
}
void loop()
{
cuplu();
PresMot();
PresAdm();
TempAntig();
TempAdm();
MAF();
RPM();
viteza();
}
void basicConditions(){
if(constructor.indexOf(">")>0) {
busy=false;
constructor="";
}
}
void ReadData()
{
if (BTSerial.available()>0)
busy = true;
while(busy = true && constructor.indexOf(">")<0)
{
if (BTSerial.available()>0){
inData=0;
inChar=0;
inData = BTSerial.read();
inChar=char(inData);
constructor = constructor + inChar;
//Serial.println(constructor);
//Serial.println(busy);
}
}
//Serial.println("done");
if (constructor.indexOf(">")>0){
deconstructor=constructor.substring(11,13);
A = strtol(deconstructor.c_str(),NULL,16);
if (constructor.length()>15) {
deconstructor = constructor.substring(14,16);
B = strtol(deconstructor.c_str(),NULL,16);
}
else B = 0;
}
}
void cuplu() {
if (busy == false){
BTSerial.println("01041");
ReadData();
tft.setTextSize(1);
tft.setFont(&FreeMonoBold18pt7b);
tft.setCursor(10, 70);
tft.fillRect(10, 40, 70, 40, GREY);
tft.print(A/2.55, 0);
}
basicConditions();
}
void PresMot() {
if (busy == false){
BTSerial.println("010A1");
ReadData();
tft.setTextSize(1);
tft.setFont(&FreeMonoBold18pt7b);
tft.setCursor(10, 170);
tft.fillRect(10, 140, 70, 40, GREY);
tft.print((3*A)/100.0, 1);
}
basicConditions();
}
void PresAdm() {
if (busy == false){
BTSerial.println("010B1");
ReadData();
tft.setTextSize(1);
tft.setFont(&FreeMonoBold18pt7b);
tft.setCursor(10, 270);
tft.fillRect(10, 240, 70, 40, GREY);
tft.print((A/100.0), 1);
}
basicConditions();
}
void TempAntig() {
if (busy == false){
BTSerial.println("01051");
ReadData();
tft.setTextSize(1);
tft.setFont(&FreeMonoBold18pt7b);
tft.fillRect(370, 40, 70, 40, GREY);
tft.setCursor(370, 70);
tft.print(A-40);
}
basicConditions();
}
void TempAdm() {
if (busy == false){
BTSerial.println("010F1");
ReadData();
tft.setTextSize(1);
tft.setFont(&FreeMonoBold18pt7b);
tft.fillRect(370, 140, 70, 40, GREY);
tft.setCursor(370, 170);
tft.print(A-40);
}
basicConditions();
}
void MAF() {
if (busy == false){
BTSerial.println("01101");
ReadData();
tft.setTextSize(1);
tft.setFont(&FreeMonoBold18pt7b);
tft.fillRect(370, 240, 70, 40, GREY);
tft.setCursor(370, 270);
tft.print(((256*A)+B)/4.0, 0);
}
basicConditions();
}
void RPM() {
if (busy == false){
BTSerial.println("010C1");
ReadData();
tft.setFont(&FreeMonoBold18pt7b);
tft.setTextSize(2);
tft.fillRect(140, 70, 200, 100, GREY);
tft.setCursor(150, 140);
tft.print(A/4.0, 0);
}
basicConditions();
}
void viteza() {
if (busy == false){
BTSerial.println("010D1");
ReadData();
tft.setFont(&FreeMonoBold18pt7b);
tft.setTextSize(2);
tft.fillRect(150, 220, 180, 60, GREY);
tft.setCursor(180, 270);
tft.print(A);
}
basicConditions();
}