Programming Arduino Getting Started with Sketches



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə43/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   39   40   41   42   43   44   45   46   ...   78
Arduino SimonMonk 2011

Serial.available()
. This function returns the number of bytes of data in the buffer that are
waiting for you to read. If there are no messages waiting to be read, then the function
returns 0. This is why the 
if
statement checks to see that there are more than zero bytes
available to read, and if they are, then the first thing that the statement does is read the
next available 
char
, using the function 
Serial.read()
. This function gets assigned to the
local variable 
ch
.
Next is another 
if
to decide what kind of thing it is that you want to flash:
At first, this might seem a bit strange. You are using 
<=
and 
>=
to compare characters.
You can do that because each character is actually represented by a number (its ASCII
code). So, if the code for the character is somewhere between 
a
and 
z
(97 and 122), then
you know that the character that has come from the computer is a lowercase letter. You
then call a function that you have not written yet called 
flashSequence
, to which you will
pass a string of dots and dashes; for example, to flash 
a
, you would pass it 
.-
as its
argument.
You are devolving responsibility to this function for actually doing the flashing. You are
not trying to do it inside the 
loop
. This lets us keep the code easy to read.
Here is the C that determines the string of dashes and dots that you need to send to the
flashSequence
function:
Once again, this looks a little strange. The function appears to be subtracting one
character from another. This is actually a perfectly reasonable thing to do, because the
function is actually subtracting the ASCII values.
Remember that you are storing the codes for the letters in an array. So the first element
of the array contains a string of dashes and dots for the letter 
A
, the second element
includes the dots and dashes for 
B
, and so on. So you need to find the right position in the
array for the letter that you have just fetched from the buffer. The position for any
lowercase letter will be the character code for the letter minus the character code for 
a
. So,
for example, 
a
– 
a
is actually 97 – 97 = 0. Similarly, 
c
– 
a
is actually 99 – 97 = 2. So, in


the following statement, if ch 
is
the letter c
,
then the bit inside the square brackets would
evaluate to 2, and you would get element 2 from the array, which is -.-:
What this section has just described is concerned with lowercase letters. You also have
to deal with uppercase letters and numbers. These are both handled in a similar manner.

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   39   40   41   42   43   44   45   46   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin