Hi, I'm trying to make ambient lightning by getting average color of 114 squares on the borders of my screen. I made python scripts that is getting these values but I have trouble with receiving this big message on Arduino. Every square is ", #FFFBFF", so 8 characters and that times 114 = 912 characters, that Arduino needs to receive and alocate to list that later will be used to assign each WS2812B led it's color.
So how do I send this huge message, or maybe you guys have another solutions to this problem.
That many characters are too many for the serial buffer to hold so you you will need a reception method like in Robin2's serial input basics tutorial.
When i use 3rd code in Serial Input Basics and modify const byte numChars to 300, then send #3a3c40#3b3d41#37393c#2e3033#262829#292a2d#2a2b2e#2b2c2f#26272a#212225#262829#212225#2d2e2f#2c2e31#26282b#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#1e1f22#212123#262627#212325#2c2c2f
via serial monitor, i get only #483e3e#434344#3e3f3f#404140#3e3c3c#313333#
back.
Please post the code, using code tags.
I don't see what you're receiving in the characters you're sending
@Robin2 's tutorial was a game changer for me. Essential part of my Arduino coding toolkit now.
If you send binary data instead of ASCII characters, it would be 114 x 3 = 342 bytes.
That would also allow you to read the data directly into the LED array, provided you have the RGB values in the correct order.
Main problem at that point is determining the start of each set of data.
If you want to use the ASCII form of the data, then do not try reading the entire 912 characters into a buffer, instead convert the color from ASCII to binary for each LED as it is received, and store that into the LED array.
By adding a specific byte as a start mark for each three bytes increase the number of bytes by 33%, but make doing with it more straightforward. For example, start every color set from 0xEE. Of course, the OP will needs to modify your sending script to replace all occurrences of 0xEE in the input data to nearest value, say 0xEF
Robin's code does not cater for buffers greater than 256 due to the use of byte for the counters; below is his code modified to cater for a bigger buffer by using uint16_t instead of byte.
// Example 3 - Receive with start- and end-markers
const uint16_t numChars = 300; <<<
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
while(!Serial);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static uint16_t ndx = 0; <<<
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
This does not compile due to the <<<
used to indicate the changes so you can compare; just remove them once you know what needs to be changed in his original code.
Note
300 (your buffer size) - 256 (overflow of your byte variable) equals 44 and that is the size of the buffer that you created.
PS
Did you mention which Arduino you are using? I might have missed it.
if it needs really to be so big, process each value after you have received it.
If you really need all in a buffer explain why.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.