1

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();
}
Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
Arjan
  • 599
  • 5
  • 23
  • what do you mean by "_without writing over the serial_" ? that's about all it does – dandavis Mar 25 '17 at 23:54
  • I'm also doing a lot of calculations but those only take about 25ms. If I add that one line of writing to the Arduino then the whole process takes 250ms. – Arjan Mar 26 '17 at 10:09
  • 1
    Without the whole program, we can't tell what baud rate you are using, nor how many LEDs you are controlling. These (and perhaps other *unseen* things) affect the timing. – slash-dev Mar 26 '17 at 14:03

1 Answers1

2

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.


Well why is it then taking 200ms you then ask?

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.

XerXeX
  • 784
  • 4
  • 16