Adding analog input to the Pi using the Digispark
The Nitty-Gritty
Now that you know what the program does in general, I will go through it line by line so you can better understand its workings. On lines 21 and 22, you initialize a variable that stores the combination as it is entered by the user (v_combo
) and a buffer variable that holds the number last read from the USB channel. The infinite loop starts on line 24, and the first thing it does is check whether the Digispark is connected to the USB channel (line 26). Look at the idVendor
and idProduct
parameters used when initializing ArduinoUsbDevice()
, and then look back at the product identification that lsusb
returned earlier (ID 16c0:05df
). If the routine can't find the Digispark, it will print an error message and exit the program (lines 28-31).
Next, the routine tries to read in a series of characters from theDevice
(i.e., the Digispark plugged into the USB port). It does so by running the read()
function of ArduinoUsbDevice
over and over (line 36) until it reaches a newline character (\n
). Theoretically, then, you could read in chains of more than one character, but because Potentiometer.ino
is set up to return digits from 0 to 9, Combination.py
lops off and keeps the first character in the chain and discards the rest (line 41).
Note that the data is transmitted raw, meaning as bytes. That's why the input must be converted into characters using the Python chr()
function and why the numbers the program receives are not really numbers at all. You couldn't add them up, for example, because they are characters. Line 43 registers the time the character is received in start_time
, which you use later to calculate how long the user sits on a certain number when guessing the combination.
If nothing is received from the Digispark (lines 45-58), it can mean one of two things. First, it could mean the user hasn't started twiddling the dial yet, in which case start_time
will not have been set on line 43. The try
on line 48 fails because the variable on line 49 is undeclared. Execution then jumps to line 58, where the program sits and waits one-tenth of a second. This is how the routine sits and waits, ignoring all previous inputs from the USB channel, at the beginning of each new attempt to guess the combination.
The second thing that could be happening is the user has twiddled the dial and you already have a digit to check. In this case, start_time
will have been set (on line 43), so you want to see if the user has been waiting at least five seconds (lines 49 and 50). If so, the routine takes this to mean that the user wants the digit to be appended to the combination (line 51), and you signal that the operation has been successful by flashing the green LED twice (line 52). You need to reset start_time
(line 53), or the routine will fill all the slots of the combination with the chosen digit immediately. Line 55 checks to see that all the slots have been filled and, if so, breaks back to the main
function.
A Real Live Wire
The hardware setup in Fritzing looks nice and tidy (Figure 5). In real life, things aren't quite so neat (Figure 6). As you can see, the potentiometer is connected to the Digispark's 5V output, ground, and analog pin 1 (P2), as in the prior project. The red and green LEDs' anodes are connected to the Pi's 24 and 25 pins, respectively. Their cathodes are both connected to one of the Pi's ground pins.
Buy this article as PDF
Pages: 8
(incl. VAT)