John L Errington MSc

Experiments with an Arduino: keypress simulator (conclusion)

At its present stage of development the keypress simulator offers 6 functions, chosen by the rotary switch.

  1. measure usb supply voltage
  2. set character rate
  3. launch and test notepad
  4. send keyboard characters to PC
  5. spare (flash led)
  6. spare (led off)

key-sim in operationKeyboard simulator in operation

interior viewView of inside of unit

Multi-tasking with an Algorithmic State Machine (ASM)

In the final program every section that takes more than a few milliseconds is written as an ASM.

Instead of using the delay function to time events we ask "is it time to do this". If it is NOT time the routine returns control to the calling program, so the Arduino is not "tied up". Here is the pseudocode for the routine that launches notepad and prints a message.

State 1: "INIT": Record time and set up: Record status for next stage. Break.

State 2: "LAUNCH": If its time, Press keys to launch a "RUN" window. Record status for next stage. Break.

State 3: "DELAY": If its time, release keys. Record status for next stage. Break.

State 4: "NOTEPAD": If it's time, enter "notepad.exe" and a return to RUN box to launch Notepad.
Record status for next stage. Break.

State 5: "PRINTMSG": Allow time for Notepad to launch and settle. Then if it's time,
print message to the Notepad window. Record status for next stage. Break.

State 6: "END": If it's time, clean up. Record status (INIT) for next stage, to allow routine to repeat if called. Break.

 

This sequence fits very well into a Switch - case structure like this:

enum stateKSM {INIT, LAUNCH, DELAY, NOTEPAD, PRINTMSG, END};

switch ( stateKSM )
{
case INIT:
timeKSM = timeNow;
stateKSM = LAUNCH;
break;

case LAUNCH:
if ( timeNow - timeKSM >= 20 )
{
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('r');
timeKSM = timeNow;
stateKSM = DELAY;
}
break;

...

} //switch

 

complete sketch for keypress simulator