How to simultaneously deal with keypad+lcd and (locking) socket connections

Hi, Im developing a loooong project with an arduino mega, a keypad+lcd panel (i2c driven), RTC clock, ethernet shield, etc. I need to process keypad presses as soon as they come. Depending on what the user presses, I need to show certain information on the LCD. But this can't have any delay Also I need to periodically connect to the server to download and update files.

My issue now: The ethernet.connect function is locking everything until it returns. It has a long timeout. So if the server is responding slowly or is off, I won't get the control back into my arduino "main" until 15-20 seconds. During that time the user may be hitting keys but he won't see anything in the LCD.

So I turned to using interrupts. Twice a second my ISR is called to check for pending keypresses, and in case there's something, show it onscreen. So even if Im in the middle of an ethernet.connect funcion, it gets interrupted to attend key presses correctly.

The problem Im experiencing is that it eventually gets frozen, Im afraid it has to deal with calling complex functions from within my ISR. The theory says you should be really short within ISR calls, do as little as you can and not call complex functions. In my case, as I have to show the pressed key in the LCD, I need to read the key press, show a string, show a number, etc, all through I2c communication.

Does anyone figure out a better way to do this? I was thinking of using mutithreading but it looks like Arduino Multithreading is not "real" so it will only help in "organizing" my source code but won't really alter the way it's run.

Thanks in advance!

Can you use a second arduino to run the keypad & LCD in a timely fashion and tell the Mega about it over serial?

edugimeno: But this can't have any delay

Delayless is impossible, even the LCD has a delay ;)

edugimeno: So I turned to using interrupts.

Don't.... Fix the real problem and that's using blocking functions. And if the library you use only has blocking functions, switch the library.

edugimeno: Does anyone figure out a better way to do this?

See the above, get rid of blocking functions :)

Wildbill: No, I can't switch to using 2 arduinos now. Due to budget, and to box size constraints basically. The keypad+LCD panel (bv4242 by ByVac) is pretty nice and has its own controller that takes commands, buffers keys, etc, but I still have that issue

Septillion: Do you mean switching to ? Have you tried it? My program is pretty comples at this time, would it be a transparent (no major change in code) switching?

Thank you

I have no idea. First of all because I don't see you code nor do I know what you use now. And second, it's been a long time since I played with ethernet...