2

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();",
  "Commands\ndiditalWrite();\ndidgitalRead();\npinMode();",
  "diditalWrite();\ndidgitalRead();\npinMode();\nanalogWrite();",
  "didgitalRead();\npinMode();\nanalogWrite();\nanalogRead();",
  "Arduino Types\nUno\nMega 2560\nLeonardo",
  "Uno\nMega 2560\nLeonardo\nNano",
  "Mega 2560\nLeonardo\nNano\nMini",
  "Leonardo\nNano\nMini\nEthernet",
  "Nano\nMini\nEthernet\nBluetooth",
  "THE END"};

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

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

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")), "");
    current[i].trim();
    local.trim();
    Serial.print("i=" + String(i) + ", current=" + current[i]);
    Serial.println(", local="+local);
  }
  lcd.clear();
  for (int i = 0; i != 4; i++){
    Serial.println(current[i]);
    lcd.setCursor(0,i);
    lcd.print(current[i]);
  }
  //lcd.print(data[slide]);
  slide += 1;
  Serial.println("New: " + String(slide));
}
6
  • Ouch, your poor poor heap... 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. Commented Apr 26, 2017 at 11:29

1 Answer 1

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.

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.