Multitask?

I am creating a system that needs to multitask. I need it to be doing sending commands off to locomotives (function "a"), changing status of a Liquid Crystal Display (function "b"), and sensing a 16 pin keypad (function "c"). And any suggestions on how to make this keypad work would be great. It is 16 pin and if you need a data sheet, https://www.jameco.com/Jameco/Products/ProdDS/169245.pdf

Have you looked in the Playground for ways of using the keypad?

Yes, however i am really new to this. and didn't understand too much. Will take another look.

tfoote:
And any suggestions on how to make this keypad work would be great. It is 16 pin and if you need a data sheet, https://www.jameco.com/Jameco/Products/ProdDS/169245.pdf

You mean "it is 16 key (8 pin)". Try the keypad library.

http://arduino.cc/playground/Code/Keypad
http://arduino.cc/playground/Main/KeypadTutorial

got the keypad part. Thanks! Now multitask?

Now multitask?

On an Arduino? No.

Now, why don't you describe what you need to do, not how you think you need to do it.

Your not trying to multitasking in the true (around computers sense). Take a look at blink without delay (see examples). Also google "polling" with micros processors.

If you want real help then you must be seen to be trying to do the work your self and you must always post you code!.

Mark

Ok, so I am creating a Digital Command Control System for my model Trains. This involves me getting input from a keypad (4 X 4), and potentiometors. Also i will be displaying information on a Liquid Crystal Screen. So you know that basis of how the DCC system works, it sends 0's and 1's to the train by varying the voltage. But in short, a zero means that a digital pin will be on for 100 microseconds and then off for 100 microseconds. a one, is on for 58 microseconds and off for 58 microseconds. I have programmed how the sending of that information and how it will be done. Now i need to get the keypad interfaced and the LCD.

How i want the keypad to work:
The data sheet for the keypad is here: https://www.jameco.com/Jameco/Products/ProdDS/169245.pdf
Keys A, B, C, D correspond to trains A, B, C, D. In DCC there is Addressing, and Functions. They keypad will be the Addressing input and be the function interface because the functions are on a on/off basis.
So, when you first need to interface with a train, you must push A, B, C, or D. Then, if you want to change it's address you must push # and its ID number. if you want to use a function you must push "star" and the function number.

How the LCD will work:
The LCD is the readout of all the trains. It must display its address, Speed, and what functions are on. When you push A, B, C, or D on the keypad, the LCD must display that Train right then. The display will not change unless there is no activity in the keypad for 10 seconds. However, during those 10 seconds, the speed and functions must continue to change as the user changes them.
The LCD is also the readout to the user. When you go to change the address, it must ask what you want to change it to, and as you enter in the new address, it must display what you have typed. (there must be a backspace & Enter) and it must ask you what function you want to change and what its statuses are.

Final Notes:
The largest address is 255
I will include a copy of the current program

DCC_Unit_1.ino (10.4 KB)

This is the LCD link, sorry forgot to include. http://www.bonanza.com/listings/HD44780-BASED-4X20-LCD-DISPLAY-WITH-EL-BACKLIGHT/6210927?gpid=21297750541&gpkwd=&goog_pla=1&as=score_threshold_2&gclid=CNKg3PKv6LMCFUxxQgodDCgA2w

tfoote:
A, B, C, D correspond to trains A, B, C, D.

and its ID number to change it's address

  • and the function number if you want to use a function
    (there must be a backspace & Enter)

Are the "#" and "*" going to double as both Address/Function and Backspace/Enter?

precicely.

In order to simulate multitasking you have to use non-blocking code... code that doesn't wait for input but reacts to new input as it is detected. To do that you need some variables to keep track of the current state:

Idle (displaying whatever you want when no specific train is selected)
Train display (displaying the data for a specified train when a letter has been pressed)
Train Address (gathering digits for an address change)
Train Function ID (gathering digits for a function ID)
Train Function Value (function specified, now need parameter for that function)

Within each state there will be other state variables, like the selected Train, the Address entered so-far, etc.

The LCD is updated periodically, say about 10 times per second. Best not to clear the screen and update everything every time since that tends to cause nasty flicker. See if you can clear the screen only on state changes and do the periodic updates only on the active parts of the screen.

At 5 to 10 bits per millisecond I don't expect your DCC messages will take long to send. You may not need to do those "in the background"

ok. Because when it stops transmiting, trains stop, so do you recomend that while i am updating addressing information & function info & LCD, that i just keep the function on? Because when i transmit it on and off, i have a booster taking my information and inverting the voltage. I have a square wave.

would it be easier/better if i used 2 different boards? one for the LCD and Keypad and one for the potentiometors and the Transmitions? This way we could be transmiting data and then once we complete a transmition, we ask for new data from the other board (lets call it board 2) so that board 1 can update its transmition?

tfoote:
Because when it stops transmiting, trains stop

How often do you have to repeat the sending of commands to keep the train from stopping?

well, when i send data, it is in packets. Once a packet is finished, i start sending a new one. it doesn't stop.

tfoote:
well, when i send data, it is in packets. Once a packet is finished, i start sending a new one. it doesn't stop.

Well that shouldn't stop you from still doing many independent sequential operations in your sketch. You don't need any kind of true multitasking, which is a overused term and does not even apply to microcontrollers. You just need to learn how to write correct program flow structure in C/C++, and learn how to avoid or work around any 'blocking functions' like the delay() function for example.

Lefty

If you have a lot of things that need to be done quasi-simultaneously, then a cooperative multitasker does make it easier. I have one for the Arduino at GitHub - dc42/arduino: Reusable modules, drivers and patches for the Arduino platform. The idea is that you write each task as a function that takes only a short time to execute, and returns an integer indicating how long it should be before the task should be executed again (if at all). I normally use a system tick rate of 1000Hz, so each task function needs to execute in at most a few hundred microseconds.

You need to be careful what library functions you call, because if you call one that blocks for a long time, it will stop other tasks getting executed on time. For example, if you use the LiquidCrystal library, don't call the clear() or home() functions, because they take a long time; also don't write more than about 10 characters in one execution of a task.