For a project, I was asked to design a presentation on Arduino. I now have run into the problem of my code suddenly stopping. It worked fine one day, I entered a new item in the array, it stopped working. It looked like I was being returned all newlines insteaed of debug info. Can anyone help?

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

int slide = 0;
const int slideNum = 13;
const String data[] = {
  "What is Arduino?\nArduino is a board\nthat can be used to\neasily program", 
  "that can be used to\neasily program\ncomplex behaviors.", 
  "Purpose\nMoney\nEase of Use",
  "How are they\ncontrolled?\nIDE, C++, Library\n", 
  "Commonly Used\nCommands\ndiditalWrite();\ndidgitalRead();",
  "Arduino Types\nUno\nMega 2560\nLeonardo",
  "Uno\nMega 2560\nLeonardo\nNano",
  "Mega 2560\nLeonardo\nNano\nMini",
  "THE END"};

//Addr: 0x3F, 20 chars & 4 lines
LiquidCrystal_I2C lcd(0x27,20,4); 

void setup()
  lcd.setCursor(0, 0);
  lcd.print("Send Newlne to start");
void loop()
  while (Serial.available() <= 0){
  Serial.println("Current: " + String(slide));
  while (Serial.available() != 0){
  if (slide == slideNum){

void next(){
  String local = data[slide];
  String current[4];
  for (int i = 0; i != 4; i++){
    current[i] = local.substring(0, local.indexOf("\n"));
    local.replace(local.substring(0, local.indexOf("\n")), "");
    Serial.print("i=" + String(i) + ", current=" + current[i]);
    Serial.println(", local="+local);
  for (int i = 0; i != 4; i++){
  slide += 1;
  Serial.println("New: " + String(slide));
  • Ouch, your poor poor heap...
    – Majenko
    Commented Apr 25, 2017 at 22:18
  • Change const String to const char *. Commented Apr 25, 2017 at 22:21
  • @TisteAndii Provide that as an answer, it does work. Commented Apr 25, 2017 at 22:42
  • I edited the question to format the code; the HTML tags you put around the code didn't work. The text in questions and answers on SE sites use Markdown Syntax, a sort of bastardized HTML. ¶ You should fix those silly misspellings (extra d, or d instead of g, in "diditalWrite" and "didgitalRead" [sic] Commented Apr 26, 2017 at 2:49
  • You should really use PROGMEM to force the strings to remain in Flash. Oh, and never use String for anything, ever.
    – Majenko
    Commented Apr 26, 2017 at 11:29

1 Answer 1


Since you haven't provided all of your code, I can't say for sure exactly what caused your problem except that Arduino String operations employ dynamic allocation a lot. This will cause heap fragmentation, which is especially bad on a chip like the ATmega328 with little RAM and subsequently cause weird issues like you're getting. I would guess that initializing the String members of the array takes up a lot of heap space while fragmenting it and when you added one more string, you ended up with a stack-meets-heap situation at some point in loop().

You're a lot better off with an array of compact strings using an array of pointers to char i.e. const char * array[] = ... since the strings are literals and so already known at compile time.

  • Thanks! Honestly, I copied all of my code, don't know why it didn't paste. Commented Apr 26, 2017 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.