Array corrupts its characters C++ -


i'm writing small program arduino able read rgb values char array of hex color codes. let me give example because hard explain differently:

from arduino serial monitor example send this:

/1ffffff000000

the first character tells arduino sequence of hex color codes. second character tells how many color codes there (it starts 0. 1 means 2 colors). loops trough 6 characters of every hex code , adds respected place in hex[] char array. hex[] array 2 dimensional because in first "dimension" has sequence number of color , in second stores rgb values of color.

the output of following:

255 255 255 0 0 0 //the first part okay, the second gets messed up.

255 255 0 0 0 0 0 //the red value of next color gets set blue value of previous color

and here code. could't find easier method idea work. if have suggestion on how make better or more efficient please let me know.

thanks in advance!

char barva[10]; char hex[10][2]; long bluetoothval;  bluetoothval = serial.read();      if (bluetoothval == '/')     {           delay(2);         serial.flush();         input=serial.read();         char load = input;         int steps = load - '0';          (int counter = 0; counter <= steps; counter++)         {              (int = 0; <= 5; i++)             {                 delay(2);                 serial.flush();                  delay(2);                 serial.flush();                 bluetoothval=serial.read();                 char load = bluetoothval;                  barva[i] = load;                 }              long int rgb = strtol(barva,0,16); //=>rgb=0x001234fe;             hex[counter][0] = (byte)(rgb>>16);             hex[counter][1] = (byte)(rgb>>8);             hex[counter][2] = (byte)(rgb);              serial.println(hex[counter][0]);             serial.println(hex[counter][1]);             serial.println(hex[counter][2]);                }          (int = 0; <= 1; i++)         {           serial.println("");           serial.println(hex[i][0]);           serial.println(hex[i][1]);           serial.println(hex[i-1][2]);             }     } 

hex should declared

char hex[10][3]; 

you accessing hex hex[counter][2] = (byte)(rgb); @ 1 place. require 10 * 3 array.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -