Displaying RGB value on LCD screen

Hello, currently I am trying to do a small project where I am sending RGB values from C# to arduino, changing the LEDs and then displaying the RGB value on an LCD screen (SparkFunSerLCD).

I am changing the LED values no problem but when I try to display the value on the screen I am completely failing to get it to display 0-255 instead I get -133 to +133 where 255 = -1. I have now got myself completely confused after trying numerous conversions trying to get it to display the right value. I read the values into Arduino and place them in an array. The code I am using is;

c# serialPort1.Write(new byte[] {68, 255, 255, 255 }, 0, 4); // 68 = header

arduino

int LightValues[250][4]; char tempRedValue = 0; char tempGreenValue = 0; char tempBlueValue = 0;

LightValues[1][0] = 100; // index using to see display value of int LightValues[1][1] = serInString[1]); //value received 255 LightValues[1][2] = serInString[2]; //value received 255 LightValues[1][3] = serInString[3]; //value received 255

tempRedValue = LightValues[1][1]; tempGreenValue = LightValues[1][2]; tempBlueValue = LightValues[1][3];

analogWrite(led1Pin,tempRedValue); analogWrite(led2Pin,tempGreenValue); analogWrite(led3Pin,tempBlueValue);

clearLCD(); goTo(0); LCD.print(LightValues[1][0]); // displays 100 goTo(4); LCD.print(LightValues[1][1]); // displays -1 should be 255 goTo(8); LCD.print(LightValues[1][2]); // dispalys -1 goTo(12); LCD.print(LightValues[1][3]); // displays -1

Please help, I am sure it is something obvious but is really annoying me.

thanks

Dave

We would need to see all of your Arduino code, but there are some things that are clearly wrong with the code you did post. Array indexes start at 0, not 1. A 250 * 4 int array is going to use up 1/2 of the memory available on 328. Since you are only using 1 column of the array, is there a reason that there are 250 columns?

A 250 * 4 int array is going to use up 1/2 of the memory available on 328.

In fact twice that since an int is 2 bytes, IE all the SRAM memory on a 328. Assuming you have more variables, function calls etc (and you do, kinda hard to avoid), this is Very Bad[ch8482] for program execution.

Sorry, should have mentioned I am using an Arduino mega ( ATmega1280) so have enough memory to play with.

In my full program I am cycling through the array changing each LED value for the script I create in C# so I am using multiple rows, seemed irrelevant for my question. I am using array index 1 just because I changed the “x” to 1, could be 0, I can’t see how that can affect what I am trying to do.
Can change to;
int LightValues[1][4];
and
LightValues[0][0] = 100; // index using to see display value of int
LightValues[0][1] = serInString[1]); //value received 255
LightValues[0][2] = serInString[2]; //value received 255
LightValues[0][3] = serInString[3]; //value received 255

but still does not work.

Below is a mini version in a full sketch which displays the same results, if you send from serial monitor Dÿ it sets the LED to 255 but displays -1 and not 255;

#include <SoftwareSerial.h>

const int txPin = 2; //LCD Pin
const int ledRPin = 9;
const int ledGPin = 10;

char RedChar=0;
int RedValue=0;
SoftwareSerial LCD = SoftwareSerial(0, txPin);
char serInString[11];

void setup() {

Serial.begin(9600);
pinMode(ledRPin, OUTPUT);
pinMode(ledGPin, OUTPUT);
pinMode(txPin, OUTPUT);
LCD.begin(9600);
clearLCD();
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{

readSerialString(serInString);
if( isStringEmpty(serInString) == false)
{

if (serInString[0]==68)
{
Serial.println(“found input”);
RedChar = serInString[1];
RedValue = RedChar;
analogWrite(9, RedValue);
clearLCD();
LCD.print(RedValue);
}

}

for (int i=0; i <= 11; i++){

serInString*=0;*

  • }*
    *delay(100); *
    }
    void readSerialString (char *strArray) {
  • int i = 0;*
  • if(Serial.available()) { *
  • while (Serial.available()){ *
    _ strArray = Serial.read();_
    * i++;*
    * Serial.print(strArray[(i-1)]);*
    * }*
    * } *
    }
    void clearLCD(){
    * LCD.print(0xFE, BYTE); //command flag*
    * LCD.print(0x01, BYTE); //clear command.*
    }
    boolean isStringEmpty(char *strArray) {
    * if (strArray[0] == 0) {*
    * return true;*
    * }*
    * else {*
    * return false;*
    * }*
    }
    Hope you can now help.
    Dave

it sets the LED to 255 but displays -1 and not 255;

If you're displaying the decimal value of a "char" (signed eight bit), 255 (0xFF) is -1

Thanks for the response, I had tried using bytes before and had been getting similar results but your answer made something click in my head and I realised I had been doing my conversions in the wrong order. I needed to convert to byte before saving to an int and not convert my int to a byte.

Thought it would be a simple mistake.

Thanks again

Dave