A problem with Arduino arrays?

For days now I’ve been trying to troubleshoot why my computer and arduino won’t talk over the serial port. Finally, I took to commenting out all the arduino code and adding it back one line at a time.

I assumed the problem was with reading the serial port, but instead I discovered that when I included the lines that added the incomingByte values to an array, the arduino seemed to stop working entirely.

So instead I created a fifty character string globally, like so:

String cc_string = "12345678901234567890123456789012345678901234567890";

Then, in the loop() function, I replaced characters in the string with the incoming bytes converted into characters like so:

        cc_count = cc_count + 1;
        cc_string.setCharAt(cc_count,char(incomingByte));
        //cc_list[cc_count] = incomingByte;

And finally I read out the characters like so:

    int ib;
    for (int i = 1; i<=cc_count; i++){
      ib = int(cc_string.charAt(i));
      if (ib == 125) {
          tri_flash();
          delay(1000);
      }
      if (ib == 126) {
          tri_flash();
      }

The function tri_flash() causes the pin 13 LED to flash rapidly three times. And that it did, twice, which confirms that both bytes 125 and 126 made it over the serial port and into the string and then were translated out again.

I’m not sure why I couldn’t just load the incomingByte variable into an integer array. It might have something to do with the size of the array (1000). Dimensioning the array didn’t cause problems, but when I then put commands into the loop function to actually load values into the array, that’s when the program/sketch wouldn’t even acknowledge that the data had been received.

Surely there’s a more elegant way to deal with this, but given my lack of expertise, perhaps it’s for the best if I just take my kludgey solution and move on.

But does it really work, or does it just appear to work? I’ll find that out tomorrow when I try to make the servos respond to the stored commands.

About engineerzero

Once and future engineer.
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s