Help reducing SRAM usage

Alright I'm working on this code. It uses an OLED 128x32 display, 3 buttons, and a ESP8266. Some of the commented out code just needs deleted but Im not too worried about the 91% flash space it uses. I am working with an arduino nano so I am worried about the 1542 bytes of dynamic it uses. I have tried everything to reduce the code and I literally cant think of anything else to do except rebuild libraries and make all globals use EEPROM read and write but obviously, that will introduce some more delays to the program. What should I do?

I have a lot of arduino nanos but nothing bigger. (was given a big pile of parts from an old highschool electronics teacher). Must I just use multiple arduinos for the simplest fixed? Could I hard program the ESP8266 in someway? How would I do that?

I'm decently versed as a basic Electrical Engineer but ChemE is my native field so some extra explanation might be necissary

#include <Arduino.h>
#include <U8g2lib.h>
//#include <SPI.h>
//#include <Wire.h>
#include <EEPROM.h>
#include "WiFiEsp.h"
#ifndef HAVE_HWEspSerial
#include "SoftwareSerial.h"
SoftwareSerial EspSerial(2, 3); // RX, TX
#endif

    
constexpr int loggerVal = 1;
WiFiEspClient client;
//Constants
constexpr int BUTTON = A0; 
constexpr int UP = 4; 
constexpr int DOWN = 5;
constexpr int SELECT = 6;
//constexpr int START = 400;

//globals
byte mode = 0; // 1 > startScreen; 2 > startDab; 3 > Edit variables; 4 > Edit Heat; 5 > Edit Cool; 6 > Reset Memory
byte hover = 0;
//int hoverMax = 2;
//byte heat = 30;
//byte cool = 60;
byte changeValue = 0;
byte buttonVal = 0;

U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); //128 x 32

void zoomBar() {
  for(int i=0; i < 128; i++){
    u8g2.clearBuffer();
    u8g2.setDrawColor(1);
    u8g2.drawLine(15+i, 1, i, 64);
    u8g2.drawLine(16+i, 1, i+1, 64);
    u8g2.drawLine(17+i, 1, i+2, 64);
    u8g2.drawLine(18+i, 1, i+3, 64);
    u8g2.drawLine(19+i, 1, i+4, 64);
    u8g2.sendBuffer();
    delay(5);
  }
}

void sendToPushingBox(){
  //char DEVID1[] = "v********";
  //char serverName[] = "api.pushingbox.com";
  //sending = true;
  client.stop(); 
  if(client.connect("api.pushingbox.com", 80)) { 
    Serial.println(F("connected"));
    client.print(F("GET /pushingbox?devid=v******"));
    //client.print(DEVID1);
    client.println(F("&timestamp=LOGGED HTTP/1.1"));
    //client.print(F("LOGGED"));
    //client.println(F(" HTTP/1.1"));
    client.println(F("Host: api.pushingbox.com"));
    //client.println(serverName);
    client.println(F("User-Agent: Arduino"));
    client.println();
    //Serial.println(postmsg);
    //Serial.println(F("Host: api.pushingbox.com"));
    //Serial.println(F("Connection: close"));
    //Serial.println();
    /*for(int i=1; i<=25; i++) {
    digitalWrite(LED1, HIGH);
    delay(50);
    digitalWrite(LED1, LOW);
    delay(50);}*/
  } 
  else { 
    //if(DEBUG){Serial.println(F("connection failed"));} 
    /*for(int i=1; i<=10; i++) {
    digitalWrite(LED1, HIGH);
    delay(250);
    digitalWrite(LED1, LOW);
    delay(250);}*/
    
  }
  delay(10);
  //sending = false;
}


void resetMemory() {
  int writeVal[] = {30, 60};
  for(int i=0; i < 2; i++) {
    EEPROM.write(i, writeVal[i]);
  }
  mode = 1;
}

void startScreen() {
  //char Line1[12];
  //char Line2[20];
  if (hover == 0) {
    u8g2.clearBuffer();    
    u8g2.setFont(u8g2_font_timR10_tr);
    u8g2.setCursor(8,14);
    u8g2.print(F("> Start Dab"));
    u8g2.setCursor(8,30);
    u8g2.print(F("Edit Variables"));
    u8g2.sendBuffer();
  }
  else if(hover == 1) {
    u8g2.clearBuffer();    
    u8g2.setFont(u8g2_font_timR10_tr);
    u8g2.setCursor(8,14);
    u8g2.print(F("Start Dab"));
    u8g2.setCursor(8,30);
    u8g2.print(F(">Edit Variables"));
    u8g2.sendBuffer();
  }
  else {
    u8g2.clearBuffer();    
    u8g2.setFont(u8g2_font_timR10_tr);
    u8g2.setCursor(8,14);
    u8g2.print(F("Edit Variables"));
    u8g2.setCursor(8,30);
    u8g2.print(F("> Reset Memory"));
    u8g2.sendBuffer();
  }
}

void startDab() {
	byte heat = EEPROM.read(0);
	byte cool = EEPROM.read(1);
  for(int i=0; i < 1; i++) {
    u8g2.clearBuffer();         
    u8g2.setFont(u8g2_font_timR24_tr);  
    u8g2.setCursor(8,28);
    u8g2.print(F("Starting"));  
    u8g2.sendBuffer(); 
    delay(1000); //Dab delay
    u8g2.clearBuffer();         // clear the internal memory
    u8g2.setFont(u8g2_font_timR24_tr);  
    u8g2.setCursor(32,28);
    u8g2.print(F("Dab"));  // write something to the internal memory
    u8g2.sendBuffer(); 
    delay(1000); //Dab delay 
  }
  //Start Heating Sequence
  u8g2.clearBuffer();         
  u8g2.setFont(u8g2_font_timR24_tr);  
  u8g2.setCursor(8,28);
  u8g2.print(F("Heating"));  
  u8g2.sendBuffer(); 
  delay(1000);
  for(int i=1; i <=heat; i++) {
    char buffer[3];
    u8g2.clearBuffer();         
    u8g2.setFont(u8g2_font_timR24_tr);  
    itoa(i, buffer, 10);
    u8g2.drawStr(50,28, buffer);
    u8g2.sendBuffer(); 
    delay(1000);
  }
  //Switch Sequence
  u8g2.clearBuffer();         
  u8g2.setFont(u8g2_font_timR24_tr);  
  u8g2.setCursor(8,28);
  u8g2.print(F("Switch"));  
  u8g2.sendBuffer();
  delay(2000);
  for(int i=cool; i >= 0; i--) {
    char buffer[3];
    u8g2.clearBuffer();         
    u8g2.setFont(u8g2_font_timR24_tr);  
    itoa(i, buffer, 10);
    u8g2.drawStr(50,28, buffer);
    u8g2.sendBuffer(); 
    delay(1000);
  }
  //Start Hit Sequence
  for(int i=-128; i < 128; i++) {
    u8g2.clearBuffer();         // clear the internal memory
    u8g2.setFont(u8g2_font_logisoso28_tr);  // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
    u8g2.setCursor(i,29);
    u8g2.print(F("DAB DAB DAB"));  // write something to the internal memory
    u8g2.sendBuffer();          // transfer internal memory to the display
    delay(10);
  }
  delay(500); //end sequence delay
  mode = 0;
}

void editVariables() {
  delay(10);
  if (hover == 0) {
    u8g2.clearBuffer();         // clear the internal memory
    u8g2.setFont(u8g2_font_timR10_tr);
    u8g2.setCursor(18,14);
    u8g2.print(F("> Heat"));
    u8g2.setCursor(18,30);
    u8g2.print(F("Cool"));
    u8g2.sendBuffer();
  }
  else if (hover == 1) {
    u8g2.clearBuffer();         // clear the internal memory
    u8g2.setFont(u8g2_font_timR10_tr);
    u8g2.setCursor(18,14);
    u8g2.print(F("Heat"));
    u8g2.setCursor(18,30);
    u8g2.print(F("> Cool"));
    u8g2.sendBuffer();
  }
  else {
    u8g2.clearBuffer();         // clear the internal memory
    u8g2.setFont(u8g2_font_timR10_tr);
    u8g2.setCursor(18,14);
    u8g2.print(F("Cool"));
    u8g2.setCursor(18,30);
    u8g2.print(F("> Back"));
    u8g2.sendBuffer();
  }
}

void editHeat() {
	byte heat = EEPROM.read(0);
  heat += changeValue;
  changeValue = 0;
  //char buffer1[7] = "Heat: ";
  //char buffer2[3];
  //buffer1 += "Heat: ";
  //itoa(heat, buffer2, 10);
  //strcat(buffer1, buffer2);
  u8g2.clearBuffer();         // clear the internal memory
  u8g2.setFont(u8g2_font_timR24_tr);  // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
  u8g2.setCursor(18,30);
  u8g2.print(F("Heat: "));
  u8g2.print(heat);
  //u8g2.drawStr(8,28,buffer1);  // write something to the internal memory
  u8g2.sendBuffer();
  delay(10);
  EEPROM.write(0, heat);
}

void editCool() {
	byte cool = EEPROM.read(1);
  cool += changeValue;
  changeValue = 0;
  u8g2.clearBuffer();         // clear the internal memory
  u8g2.setFont(u8g2_font_timR24_tr);  // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
  u8g2.setCursor(18,30);
  u8g2.print(F("Cool: "));
  u8g2.print(cool);
  //u8g2.drawStr(8,28,buffer1);  // write something to the internal memory
  u8g2.sendBuffer();
  delay(10);
  EEPROM.write(0, cool);
}



void setup(void) {
	bool wifiEnabled = false;
  Serial.begin(115200);
  u8g2.begin();
  int status = WL_IDLE_STATUS; 
  pinMode(UP, INPUT_PULLUP);
  pinMode(DOWN, INPUT_PULLUP);
  pinMode(SELECT, INPUT_PULLUP);
  zoomBar();
  hover = 0; mode = 0;

  //EEPROM
  /*int j = 0;
  heat = EEPROM.read(j); j++;
  cool = EEPROM.read(j);*/

  //Wifi
  EspSerial.begin(9600);
  WiFi.init(&EspSerial);
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue
    while (wifiEnabled);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    //Serial.print("Attempting to connect to WPA SSID: ");
    //Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin("***", "*****"); //SSID, pass
  }
  //Serial.println(F("You're connected to the network"));
}

//long loopItterations = 0;
void loop(void) {
  switch (buttonVal) {
    case UP:
      buttonVal = 0;
      if(hover != 0) {
        hover--;
      }
      changeValue = 1;
      delay(150);
      break;
    case DOWN:
      buttonVal = 0;
      if(hover != 2) {hover++;} //hoverMax = 2
      changeValue = -1;
      delay(150);
      break;
    case SELECT:
      buttonVal = 0;
      if (mode == 1) {
        if(hover == 0) {mode = 2;}
        else if(hover == 1) {hover = 0; mode = 3;}
        else {hover = 0; mode = 6;}
      }
      else if (mode == 3) {
        changeValue = 0;
        if(hover == 0) {mode = 4;}
        else if(hover == 1) {mode = 5;}
        else {hover = 1; mode = 1;}
      }
      else if (mode == 4 or mode == 5) {
        hover = 0;
        mode = 3;
      }
      else {
        Serial.println(F("I dont know why you clicked the button bro"));
      }
      delay(200);
      break;
    /*case START:
      buttonVal = 0;
      delay(100);
      break;*/
    default:
      //buttonVal = 0;
      break;
  }

  switch (mode) {
    case 1:
      //hoverMax = 1;
      startScreen();
      break;
    case 2:
      startDab();
      break;
    case 3:
      //edit variables
      //hoverMax = 2;
      editVariables();
      break;
    case 4:
      editHeat();
      //edit heat
      break;
    case 5:
      editCool();
      //edit cool
      break;
    case 6:
      resetMemory();
      //Serial.println(F("Reset Memory"));
    default:
        for(int i=-100; i < 128; i++) {
          u8g2.clearBuffer();         // clear the internal memory
          u8g2.setFont(u8g2_font_logisoso28_tr);  // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
          u8g2.setCursor(i,28);
          u8g2.print(F("ACAB")); 
          u8g2.sendBuffer();          // transfer internal memory to the display
          delay(10);
        }
        mode = 1;
      break;
  }
  if(!digitalRead(UP)) {buttonVal = UP;}
  if(!digitalRead(DOWN)) {buttonVal = DOWN;}
  if(!digitalRead(SELECT)) {buttonVal = SELECT; delay(100);}
  /*loopItterations++;
  if(loopItterations%250 == 0) {
  Serial.println(loopItterations); }
  if(loopItterations == 25l*1) {
    Serial.println("loop 1");
    hover = 1;
  }
  if(loopItterations == 25L*2) {
    Serial.println("loop 2");
    hover = 0;
  }
  if(loopItterations == 25L*3) {
    Serial.println("loop 3");
    mode = 2;
  }
  if(loopItterations == 25l*4) {
    Serial.println("loop 4");
    mode = 1;
    loopItterations = 0;
  }*/
}

I suggest a different approach:
check the datasheet of a ESP8266 and compare how many RAM, how many Flash it would have compared to the NANO.

Then see the processor speed.
for me it doesn't make sense to add an ESP8266 to the NANO. Use the ESP instead of the NANO.

--> for a start, get a ESP8266 on a makerfriendly board, something like a NodeMCU or a WemosD1 and programm the ESP8266 itself, connect all components to the ESP, leave away the NANO for another project.

1 Like

Yes. +1 to @noiasca's comments. Using a Nano and an ESP like this is similar to pulling a Tesla car with a donkey.

You didn't say, so my guess would be that you are using an ESP-01. The problem with those is that although the ESP chip on the board is fully featured, there are very few pins available. Only 4 in fact, and you need 2 of those for uploading a sketch to the esp, leaving only 2. These can most usefully be used as an i2c bus, allowing you to attach many sensors and displays.

As mentioned, getting a fully featured esp development board like Wemos Mini makes life far simpler. But if you truly want to use the ESP-01, it can be done.

Have a look at the Nano 33 IOT

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.