Development

Off-The-Shelf Hacker: Use Processing’s Meter Library to Build Gauges

1 Jun 2019 6:00am, by

I’ve cobbled together several gauge interfaces using the Processing programming language. In one, I programmatically built the gauge elements from lines, arcs and text. In another project, I put an image of a gauge on the screen then rotated an image of the needle around a central pivot point. Each approach has its strengths and challenges.

Processing has a lot of cool libraries. Today we’ll explore a Processing library I found that makes very nice modern-looking gauges. It uses the programmatic gauge element building method and has a ton of parameters you can adjust to suit your needs. Just set a value and the gauge display changes. It’s much easier to use than my original scheme of adjusting sizes, colors and gauge behavior, down in the program code.

Install the Meter Library in Processing

I’m using Processing version 3.4 on both the Linux notebook and the Raspberry Pi 3, so you may want to upgrade to at least that level. The current version is 3.5.3.

Start Processing and travel to the Sketch tab, mouse over the Import Library menu item, then select Add Library from the top of the list. The Contribution Manager window will appear.

Type “meter” into the Search box and hit return. The meter library by Bill Kujawa should appear. Select it and press the Install button, at the lower right. Close the window after the installation completes.

The Processing library contribution manager screen showing the meter library

I started out with the “full circle meter” program in the examples section as the basis for my gauge. Click Files –> Examples –> Contributed Libraries –> Meter. In the list, select full-circle meter.

A Word about the Input Data

My tried and true method of getting data from a microcontroller over to the Processing gauge program on the Linux laptop or a Raspberry Pi is to go through the USB port. Arduinos, of course, can push data over the USB cable, as well use it for power.

I also used the thermocouple/amplifier rig to provide a data stream. We’ve used it in recent stories. The gizmo measures temperatures up to about 900 degrees Fahrenheit. I arbitrarily selected zero as the lowest value the gauge will display. We read “data” from the microcontroller, so you can start and end on any value you choose. The values could just as easily be displayed as a percentage, say from 0% to 100%, with slight changes to the Processing code. The gauge needle pointing to “Low,” “Medium,” or “High” might be applicable in some situations.

Let’s look at the code.

The Code

Once the library is installed it’s pretty much just pulling in the data, setting the display parameters and then calling the gauge display functions.

While the Processing code follows the normal initialize variables, “setup” and “draw” format there are a few areas that need a little extra attention.

“m” is the high-level variable for each meter to be displayed. Parameters are set with variables of the form “m.set[parameter name]”. For example, the title text that shows up at the top of the meter appears as “m.setTitle(“Thermocouple Temp”);” in the code. The variable names for the parameters weren’t intuitively obvious until I looked at the “SetAllValues” script. Here you’ll find the names and typical values for the parameter. Once you get the hang of working with the variables, it’s just a matter of familiarizing yourself with the possible values and seeing how it looks on the display. For the most part, I used default values and only put ones I changed in my code.

Something I added was the lines around the “catch” function. It seems that if you don’t include code that looks for the IOException situation, you’ll get an error during program execution. I first ran the program without the catch function and got the IOException error. Somehow, I remembered seeing the error handler in another example. Copied and pasted the code into my script… and the problem was solved.

Not being a full-time programmer, I rely on remembering seemingly random bits of strategic information, that might be useful in the future. It’s one of those “intuitive” skills that off-the-shelf hackers seem to develop over time. Don’t question why or even how we do it, just use it to get the job done. Make sure to capture the “insight” in your lab notes for future generations.

I also used the standard “map” Processing function to scale the input values to fit the 0 to 180-degree gauge face, from 0 to 1000. You will want to adjust this to whatever input data you are using from your sensor. Keep in mind that the Arduino code might do some data scaling before it gets to the Processing display code.

Lastly, notice that I included a few extra spaces in front of the “1000” in the scaleLabels variable. The label was right up against the thick black half-arc gauge graphic and looked cramped. The spaces gave it a consistent look with the rest of the labels.

Don’t forget to plug in the sensor and use the usual stty command, in the terminal, so the USB port is in the proper configuration, before running the Processing program. Otherwise, you might see the gauge but not have any data come in over the USB line.

Here’s a short clip of the gauge in action with the thermocouple input over USB.

The layout is very clean and efficient. These meters would work great for on-screen dashboards, particularly with multiple sensors.

What’s Next

I haven’t found very many meter or gauge libraries out there for Arduino and/or Processing sensor projects. Lots of people just send the sensor reading to a little LCD on their Arduino, I guess.

An area that might be interesting to explore would be implementing code on an ESP8266 that sends the sensor data over WiFi to the Processing gauge program on a networked Linux machine. An application might be standalone like some kind of Linux notebook to drone sensor, used out in the field. Or, we might have a wireless sensor out in a field or on top of a building that connects to the Processing gauge via a local network.

Catch Dr. Torq’s Off-The-Shelf Hacker column, each Saturday, only on The New Stack! Contact him directly for consulting, speaking appearances and commissioned projects at doc@drtorq.com or 407-718-3274.

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

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.