Well, this is not a surprise. I have been really happy these past days playing with my Arduino Mega and working on the code for my Pedagogical and Phantasmagorical Inamorata Prognostication Engine. I even enjoy wrestling with bugs in my programs—that "Ah Ha!" moment when you realise why things aren't working just the way you expected.
Thus far, I've been using the IDE's default Serial.Print() commands to debug my software. These transmit values to a sort of terminal window on my host PC. The whole serial interface can be very useful—especially the fact that you can use it to transmit commands and data values from the PC to the Arduino whilst one's program is running in the Arduino. On the other hand, it is a tad "clunky," and using only this facility to debug one's code can quickly become a little wearing on the nerves.
In order to address this issue, I invested in an RGB LCD Shield Kit from Adafruit.com. This is really rather tasty in that you can change the background colour under program control. Even though there are only two rows of 16 characters, it's amazing how useful this little scamp is proving to be.
Of particular interest are the five buttons on the bottom left-hand side (Up, Down, Left, Right, and Select). You can read the state of these buttons from your (they are automatically de-bounced by the Adafruit library) and use them to implement a simple menu system, for example.
This RGB LCD Shield is presented as a kit, but it takes only a few minutes to solder everything together (this would make a great father-son project). Everything is controlled using the I2C bus, which means you use only the two I2C pins on your Arduino. (This display uses I2C address 0x20, and as far as I know you can't change this, so you have to make sure that no other devices on the I2C bus are set to this address.)
As usual, the example code and tutorials from Adafruit.com quickly get you up and running. Once you've instantiated your LCD display, you can use commands like the following:
lcd.setBacklight(WHITE);
lcd.setCursor(0,0); // Set cursor to Column 0, Row 0
lcd.print("Hello World");
Once you've used the setCursor() command, the display keeps track of what you write to it and the corresponding cursor position. For example, if you have three integers called dayNow, monthNow, and YearNow that you've already loaded with valid values, then you might use the following commands:
lcd.setCursor(0,0); // Set cursor to Column 0, Row 0
lcd.print("Date=");
lcd.print(dayNow);
lcd.print("/");
lcd.print(monthNow);
lcd.print("/");
lcd.print(yearNow);
In this case, the result on the display would look something like the following:
Date=1/14/2014
Similarly, if you have already declared an integer variable called "buttons," you can use the following command to read the values on the shield's buttons (Up, Down, Left, Right, and Select):
buttons = lcd.readButtons();
You can then use statements like "if (buttons & BUTTON_UP)" to determine if an individual button has been pressed. One thing that caught me out is that the readButtons() function only reads the current state of the buttons at the time the command is executed. I had originally expected the buttons to be latched, so I was puzzled when I briefly clicked a button and nothing happened (my readButtons() call is buried towards the end of quite a long loop). It took me quite a bit of debugging to realise that all I had to do was hold a button down until my program got around to reading it.
It would be great if you could instruct the shield to either latch any button presses (and auto-clear them when you read the state of the buttons) or to respond with the unlatched values. But you can't have everything, and once you know how things function you can easily work around them.
The RGB LCD shield is intended for stacking on an Arduino Uno. The I2C pins are in a different place on an Arduino Mega, so the easiest solution is to use these boards side-by-side and connect them using wires. My prototyping setup is shown in the image below (I'm sorry about the poor quality of this image, which was taken with my iPad); my Arduino Mega is on the left and my RGB LCD display is on the right:
This is great. The code for my Inamorata Prognostication Engine involves all sorts of things, like reading the current date from a real time clock (RTC) and using this to calculate the dates of the next Full Moon and the next Blue Moon. Using the RGB LCD shield, I can easily scroll up and down displaying whatever values I wish. This is going to make debugging my code much easier, which is wonderful because my code contains so many bugs (LOL).
Have you played with this display or with any other display? Do you use your display for debugging purposes? Are there any tips and tricks you would care to share? If so, please post them in the comments below.
文章评论(0条评论)
登录后参与讨论