RGB LCD Character Codes – Display Sketch

While working on projects using the RGB LCD shield, I’m finding it handy to see different character / color combinations on the screen.  I’ve written a sketch that allows me to page through all the available characters using the left and right buttons and all available backlight colors using the up and down buttons (this also works with the negative version of the RGB LCD shield).

LCD Character Code Sketch Screen Capture

All the characters on the bottom row share the same four high bits; the character representing those four bits is indicated in the top row by underlining (in the above picture, the number seven is underlined indicating that all characters in the bottom row have a character code starting with seven).  The characters are displayed below the digit representing the low four bits for the character code.  In the above picture, the left arrow is under the F character, so the code for left arrow is 0x7F.  Following the same logic, the code for q is 0x71.

There are a few areas in the code worth calling out.  The first is the function that treats a button press and release as a single click.  The shield library provides readButtons(), which is a handy function for reading the current state of the buttons.  You can detect clicks by calling readButtons() at regular intervals and performing some bitwise operations on the results.  The first step is performing a bitwise XOR (exclusive OR) on two consecutive reads to get a bitmask value showing which buttons changed state:

1100 XOR 1001 results in 0101

This value needs to be further filtered to find out which buttons were released on the second read.  Performing a bitwise negation on the second value produces a new value where the off buttons are ones and the on buttons are zeros.  This value can be bitwise ANDed with the changed button value to get a new value where clicked buttons are ones and all other buttons are zeros.

Which can be represented in code as follows:

uint8_t read_clicked_buttons() {
  // Static variables are intialized once and hold value between calls to the function.
  static uint8_t last_buttons = 0;

  // Read which buttons are currently on.
  uint8_t current_buttons = lcd.readButtons();

  // Calculate which buttons changed state since the last read.
  uint8_t changed_buttons = last_buttons ^ current_buttons;

  // Calculate which buttons are currently off.
  uint8_t off_buttons = ~current_buttons;

  // Calculate which buttons transitioned from on to off between reads.
  uint8_t clicked_buttons = changed_buttons & off_buttons;

  // Store current buttons for next call.
  last_buttons = current_buttons;

  return clicked_buttons;
}

The second interesting section is the underlining of the selected column.  This is implemented by manipulating the cursor settings as the last step when drawing the screen:

  lcd.noCursor();

  // draw screen

  lcd.setCursor(char_offset, 0);
  lcd.cursor();

Full source is available on github.


Adafruit publishes a wide range of writing and video content, including interviews and reporting on the maker market and the wider technology world. Our standards page is intended as a guide to best practices that Adafruit uses, as well as an outline of the ethical standards Adafruit aspires to. While Adafruit is not an independent journalistic institution, Adafruit strives to be a fair, informative, and positive voice within the community – check it out here: adafruit.com/editorialstandards

Stop breadboarding and soldering – start making immediately! Adafruit’s Circuit Playground is jam-packed with LEDs, sensors, buttons, alligator clip pads and more. Build projects with Circuit Playground in a few minutes with the drag-and-drop MakeCode programming site, learn computer science using the CS Discoveries class on code.org, jump into CircuitPython to learn Python and hardware together, TinyGO, or even use the Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for CircuitPython, MakeCode, and Arduino. It has a powerful processor, 10 NeoPixels, mini speaker, InfraRed receive and transmit, two buttons, a switch, 14 alligator clip pads, and lots of sensors: capacitive touch, IR proximity, temperature, light, motion and sound. A whole wide world of electronics and coding is waiting for you, and it fits in the palm of your hand.

Have an amazing project to share? The Electronics Show and Tell is every Wednesday at 7:30pm ET! To join, head over to YouTube and check out the show’s live chat and our Discord!

Join us every Wednesday night at 8pm ET for Ask an Engineer!

Join over 38,000+ makers on Adafruit’s Discord channels and be part of the community! http://adafru.it/discord

CircuitPython – The easiest way to program microcontrollers – CircuitPython.org


New Products – Adafruit Industries – Makers, hackers, artists, designers and engineers! — New Products 11/15/2024 Featuring Adafruit bq25185 USB / DC / Solar Charger with 3.3V Buck Board! (Video)

Python for Microcontrollers – Adafruit Daily — Select Python on Microcontrollers Newsletter: PyCon AU 2024 Talks, New Raspberry Pi Gear Available and More! #CircuitPython #Python #micropython @ThePSF @Raspberry_Pi

EYE on NPI – Adafruit Daily — EYE on NPI Maxim’s Himalaya uSLIC Step-Down Power Module #EyeOnNPI @maximintegrated @digikey

Adafruit IoT Monthly — Halloween, WiLo, and more!

Maker Business – Adafruit Daily — Checking in on Intel

Electronics – Adafruit Daily — Probe Compensation

Get the only spam-free daily newsletter about wearables, running a "maker business", electronic tips and more! Subscribe at AdafruitDaily.com !



No Comments

No comments yet.

Sorry, the comment form is closed at this time.