I am working on a project in which I need a timer counting down. But I ALSO need to constantly be checking for key input from a keypad. In the setup, I have a while loop for the timer, using delays to count down one integer every second. I need to be able to check for keypad input while the loop is going on.
I cannot seem to find any other setup than the one I have done…I did kind of trap myself with the delay. I mean, the Mega has a TON of memory and processing power. It has to be able to handle multitasking, yes?
I have tried to understand using timers and milliseconds, but I cannot wrap my head around it. Any help would be greatly appreciated!
Try to avoid delays and while() loops. Make loop() run as fast as possible. It should run several thousand times per second. Most of those times, there's nothing to do: no inputs have changed and no timers have expired.
Sometimes it does something. Try to make that something short. Don't wait for the user to push a button - that's an input that the main loop should be checking.
Let us assume that we have the following two tasks:
At the elapse of 200 mS time, we need to refresh '7-seg display unit' being driven by PORTD & PORTB.
A switch (K1) is connected at APin-A0. It is to be sampled for close condition and then ignite built-in
LED (L) of ArduinoUNO.
Solution: (for one pass only)
L1: Initiaize T1 to genertae 200 mS Time Tick
Initialize others as needed (enable internal pull-up at A0-pin and etc.)
L2: if (TOV1 has occured)
Refresh Display
L3: if (K1 is found closed)
Ignite LED (L)
L4: Halt
Transform the Pseuod Codes of Step-3 into C codes and accommodate them in the Arduino
void setup() {} and void loop() {} structures.
rowanaut20:
See my edit of the original post for what I did.
Please don't make changes to earlier Posts (apart from correcting typos) as it destroys the chronology of the Thread and someone reading it at a later date cannot learn from your experience.
If it is possible to restore the Original Post and add the new stuff after this it would be good.
rowanaut20:
I cannot seem to find any other setup than the one I have done...I did kind of trap myself with the delay. I mean, the Mega has a TON of memory and processing power. It has to be able to handle multitasking, yes?
Just jumping on this point here, a Mega actually doesn't have any more processing power than an Uno. It has more IO, peripherals, and memory, but the CPUs are clocked the same.
a Mega actually doesn't have any more processing power than an Uno.
And that's why you don't stay up really late programming, get frustrated, and write a question. I don't even know why I said that. But thanks for clarifying! :3
Jiggy-Ninja:
a Mega actually doesn't have any more processing power than an Uno. It has more IO, peripherals, and memory, but the CPUs are clocked the same.
The Mega is even a little slower, because it has to handle longer addresses on calling and returning.
The Mega is even a little slower, because it has to handle longer addresses on calling and returning.
ArduinoUNo uses Atmega328 chip with Flash Space : 0x0000 - 0x3FFF (word address).
(a) During SUR/ISR call, the MCU saves 2-byte data (the value of Return Address) onto stack.
(b) While returning from SUR/ISR, the MCU retrieves return address (2-byte) from stack.
(c) Let us assume that this stack handling time is t1.
ArduinoMEGA uses Atmega2560 with Flash Space : 0x000000 - 0x01FFFF (word address).
(a) During SUR/ISR call, the MCU saves 3-byte data (the value of Return Address) onto stack.
(b) While returning from SUR/ISR, the MCU retrieves the return address (3-byte) from stack.
(c) Let us assume that this stack handling time is t2.
t2 > t1; this is what has been concisely indicated by Whandall.