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, 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.

Figure 5: The Fritzing diagram of the safe project looks nice and tidy…
Figure 6: …In the the real world, the setup isn't as pretty.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Get your Pi to read analog data

    The Raspberry Pi still lacks analog GPIOs that would allow it to read directly from temperature and light sensors, or even humble potentiometers. With an inexpensive chip and some software-fu you can grant the Pi the gift of analog sensing.

  • A home intrusion detection setup (sort of)

    At least part of the popularity of the Raspberry Pi can be attributed to its high maker value; that is, a skilled maker with a Pi can build marvelous and beautiful things. Me? Not so much, but I was willing to try to build a home security system with the stuff in my junk box. Here's what happened …

  • A new way of no-solder prototyping

    The Grove system's standardized connector and multitude of devices allow quick and easy project prototyping with your favorite small-board computers.

  • Write your own drivers for Arduino

    So, you have some new kit for your Arduino – maybe some sensors or ICs – but programming them is clumsy and painful. Don't despair: You can make your life easier by writing your own drivers!

  • Use an analog sensor as a video game controller

    We put our Analog-to-Digital converter to work reading positions from an analog sensor (a potentiometer) and control a bat in a simple implementation of the classic Breakout game.