0
votes

I'm running into a problem on Arduino.

  1. I want my Arduino listen to input from a piece of 12 key's keypad, this is easy, there is already keypad library avaliable.

  2. I want to display time on a 16X2 lcd screen that I read from a RTC module, update per second. This is also easy, implemented already.

BUT, when I try to combine these two tasks, to make arduino listen keypad and keep updating the lcd, there is trouble: I'm using a delay(1000) to tell arduino loop per second, which works for updating time on lcd, but it somehow blocks the keypad listening.

Is there any solutions for this thing? Like multithreading or other clever workaround?

2
Instead of using delay I would recommend using interrupts. This way you will never block. Take a look at a post I wrote some time ago which shows how to do this: blog.3d-logic.com/2012/08/26/…Pawel
Instead of using delay, how about measureing the time using millis() and run needed process after when mills() - lastRunTIme >= 1000? lastRunTime should be the last time when the process invoked and be meacured via millis()MikeCAT

2 Answers

0
votes

How about using millis() to control the interval of LCD updating like this?

unsigned long lastRanTime;

void loop() {
  if (millis() - lastRanTime >= 1000) {
    // update the lcd
    lastRanTime = millis();
  }
  // listen for the keypad
}
0
votes

as Pawel suggests the best practice is tp use interrupts on the key inputs. You can google about the concept of interrupts, there are plenty.

If you are on a project that needs a menu structure where you can access data and trigger functions AND just want to get things done by having a framework to do so, I recommend you using the LCDMenuLib:

https://github.com/Jomelo/LCDMenuLib

it's from a german guy. The project description on arduino.cc forum is mostly in german, but the thorough code comments and examples are in english. This framework will take care of displaying the menu structure and handling the key inputs for you in a non-blocking way (with interrupts). You can pretty much concentrate on what functions to trigger or what to display in the menu leafs.