mattyp
mattyp

Reputation: 25

When programming an Arduino in C, how do we write two characters next to each other?

I have the following code for my arduino, however the adafruit lcd display only prints the down arrow character and not the up arrow then the down arrow. (the loop function is empty so I didn't include it).

#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

#define UP_ARROW 0
byte up[] = {4, 14, 31, 4, 4, 4, 0, 0};
#define DOWN_ARROW 1
byte down[] = {0, 0, 4, 4, 4, 31, 14, 4};

void setup() {
  lcd.clear();
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.createChar(UP_ARROW, up);
  lcd.write(UP_ARROW);
  lcd.setCursor(1,0);
  lcd.createChar(DOWN_ARROW, down);
  lcd.write(DOWN_ARROW);
}

Upvotes: 0

Views: 744

Answers (3)

mattyp
mattyp

Reputation: 25

Turns out my problem is that creating a character resets the cursor position to (0,0), so when I set the cursor to (0,1) and create the down arrow, it resets the cursor to (0,0). My solution was to create the custom characters first and then set the cursor and write them.

Upvotes: 0

Tom
Tom

Reputation: 303

From the Arduino reference:

NB : When referencing custom character "0", if it is not in a variable, you need to cast it as a byte, otherwise the compiler throws an error. See the example below.

In your example, UP_ARROW will be replaced be 0 in lcd.write(UP_ARROW);

Maybe try: lcd.write(byte(UP_ARROW));

Hope it helps.

Upvotes: 0

Blindy
Blindy

Reputation: 67417

Based on your library's source code,

void Adafruit_RGBLCDShield::createChar(uint8_t location, uint8_t charmap[]) {
  location &= 0x7; // we only have 8 locations 0-7
  command(LCD_SETCGRAMADDR | (location << 3));
  for (int i=0; i<8; i++) {
    write(charmap[i]);
  }
  command(LCD_SETDDRAMADDR);  // unfortunately resets the location to 0,0
}

If I were you I would create the characters at the start of the program, then position the cursor and write them as needed.

Upvotes: 2

Related Questions