LCD Tests

Received the necessary wires to connect the 16×2 LCD display, so I decided to tackle that project first before attempting anything with 5V DIN Sync. Due to my problems reading data back to the computer via USB while using the MIDI Shield, I figured I needed a different way to visualize the internal data. I also needed to know if I would be running out of pins trying to hook this all up.

Most LCDs have a sort of parallel interface where you have to talk to 8 pins all at once. While the Arduino Mega 2560 has something like 54 digital I/O pins, I didn’t want all that wire-clutter to deal with, so I got an LCD module from DFRobot that was mounted on an I2C board. This board converts I2C signals into the 8 pins the LCD needs. Nice.

This was my first time experimenting with I2C which is a communication protocol that is not directly supported by the Arduino–you have to add the Wire library to your code in order to add I2C functionality. What I also discovered is that the I2C pins are different between the standard Arduino Uno and the Arduino Mega 2560. This difference, however, is good because the I2C pins aren’t connected to the MIDI Shield at all, so there are no electrical conflicts connecting both the LCD and MIDI Shield. Whew!

All it takes to connect this LCD module are +5VDC, Ground, SDA, and SCL wires. The first two simply provide power to the module. All the communication between the Arduino and LCD is handled by the SDA (Serial Data) and SCL (Serial Clock) wires. Quite convenient.

My first attempt at the code had me adding the Wire library and trying to address the LCD with basic commands from the Wire library. No success, but I didn’t try long. I was more concerned with the fact that the LCD was just showing one line of full pixels and nothing else…I was concerned it may be broken.

So I decided to jump right in and also add the LiquidCrystal_I2C library to my project which was intended for displays of this configuration. It sort of annoys me that I have to add both libraries to support the LCD, but this Arduino Mega 2560 has tons of Flash RAM to hold big programs…I’m only using 4,232 bytes out of 258,048 bytes at the moment.

Upon using the LiquidCrystal_I2C library functions, I got text immediately on the screen. Only the top line shows anything at this point, so I now need to look through the library for a method to write to the second line–I need to verify that both lines work.

Oh yes indeed...

OK–I did it the cheap way: I loaded an example program which shows characters on two lines. That quickly verified the proper operation. But it also revealed that I can actually reprogram the characters dot-by-dot to create my own icons! How fucking awesome is that?!?! I had no idea that would be possible…that’s a total bonus. The only bummer is that I can see that the actual resolution of each character on the display is 8 pixels tall by 5 pixels wide, but this controller only seems to be able to address the first 7 lines. Characters that should be extending down into the 8th line, like a lower-case “y” or a comma, are not doing so. They aren’t being cut off–the font has simply been designed to keep all characters in the first 7 lines. And it doesn’t look like custom characters can write in the 8th line, either. :-(

 

Next project: Display knob values on the screen in real-time. Here we go…

…and done! So this one wasn’t too hard, either. I learned about lcd.setCursor() and lcd.print() to get stuff on the screen. I was able to print a header in the first line (“KN1 KN2 Buttons”) with the actual values on the second line.

Some bummers: Obviously, numbers printed on this screen are left-aligned rather than right-aligned. I would prefer right-aligned so I may have to come up with a creative way to do that using if-statements to inspect the value to be displayed. If it’s  9 or less, then I need to shift the cursor two characters to the right before I print. If the number is between 10-99, then I only need to shift one character to the right before printing. 101-127 require no shift.

So let’s do it! I’m going to build this right-aligned display into a function that I can reuse.

Scanning the positions of two knobs with right-aligned values on the LCD.

My first attempt worked, but I’m not feeling that it’s as elegantly-coded as I’d like. I had a bunch of else-if-statements to determine the length, but I feel there’s got to be a better way to do it. Is there a “getLength” sort of function that tells you the number of characters in a variable? If so, that’s what I need to use rather than this else-if mess. I also need to build something that clears the leading digits when the number gets shorter (right now, the old digits are left on the screen).

Before I do this, though, I’m now realizing that I probably need a Knob class that can read and write and display it’s label and data at a point I specify. The Knob class should probably read the analog value, too, and return it. Yeah, that sounds good. Now, where to start? Classes are one of those things I never fully grokked from previous programming experience. Let’s see if I can figure this out…

…and I’m done for the night. Did not create a functioning class yet, though I did at least define its variables, structure, and methods. I believe it’s all just a matter of syntax at this point, a matter the compiler doesn’t take lightly.

Share