2

I'm writing a small program for the Arduino that is able to read RGB values from a char array of HEX color codes. Let me just give you an example because it is hard to explain differently:

From the arduino serial monitor I for example send this:

/1ffffff000000

The first character tells the Arduino that this will be sequence of hex color codes. The second character tells it how many color codes there will be (it starts with 0. Thus 1 means two colors). Then the it loops trough six characters of every HEX code and adds it to the respected place in the hex[] char array. Hex[] array is two dimensional because in the first "dimension" it has the sequence number of a color and in the second it stores the RGB values of that color.

The output of this is following:

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

255 255 0 0 0 0 0 //the RED value of the next color gets set as the BLUE value of the previous color

And here is the code. I could't find any easier method for this idea to work. If you have suggestion on how to make this 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';

        for (int counter = 0; counter <= steps; counter++)
        {

            for (int i = 0; i <= 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]);       
        }

        for (int i = 0; i <= 1; i++)
        {
          Serial.println("");
          Serial.println(hex[i][0]);
          Serial.println(hex[i][1]);
          Serial.println(hex[i-1][2]);    
        }
    }
orglce
  • 513
  • 2
  • 7
  • 19

1 Answers1

4

hex should be declared as

char hex[10][3];

You are accessing hex as hex[counter][2] = (byte)(rgb); at one place. For this you require a 10 * 3 array.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • Realy?? hahah I have been figuring out this for two days and finally got it to work. Thank you so much!! :D – orglce Feb 08 '16 at 11:12