GPIO on Jetson TX2 and synchronization

I am running Processing 3.5.4 on a Jeston TX2. It was also easy to access the GPIO pins. I wanted to get a digital pulse in a reliable way at the onset of given frame (code below), but it is not working well. I am monitoring the pulse and the changes on the monitor (with a photocell) using an oscilloscope. The pulse precedes the change in frame by ~40ms, which is longer than the duration of 2 frames (at 60Hz). What’s going on? Is there a way to block the code until the next VSYNC and issue the pulse right after? Thanks!

code

I am not sure but, isn’t it better to use millis() instead of frameCount in that project? You can see methods of using millis here: https://forum.processing.org/two/discussion/8085/i-display-images-in-sequence-but-i-see-only-the-last-one-why
One question: how could you install processing in a Jetson? I tried so many times in Jetson Nano and I couldn’t.