Hardware Reference
In-Depth Information
45, the USB task function is run. This function either returns with an event or
times out after 5 seconds. There is no way of telling exactly how this function
ends, so the sketch looks at the contents of the variable curkeycode . If a valid
ASCII character is detected (a keycode between 97 and 122), then the sketch adds
that character to the end of the string. If the value 19 is received, then the sketch
has received a return key press, so a new line is printed, and sortSentence()
is called with the variable sentence , and the boolean variable is set to false ,
telling the loop that it is no longer expecting text input from a keyboard. If any
other value is received, it is simply ignored. These include special characters,
function keys, and control characters.
At the end of the while loop, the value of curkeycode is set to zero, an indica-
tion that the value has been read, and that the while loop expects a new value.
Without this, the while loop might interpret this information as a key press,
even if no key was pressed. Remember, the USB task function times out after
5 seconds, and then the rest of the sketch looks at the value of this variable. It
has to be reset at the end of the loop.
While there's no logic for parsing the text you've entered in this example,
sortSentence() is where you would write the code for i guring out the sequence
of events in your text adventure story. To run this example, once you've uploaded
the code to the Arduino, connect the keyboard to the Native USB port and your
computer to the Programming port. Open the serial monitor and start typing
away on the keyboard attached directly to the Due. You should see your words
come up in the serial monitor once you press the return key.
Summary
In this chapter you have seen how the Arduino Due can be controlled by a USB
keyboard and mouse. You have seen the functions used to get the status of
inputs and to receive movement information. You have created the beginning
of an interactive system allowing you to enter text to your Arduino. In the next
chapter, you will see the Arduino Esplora and the library that is used to pro-
gram this incredible device and use all the electronics present on this device.
 
Search WWH ::




Custom Search