Interface & Application Programming
I set out this week to build a simple interface that could be driven off of input from my Hall Effect sensor board. I started by reading up on Processing - an open source software language that allows you to design interfaces and visual art. It's really quite cool and I'm looking forward to exploring it further in the future. This week, however I stuck to the extreme basics.
Here's what I made:
...a simple window that draws a rectangle of a specific size and colors it to match the color of the LED. I know... Super basic, but here's what I had to do to accomplish this.
I first had to figure out how to draw a rectangle on the screen. This turned out to be pretty straightforward as there are great built in examples, as well as ample online documentation. The following lines of code will draw a red rectangle that is 100x300 pixels big. Its upper-left-hand-corner will be offset 100 pixels in both directions from the upper-left-hand-corner of the Processing window.
Next, I had to figure out how to read the information that my board was sending via Serial communication. I found that I could establish connetion to the board by first importing a serial library, and then opening a port at a desired baud rate. I used 9600 as that is the rate at which my board is communicating.
I also found the "Serial.list" command to be crucial as it will list all of the COM ports you have on your computer and their corresponding port numbers. Without this information, you are just guessing which number to put in square brackets when you open the port.
With the port successfully opened, I could attempt to read the information my board was sending across the Serial bus. This was a matter of using a simple "read" command. Following that up with a "println" command produced an exciting list of numbers! The gauss values that my board was sending! My first success!
Filled with excitement, I tried to write logic that would change the color of my rectangle depending on the value I read. Writing the if-else statement was pretty straightforward, but when I ran my code, I got this error: "The operator > is undefined for the argument type(s) String, int"
Duh! I was trying to perform a math operation on a string. I thought I'd first look at the code on my board and see if I could send an integer across the serial bus, but I couldn't figure out how to do that. Instead, I found that I could convert a string to an integer using Processing. I added the "parseInt" command to perform the conversion. The error went away, but my rectangle still wouldn't change colors :(.
Thank goodness for forums, though! I found a discussion talking about this very problem. I guess it's common for a string that is passed via serial to have "white space" characters (leading or trailing spaces). Using the "tirm" command allowed me to clean up these spaces before passing the string to the integer convesrion. With this in place, the program executed! As I passed the magnet over the hall effect sensor, the rectangle would change color! Red when it detected the norht pole and green when it detected the south pole - exactly what the RGB LED on my board does!