String output disappear

Board : arduino nano

String menuItems[] = {"TMP:"+String(tmp), "RH:"+String(rH), "LDR:"+String(LDR), "SM:"+String(SM), "ESP8266" };

output:
**
TMP:NAN
RH: NAN
LDR:734

ESP8266
**
the line "SM" seem disappear,
and when i try to debug
this is work

String menuItems[] = { "TMP:x", "RH:x", "LDR:x", "SM:x", "ESP8266" };

and this is work

String menuItems[] ={String(tmp), String(rH), String(LDR), String(SM), "ESP8266")}

and when i try to combine the code it not work
how do i fix this

Welcome to the forum

Please post a complete sketch that illustrates the problem

this is whole sketch


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include <Fonts/FreeMono12pt7b.h>
#include <DHT.h>



#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels

#define OLED_RESET -1  // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

////////////////////////////////////////////////////////////////////////

#define DHTTYPE DHT11

#define BUTTON_UP_PIN 8
#define BUTTON_DOWN_PIN 9
#define BUTTON_ENTER_PIN 10

#define LDR_A A3
#define SM_A A4
#define DHT11_IN 12
#define DHT11_OUT 13
////////////////////////////////////////////////////////////////////////

#define MENU_ITEMS 5
#define ITEMS_PER_PAGE 3

int selectedOption = 0;
int menuOffset = 0;

DHT dht_in(DHT11_IN, DHTTYPE);
DHT dht_out(DHT11_OUT, DHTTYPE);

void setup() {
  pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
  pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
  pinMode(BUTTON_ENTER_PIN, INPUT_PULLUP);


  Serial.begin(9600);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(("display not found"));
  }
  display.clearDisplay();
  //SUART.begin(9600);
  dht_in.begin();

  display.clearDisplay();
}

bool in_submenu = false;
bool previousUpButtonState = true;
bool previousDownButtonState = true;
bool previousEnterButtonState = true;

/////////////////////////////////////////////////////////////////////////////////////////

void displayMenu(String menuItems[]) {
  int endIdx = constrain(menuOffset + ITEMS_PER_PAGE, 0, MENU_ITEMS);

  for (int i = menuOffset; i < endIdx; i++) {
    int y = (i - menuOffset + 1) * 20;
    display.setCursor(0, y);
    if (i == selectedOption) {
      display.drawRect(0, (i - menuOffset + 1) * 20 - 16, SCREEN_WIDTH, 20, WHITE);
    }
    display.print(menuItems[i]);
    // Serial.println(menuItems[i]);
  }
}

void subMenu() {
  display.clearDisplay();
}

/////////////////////////////////////////////////////////////////////////////////////////
unsigned long previousMillis = 0;
float rH = 0;
float tmp = 0;
bool tunnel_av = true;


void loop() {
  bool upButtonState = digitalRead(BUTTON_UP_PIN);
  bool downButtonState = digitalRead(BUTTON_DOWN_PIN);
  bool enterButtonState = digitalRead(BUTTON_ENTER_PIN);

  if (millis() - previousMillis >= 3000) {
    previousMillis = millis();
    rH = dht_in.readHumidity();
    tmp = dht_in.readTemperature();
  }
  int LDR = analogRead(LDR_A);
  int SM = analogRead(SM_A);

  Serial.println(LDR);
  Serial.println(SM);
  /*
  String menuItems[MENU_ITEMS];

  menuItems[0] = "TMP:" + String(tmp);
  menuItems[1] = "RH:" + String(rH);
  menuItems[2] = "LDR:" + String(LDR);
  menuItems[3] = "SM:" + String(SM);
  menuItems[4] = "ESP8266";

  */
  String menuItems[] = { "TMP:" + String(tmp), "RH:" + String(rH), "LDR:" + String(LDR), "SM:" + String(SM), "ESP8266" };
  //String menuItems[] ={String(tmp), String(rH), String(LDR), String(SM), "ESP8266"};

  //String menuItems[] = { "TMP:x", "RH:x", "LDR:x", "SM:x", "ESP8266" };

  Serial.println("------------");

  if (upButtonState && !previousUpButtonState) {
    selectedOption--;
    if (selectedOption < 0) {
      selectedOption = 0;
    }
    if (selectedOption < menuOffset) {
      menuOffset = selectedOption;
    }
  }

  if (downButtonState && !previousDownButtonState) {
    selectedOption++;
    if (selectedOption >= MENU_ITEMS) {
      selectedOption = MENU_ITEMS - 1;
    }
    if (selectedOption >= menuOffset + ITEMS_PER_PAGE) {
      menuOffset = selectedOption - ITEMS_PER_PAGE + 1;
    }
  }

  if (enterButtonState && !previousEnterButtonState) {

    in_submenu = !in_submenu;
  }

  display.clearDisplay();
  display.setTextColor(WHITE);

  display.setFont(&FreeMono12pt7b);

  if (in_submenu) {

  } else {

    displayMenu(menuItems);
  }

  display.display();

  previousUpButtonState = upButtonState;
  previousDownButtonState = downButtonState;
  previousEnterButtonState = enterButtonState;
}

If this is the Classic Nano (ATmega328), beware that String operations corrupt memory, leading to program crashes, and that the TFT display consumes half of SRAM (not reported in the memory use summary at load time).

Avoid using Strings, they are never necessary.

oh thanks a lot , i think that why :sweat_smile:
i change String to char ,more hard work but it work now
thanks again :grinning:

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