Hey guys, Im working on a project that will set the color of an RGB LED strip through an android app and send the color data to the arduino. I have various pieces working alone but combining all the aspects is giving me a fit.
Im storing the color data in an array for each LED (8 for now)
int LED1[3] = {255,255,255};
int LED2[3] = {255,255,255};
And using that color data to turn on the LED when the sketch loads, shining white. I then can connect my phone over bluetooth, tested this with a couple different apps and sketches. I have an android app that sends data in the following format over bluetooth
<L1,131,189,3>
< = start of message
L1 = LED1
131 = red value
189 = green value
3 = blue value
= end of message
So I need help with the code to read the serial data starting with < and ending with > and fill in LED1[x] with the values sent for the RGB color info.
You will need to right a simple splitter code. Using IF/Else statement, read in one char at a time and use some IF statements to look for '<' '>' and ',' . You can use Arrays to store the data and then convert them into integers for you LEDs.
You can take it a step further and incorporate case statements to switch between multiple LEDs.
There already is a sample code provided in the IDE that allows you to enter 3 values and will adjust the LED accordingly.
Using the simple code below I have my bluetooth adapter hookup up to pins 10 &11 using software serial and displaying the info received from android to the serial monitor. If I send a number of 1 I get back a square (im not sure how to display that on a forum) and if I send text such as "hello" I get back h,?½ÿ
Im pretty sure I need to get this simple sketch working before I move on any further.
What happens if you just read in a single char, do you get back that char? The code you posted will NOT work. What you should do is make a set sized array and store the chars each time a new one comes in. So if data is available, update a counter and add the new char to the array. And it may need a null terminator at the end of the array.
To view what you have, use a for loop to display each char in the array.
You may use the '<' and '>' chars as signaling the start and end of a string. When you get a '<' you start appending characters to your target string, when you get '>' you stop appending and process the string. You can use a char[] instead of a String. Each time you read a '<' you initialize the array to 0. Since all your data are integers you may try this:
// assuming you declared led_no, red_val, green_val, blue_val as integers
sscanf(your_string, "L%d,%d,%d,%d", &led_no,&red_val,&green_val,&blue_val);
So I need help with the code to read the serial data starting with < and ending with >
This code will do that:
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
and fill in LED1 with the values sent for the RGB color info.
Parsing the data using strtok() and atoi() is relatively simple. Google the functions. If you have questions, ask.
Thanks for all the help already, I am close to having this working 100%.
I am sending data in the following format as of right now..
<2,3342,5232,2442>
a serial.println of inData will print...
2,3342,5232,2442
just as expected. Passing inData through a few strtok functions I get back the following values