How do I reduce serial latency between PC and Arduino?

Recently I'm making a hardware hotkey macro program using Python & Arduino Leonardo.

I just successfully ran the program as I intended, but there is still a problem, that is latency.

Is this what it meant to be? Or is there any way to fix this problem?

My code is below :

#include <Keyboard.h>

char upArrow = KEY_UP_ARROW;
char downArrow = KEY_DOWN_ARROW;

char receivedChar; //a vairable for saving a letter transmitted by python

unsigned long lastDebounceTime; //declare a variable for debouncing
volatile int state = false; //declare a variable for using switch

void setup() {
    Serial.begin(115200);
    Keyboard.begin();
    pinMode(2, INPUT_PULLUP); //set pin2 as input
    pinMode(13, OUTPUT); //activate built-in LED
    attachInterrupt(digitalPinToInterrupt(2), isr, FALLING); //operate isr when pin2 gets falling edge signal
}

void loop() {
    while(!state); //wait until state == true
    recieveChar(); 
}

void isr()  { //activate/deactivate Arduino by using switch
    if ( (millis() - lastDebounceTime) < 200   )  return;  //ignore bouncing occurred within 0.2sec from initial input

    if (state == false) {
        state = true; //activate Arduino
        digitalWrite(13, HIGH); //turn on built-in LED
    }
    else {
        state = false; //deactivate Arduino
        digitalWrite(13, LOW); //turn off built-in LED
    }
    
    lastDebounceTime = millis(); //set the reference point of debouncing as current time
}

void recieveChar() { //a function recieves a letter from python thru serial communication
    if (Serial.available()) {
        receivedChar = Serial.read();
        pressKey();
    }
}

void pressKey() { //a function operates hotkey based on transmitted letter in recievechar()
    if(receivedChar =='a') {
        Keyboard.press(upArrow); 
        delay(25);
        Keyboard.release(upArrow);
        delay(25);

        Keyboard.press(downArrow); 
        delay(25);
        Keyboard.release(downArrow);
        delay(25);

        Keyboard.press('c'); 
        delay(25);
        Keyboard.release('c');
        delay(25);
        }
}

What duration of latency are you experiencing ?

Could the delay()s in your sketch have anything to do with it ?

When the program is running There is some noticable input delay between computer and arduino. For instance, When I press 'a' key arduino react to it almost 1 second later. the delay function has nothing to do with that.

You can create some debug statements to narrow down the cause of the delay. Print the value of millis() immediately after receiving a character, and then again at each further processing stage.

What is running on the PC to send keystrokes to the Arduino via the serial port?

Does the Leonardo have LEDs on the serial lines like the UNO? If so, when you hit the key, is there noticeable latency before they "flicker" during the message transfer? This would tell whether the latency problem is on the PC side or the Arduino side.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.