Hi!
I am working on a project with a strip of 300 LEDs, and I am using a python script to send, a string of numbers separeted with commas (f.e. "4,1,2,3,4,5" would turn the LED "1" in red and the "2","3","4" and 5 LEDs green, the "4" is just the number of green LEDs), to tell the Arduino which LEDs to turn on.
My problem is that I have written some arduino code that takes this string and actually turns on the right LEDs, however when the arduino is working for a while (maybe less than a minute) it freezes. To try to figure out what is happening I printed the data that the Arduino is reading and I get this:
The two last strings written where: "140,41,76,81,116,121,156,155" and
"140,76,81,116,121,156,155,154". Using ser.write(variable) in python where variable="140,41,76,81,116,121,156,155" (if needed I can share more of the python code, but as it works for a while I think this is an arduino problem).
#define MAX_NUMBERS 30 // Maximum number of numbers to read
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 300
// Array for the LEDs
CRGB leds[NUM_LEDS];
int* parseNumbers(char* str) {
static int numbers[MAX_NUMBERS]; // Array to store numbers
int num_index = 0; // Index of current number being read
char* p = strtok(str, ","); // Get the first token
while (p != NULL && num_index < MAX_NUMBERS) {
numbers[num_index] = atoi(p); // Convert the token to an integer and store it in the array
num_index++; // Increment the index to read the next number
p = strtok(NULL, ","); // Get the next token
}
return numbers;
}
void setup() {
// Setting up the LEDs info
Serial.begin(115200);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.clear();
FastLED.show();
}
void loop() {
// Attempt to kill the loop if something breaks
int starttime = millis();
int endtime = starttime;
while ((endtime - starttime) <=40)
{
if (Serial.available() > 0) {
char str[180];
int* numbers;
int i;
int num_index = 0;
// Read input string from serial
while (Serial.available() > 0 && num_index < sizeof(str) - 1) {
char c = Serial.read();
if (isdigit(c) == false & c!=',' ) {
break;
}
str[num_index] = c;
num_index++;
}
str[num_index] = '\0'; // Null-terminate the string
// Parse the string into an array of integers
numbers = parseNumbers(str);
FastLED.clear();
leds[numbers[0]] = CRGB(255,0,0);
Serial.println(numbers[0]);
for (i = 1; i < MAX_NUMBERS; i++) {
leds[numbers[i]] = CRGB(0,255,0);
Serial.print(numbers[i]);
Serial.print(" ");
}
//Serial.println("");
FastLED.delay (50);
//delay(10);
}
}
}
This is the code I am using, I do not understand why it works during a while but suddenly it breaks. I tried to create a "filter" with if (isdigit(c) == false & c!=',' )
to try and avoid this weird symbols and also I tried to add a limit to the time the arduino has to finish the loop but none of these work. If someone could give some insights on what may be happening, or how to write an exception to check and catch this weird characters or even provide a better way to send information between a python script and the arduino it would be apreciated. (Also if there is any optimization to the code, I know it is not the cleanest but I did not know how to read a string separeted with commas and pass that to an array in a better way)
Thanks in advance!