I'm making an LED spectrum analyzer with an Arduino Due and my PC. The audio processing is done on the PC, and then sent to the Arduino. The exact data sent is a 'coordinate', or more specifically, a number representing a particular band, and another number representing the maximum amplitude of that band. There are 11 bands. Each coordinate has a x and a y value, separated by a comma, and then sent on a new line.
This is my code as of now:
void loop() {
if (Serial.available()) {
xCoord = Serial.parseInt();
yCoord = Serial.parseInt();
if (xCoord != 0) {
int r, g, b;
for (int i = 0; i < 8; i++) {
int fromCoordToIndex = ((xCoord - 1) * 8) + i;
//
//do some calculations for the color
//
strip.setPixelColor(fromCoordToIndex, strip.Color(r, g, b));
strip.show();
}
}
}
}
The problem is parseInt
seems to be very very slow (it's not the timeout issue, it's set to 1). From what I can see, it seems to be skipping some data, so every now and then a band gets missed out. The only way I can fix this is to insert a delay between each coordinate being sent. I found that 40 ms works ok, but remember there are 11 bands, so that means about half a second to refresh the whole board, which feels like a slide show...
I've tried inputting individual characters and then using toInt
, I've tried parseInt
obviously, I've tried native USB (it's a Due), and I've tried fiddling with baud rates. None seems to have had any effect.
parseInt
if your data has a rigid format (for example, always 3 digits long, padded with zeros or spaces. Tell us if that is the case (and what format are you using).strip.show();
out of the loop for a bigger speedup than type conversion overhead