Reputation: 619
So my Arduino is taking almost 200ms processing a total of 128 bytes. The whole process without writing over the serial port is only taking 25ms. Why is my Arduino bottlenecking so much?
Arduino
setColor
is simply setting the color of the ledstrip using the FastLED library.
void loop() {
if(Serial.available() == 4){
int led = Serial.read();
int r = Serial.read();
int g = Serial.read();
int b = Serial.read();
setColor(led, r, g, b);
Serial.write(1);
if(Serial.available() > 0) Serial.read();
}
}
C#
In a loop I am doing the following to write the data:
attempt:
if (Port.isReady) {
strip.Send(new byte[] { id, c.R, c.G, c.B });
Port.isReady = false;
} else {
goto attempt;
}
public void Send(byte[] bytes) {
port.Write(bytes, 0, bytes.Length);
}
And I read the Arduino response using:
private const int DONE = 1;
public void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) {
Console.WriteLine("- Serial port data received -");
Console.WriteLine("Nr. of bytes: {0}", port.BytesToRead);
if (port.ReadByte() == DONE) {
isReady = true;
}
port.DiscardInBuffer();
}
Upvotes: 1
Views: 861
Reputation: 792
Well it all depends on your baudrate. If your baudrate are 9600 you can receive 9600 bit per second, thats 1200 bytes.
So 128/1200 = 0.1066 = 107 ms spend on receiving 128 bytes.
Higher baudrates will give shorter readtime.
My guess is that it is because of the many calls to setColor()
You are calling that once for every 4 bytes so that is 32 times.
I don't know the execution time on that function, but if it's something like 2-3ms you'r hitting that 200ms fast.
Upvotes: 2