0

I (think I) am trying to iterate through the values of an array and print the string to a tft. I have declared the array of 4 strings, and that works fine. But when I try to print it, I get garbage. Am I missing a nuance of print? Or do I just not understand how to use arrays and strings...?

The code is as follows:

Declarations:

//Active System
char hvacSystems[4][5] = {"Off", "Fan", "Cool", "Heat"};
int activeSystem = 0;  
String displayActiveSystem = hvacSystems[activeSystem]; <-- Should now be String "off"

Use (note the fake if statement for the question)

  if(systemNeedsToBeCycled){
    activeSystem++;
    displayActiveSystem = hvacSystems[activeSystem]; <-- Should now be String "Fan"
    tft.print(displayActiveSystem);
  }

Instead I get random characters most of the time, sometimes nothing.

In case its a memory thing, please know that:

Sketch uses 26,480 bytes (92%) of program storage space. Maximum is 28,672 bytes. Global variables use 1,126 bytes (43%) of dynamic memory, leaving 1,434 bytes for local variables. Maximum is 2,560 bytes.

3
  • Maybe the baudrate is wrong? Or activeSystem is bigger than 3 and it gets out of bounds?
    – gre_gor
    Jul 9, 2015 at 21:54
  • ^ Bingo. I didnt have a catch on activeSystem.
    – John
    Jul 10, 2015 at 1:09
  • I didnt realize how fast it looped and it went over in milliseconds. it then progressed through a bunch of memory spitting garbage all the way. Man I feel stupid right now
    – John
    Jul 10, 2015 at 1:11

4 Answers 4

0

If the variable activeSystem exceeds the length of the array, it's out of bounds, at which point the behavior is undefined.
In your case you get "garbage data", which is actually just data in memory after the array.

Because the is no implicit check for array bounds, you need to check them yourself.

1

I think this is the correct way to create an array of C strings:

    char * hvacSystems[] = {"Off", "Fan", "Cool", "Heat"};
    int activeSystem = 0;  
    char * displayActiveSystem = hvacSystems[activeSystem]; 

    if(systemNeedsToBeCycled){
        activeSystem++;    // make sure to set a bound for this variable
        displayActiveSystem = hvacSystems[activeSystem]; 
        tft.print(displayActiveSystem);
    }

Since strings are basically stored as char pointers so an array of strings is an array of char * variables. Or you could create an array of Arduino strings:

    String hvacSystems[] = {"Off", "Fan", "Cool", "Heat"};
    int activeSystem = 0;  
    String displayActiveSystem = hvacSystems[activeSystem]; 

    if(systemNeedsToBeCycled){
        activeSystem++;    // make sure to set a bound for this variable
        displayActiveSystem = hvacSystems[activeSystem]; 
        tft.print(displayActiveSystem);
    }
0

I am not sure I understand 100% but here is my best shot. If the below is wrong the issue is not due to being out of memory. Try this

//Active System
char hvacSystems[] = {"Off", "Fan", "Cool", "Heat"}; 
// or char hvacSystems[4] = {"Off", "Fan", "Cool", "Heat"}
int activeSystem = 0;  
String displayActiveSystem = hvacSystems[activeSystem]; <-- Should now be String "off"

if(systemNeedsToBeCycled){
    activeSystem++;
    displayActiveSystem = hvacSystems[activeSystem]; <-- Should now be String "Fan"
    tft.print(displayActiveSystem);
}

Your line

char hvacSystems[4][5] = {"Off", "Fan", "Cool", "Heat"};

You are attempting to create a two dimensional array here. Essentially a 4x5 table. With your code if you do

tft.print(hvacSystems[0][1]);

It should give you "Off". Basically you made a table that looks like

0{"Off", "Fan", "Cool", "Heat", null}

1{null, null, null, null, null}

2{null, null, null, null, null}

3{null, null, null, null, null }

Arrays: https://www.arduino.cc/en/Reference/Array 2d Arrays:

http://forum.arduino.cc/index.php?topic=220385.0

1
  • It wont compile without the second dimension or with no dimension. Which makes sense if you consider that a string is an array of char's [4][5] 0{"O","F","C","H"} 1{"f","a","o","e"} 2{"f","n","o","a"} 3{"/0","/0","l","t"} 4{null, null, "/0","/0"}
    – John
    Jul 10, 2015 at 11:11
0

The problem does not appear to be what you think it is, because I can't reproduce it.

See this:

//Active System
char hvacSystems[4][5] = {"Off", "Fan", "Cool", "Heat"};
int activeSystem = 0;  

String displayActiveSystem;

bool systemNeedsToBeCycled = true;

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  displayActiveSystem = hvacSystems[activeSystem]; // <-- Should now be String "off"

  Serial.println (displayActiveSystem);

  if(systemNeedsToBeCycled){
    activeSystem++;
    displayActiveSystem = hvacSystems[activeSystem]; // <-- Should now be String "Fan"
    Serial.println (displayActiveSystem);
    }
  }  // end of setup

void loop ()
  {

  }  // end of loop

Output:

Off
Fan

Please post a complete sketch that reproduces the problem. The placement of variables, and their type, might influence the result.

I used Serial.println() to show the variable contents, which are what you expected.


Possibly this will work better:

tft.print(displayActiveSystem.c_str ());
1
  • while you were hindered by my lack of code, the problem was there. activeSystem was unlimited and indexing invalid areas of hvacSystems after 3 loops. The loops run so fast that I missed the valid data at the beginning.
    – John
    Jul 10, 2015 at 11:18

Your Answer

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

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