Culture / Technology / Tutorials / /

Off-The-Shelf-Hacker: Button Pressing with the Processing Language

12 Dec 2015 9:55am, by

In the “Off The Shelf Hacker: Run a Slide Deck from the Raspberry Pi” article, a couple of weeks ago, I talked about using buttons with a Python script to change slides on my Raspberry Pi powered “Conference Presentation and Manipulation Apparatus.”

Today, we’ll look at using input buttons to change the color of a box on the screen, this time with the Processing Language. Applications might include hooking switches up to a machine and building an industrial process dashboard panel. Or, how about a home security system with inputs from door switches you can watch from your easy chair? The Pi and Processing offer a lot of interesting possibilities.

The Processing language is geared for artists and creatives who want to get visual projects going without a steep learning curve or a big hassle. The code is identical to the code you’d write when programming an Arduino. Libraries and specific functions, predictably are slightly different, while the code is the same. I think using standard tools like Processing and the Arduino IDE make it easy to get things done as an off-the-shelf-hacker.

Get The Hardware

The hardware consists of a Raspberry Pi 2, the 40-pin connector and cable from last time and a “clicker” I built for my Fossetcon talk. Of course, you’ll also need a big-screen monitor, keyboard/mousepad and a network connection, as the example will run on the Raspbian Linux desktop.

Graphic #1 - Steampunk "clicker"

Graphic #1 – Steampunk “clicker”

Graphic #1 shows the details of the clicker. I purchased a couple of industrial-looking push button assemblies from Skycraft, a long-time, local surplus electronics store. Paid a dollar a piece.

I attached the buttons to an old brass colored door striker plate, that was in my “junk box”, using the existing screw holes. You can save a lot of time and utilize considerable creative license, designing Steampunk-themed gadgets, when you have a well-stocked junk box.

A couple of #6-32 x 1/2″ machine screws and nuts fastened a flattened 4-inch long piece of 1” diameter copper pipe to the door striker/push button plate and acted as a handle. A 10-foot length of CAT 5 cable was threaded through the handle and used two pairs of wires to connect the buttons to the 40-pin Pi connector.

I like to go into the hardware details, as much as possible, because I think it’s important for TheNewStack OTSHs to be knowledgeable not only in the software and computer aspects of physical computing, as well as the mechanical and electrical aspects.

Change The Box Color With A Clicker

Like most other physical computing programs, we want to read some inputs, maybe do a few calculations and set some outputs.

In this case, we’ll have a loop that constantly reads the state of the two clicker buttons and when it detects a push, changes the color of a square on the screen.

Refer to the recent “Off-The-Shelf-Hacker: Enrich Your Project with a Processing Sketchbook” for help on installing Processing on your Raspberry Pi. You’ll want to download the Linux ARMv6hf (Raspberry Pi) version instead of the Linux 64-bit (Intel architecture) version that I used on my Linux notebook. The installation process is the same on both platforms.

I still had to run the Processing integrated development environment (IDE) using sudo, for some reason. Processing 3 is supposed to give you access to the GPIO pins without permission concerns, so I’ll have to investigate why it didn’t work correctly.

Switch to the Processing 3 directory and execute the application.

pi% cd processing3  
pi% sudo ./processing 

Here’s the code I used to detect button presses with my “clicker”.

void setup() {

 // On the Raspberry Pi, GPIO 4 is pin 7 on the pin header,
 // located on the fourth row, above one of the ground pins
 // For this particular board one could also write:

 // frameRate(0.1);

void draw() {
 // sense the input pin
 if (GPIO.digitalRead(21) == GPIO.HIGH) {
 //println("right button pressed");
 // delay(200);
 if (GPIO.digitalRead(20) == GPIO.HIGH) {
 // println("left button pressed");
 // delay(200);

 rect(30, 20, 55, 55);

The program isn’t very complicated. Basically, the GPIO pins are initialized and then a pair of if statements check for when a pin goes HIGH (pressed). I commented out the print and delay statements because they were mostly used for troubleshooting.

Run the application by hitting the play button (arrow at the top left in the editor) and after a short time a new window with a square should appear on the screen. Press each button and the color of the box should change.

The response was very good with no lag or issues. There’s been some chatter on the Internet that Processing 3 on the Pi is slow and clunky. I found that the program is fast enough to detect switch bounce when you push a button. Uncommenting the println statements will give you three or four lines of text for each press. Uncomment the delay() statement and perhaps increase it to 250 ms to eliminate the duplicate lines.

Also, don’t forget that Processing has a huge user community, who have generated some awesome libraries. ControlP5, in particular, has a rich set of screen gauges, buttons, sliders and so on, that are perfect for building desktop dashboards that track inputs.

You might also want to investigate running your sketch from the command line. Check out this Adafruit article. Take a look at page 9.

Wrap Up

Detecting on/off inputs with the Raspberry Pi is pretty straightforward. We’ve seen how to do the job in both Python and Processing. Your projects might use push buttons, magnetic reed switches, an optical sensor or other on/off sensors. Being able to show a change on the screen gives you the ability to build desktop dashboards. Take a look at the Web site for additional ideas.

A digest of the week’s most important stories & analyses.

View / Add Comments