Racing sim shifter Megajoy input delay Arduino Mega 2560 with ATmega16u2.

Hello all
This could be something simple or stupid even, I’m by no means experienced with Arduino programming or hardware. With that I’d appreciate examples with insights or suggestions if it’s more advanced code.

I’m using MegaJoyArduinoSample.ino sktech that came with Megajoy, all of that is working perfectly fine. I have my joysticks in windows so base Megajoy functionality is fine.

I’m hoping I can get a little help with a issue i’m having with addition I’ve made to sketch.
I’m trying to activate joystick button based on specific ranges of values form analog A6 and A7 pins. These pins are x and y axis on second joystick in windows (standard Megajoy).
My sketch is kind of working, i used “Ifs” to bring corresponding digital input LOW when target A6, and A7 values are reached. Problem is that the button activation has a ~5 second delay.
Puzzling part of all this, is that I have a “else” in there which resets the digital inputs HIGH when A6 and A7 are outside of specified ranges, that part works instantly.
I attached the sketch and library file.

Thank you

MegaJoy.h (9.36 KB)

Shifter_Adaptor.ino (2.53 KB)

    for(int s = 0; s <= 8 ; s++ )digitalWrite(g, LOW);

Please explain. The way you have this, if g is non-zero, you write a LOW to pin g nine times. Why? After you set it LOW the first time, the next 8 writes of LOW do absolutely nothing.

And it’s likely this part will get optimized away as effectively it does nothing:

for (int s = 0; s <= 8 ; s++ )

If you had this

for(int s = 0; s <= 8 ; s++ ){
digitalWrite(g, LOW);
digitalWrite(g, HIGH);

you would see the pin toggle quickly 8 times.