Hello fellow Arduino helper,
I'm experiencing a strange problem with my Neopixel Stick connected to an Arduino (tested with Nano and Mega) which is getting Data Input via a serial communication. The Neopixel LED show up seems like "delayed" or it even seems like that the Arduino is overloaded.
Let my clarify this a bit:
My idea is that I retrieve a few values from a game called iRacing. One of these values is the RPM count. With that RPM I want to create a RevLED which lights up LED's if the RPM is rising. Just like it is in a racecar.
The data retrieving from the game worked well so far and this isn't the problem at all. I even tried this whole thing with normal LED's and it worked as it should. Each LED glew up in the order I wanted and at the exact point I wanted, regarding to the RPM of my ingame-car.
Normal LED's weren't the best choice for me because of the much wiring work you have to do and that normal LED's have a fix color.
So I went on a research and found out that the Neopixel Sticks would fit my needs just fine. Just three connection pins, RGB and they seem like that they're easy to program.
My main problem is that the code that worked well with normal LED's don't work out with the Neopixel. The Neopixel seems like its delayed while showing up the colors on the stick. Or maybe the Arduino is overloaded because of the SerialInput?! (Imo, this shouldn't be any problem because it worked with normal LED before)
Here is an example of that "delayed" show up so you can understand my problem better. As you can see that some LED's show up later than others or in the wrong order.
The Code
The Input for the Arduino is sent by my PC as I mentioned. It is a C# script and that is the Code I use. Note: The RPM_array saves the RPM number at which the LED should light up. So if current_RPM is bigger than RPM_array[0] which is in my example 6093 (the comment at the end of the line) the C# script sends a command on which the arduino will show up the first and last LED. (I hope you get this if you see the C# and Arduino code)
So here is the C# code:
private void RPM_serialWrite()
{
if (current_RPM > RPM_array[0]) //6093
{
serialPort1.Write("&Q!");
}
if (current_RPM < RPM_array[0])
{
serialPort1.Write("&q!");
}
if (current_RPM > RPM_array[1]) //6212
{
serialPort1.Write("&W!");
}
if (current_RPM < RPM_array[1])
{
serialPort1.Write("&w!");
}
if (current_RPM > RPM_array[2]) //6331
{
serialPort1.Write("&E!");
}
if (current_RPM < RPM_array[2])
{
serialPort1.Write("&e!");
}
if (current_RPM > RPM_array[3]) //6450
{
serialPort1.Write("&R!");
}
if (current_RPM < RPM_array[3])
{
serialPort1.Write("&r!");
}
if (current_RPM > RPM_array[4]) //6569
{
serialPort1.Write("&A!");
}
if (current_RPM < RPM_array[4])
{
serialPort1.Write("&a!");
}
if (current_RPM > RPM_array[5]) //6688
{
serialPort1.Write("&S!");
}
if (current_RPM < RPM_array[5])
{
serialPort1.Write("&s!");
}
if (current_RPM > RPM_array[6]) //6807
{
serialPort1.Write("&D!");
}
if (current_RPM < RPM_array[6])
{
serialPort1.Write("&d!");
}
if (current_RPM > RPM_array[7]) //6926
{
serialPort1.Write("&Y!");
}
if (current_RPM < RPM_array[7])
{
serialPort1.Write("&y!");
}
}
Arduino Code:
#include <FastLED.h>
#define PIN 3
#define NUM_PIXELS 16
String str;
CRGB leds[NUM_PIXELS];
void setup() {
Serial.begin(250000);
FastLED.addLeds<NEOPIXEL, PIN>(leds, NUM_PIXELS);
}
void loop() {
while (Serial.available())
{
str = Serial.readStringUntil('!');
switch (str.charAt(0))
{
case '&':
str.remove(0, 1);
RevLed(str.charAt(0));
FastLED.show();
break;
}
}
}
void RevLed(char rpmChar)
{
switch (rpmChar)
{
case 'Q':
leds[0] = CRGB::Green;
leds[15] = CRGB::Green;
break;
case 'q':
leds[0] = CRGB::Black;
leds[15] = CRGB::Black;
break;
case 'W':
leds[1] = CRGB::Green;
leds[14] = CRGB::Green;
break;
case 'w':
leds[1] = CRGB::Black;
leds[14] = CRGB::Black;
break;
case 'E':
leds[2] = CRGB::Green;
leds[13] = CRGB::Green;
break;
case 'e':
leds[2] = CRGB::Black;
leds[13] = CRGB::Black;
break;
case 'R':
leds[3] = CRGB::Green;
leds[12] = CRGB::Green;
break;
case 'r':
leds[3] = CRGB::Black;
leds[12] = CRGB::Black;
break;
case 'A':
leds[4] = CRGB::Green;
leds[11] = CRGB::Green;
break;
case 'a':
leds[4] = CRGB::Black;
leds[11] = CRGB::Black;
break;
case 'S':
leds[5] = CRGB::Green;
leds[10] = CRGB::Green;
break;
case 's':
leds[5] = CRGB::Black;
leds[10] = CRGB::Black;
break;
case 'D':
leds[6] = CRGB::Green;
leds[9] = CRGB::Green;
break;
case 'd':
leds[6] = CRGB::Black;
leds[9] = CRGB::Black;
break;
case 'Y':
leds[7] = CRGB::Red;
leds[8] = CRGB::Red;
break;
case 'y':
leds[7] = CRGB::Black;
leds[8] = CRGB::Black;
break;
}
}
I tried the original Adafruit library aswell but it ended up with the same problem. The FastLED library seems faster anyways, right? click
Hopefully some of you can help me out with this particular, really annoying problem. I'll try to answer questions as soon as possible, if you need further information.
Fabian