LCD library use - what am i doing wrong?

Hi Guys,

I am toying with the LCD library and a 16x2 LCD screen, but I am not having success ATM.

What I am trying to do is to have chords names displayed in various locations of the display (4 in the upper line, 4 in the lower line).

Here is the struct i take my names from:

struct chordName {int chord1; int chord2; int chord3; int chord4; int chord5; int chord6; int chord7; int chord8;} 
chordName [SCALES] = {
{'C', "Dm", "Em", 'F', 'G', "Am", "B°", 'C'},//Cmaj (Am)
{"Db", "Ebm", "Fm", "Gb", "Ab", "Bbm", "C°", "Db"},//Db (Bbm)
{'D', "Em", "F#m", 'G', 'A', "Bm", "C#°", 'D'},//D (Bm)
{"Eb", "Fm", "Gm", "Ab", "Bb", "Cm", "D°", "Eb"},//Eb (Cm)
{'E', "F#m", "G#m", 'A', 'B', "C#m", "D#°", 'E'},//E (C#m)
{'F', "Gm", "Am", "Bb", 'C', "Dm", "E°", 'F'},//F (Dm)
{"Gb", "Abm", "Bbm", "Cb", "Db", "Ebm", "F°", "Gb"},//Gb (Ebm)
{'G', "Am", "Bm", 'C', 'D', "Em", "F#°", 'G'},//G (Em)
{"Ab", "Bbm", "Cm", "Db", "Eb", "Fm", "G°", "Ab"},//Ab (Fm)
{'A', "Bm", "C#m", 'D', 'E', "F#m", "G#°", 'A'},//A (F#m)
{"Bb", "Cm", "Dm", "Eb", 'F', "Gm", "A°", "Bb"},//Bb (Gm)
{'B', "C#m", "D#m", 'E', "F#", "G#m", "A#°", 'B'}//B (G#m)
};

... and here is the "LCD" part of the code i wrote to place my chords:

lcd.clear();
      // set the cursor to column 0, line 0
      // (note: line 1 is the second row, since counting begins with 0):
      lcd.setCursor(0, 0);
      lcd.print(chordName[key].chord1);
      lcd.setCursor(0, 4);
      lcd.print(chordName[key].chord2);
      lcd.setCursor(0, 8);
      lcd.print(chordName[key].chord3);
      lcd.setCursor(0, 12);
      lcd.print(chordName[key].chord4);
      lcd.setCursor(1, 0);
      lcd.print(chordName[key].chord5);
      lcd.setCursor(1, 4);
      lcd.print(chordName[key].chord6);
      lcd.setCursor(1, 8);
      lcd.print(chordName[key].chord7);
      lcd.setCursor(1, 12);
      lcd.print(chordName[key].chord8);

The LCd is correctl connected ("Hello, World" example runs great). I have included the library and initialized the LCD screen; the only thing I can see are

CF (first line of the display) GC (second line of the display)

There must be something obvious I am missing ... any help would be much appreciated!!

What makes you think that e.g. "Dm" is an integer? A char pointer might be a better choice in your definition of the struct. In which case you also have to replace all single quotes by double quotes.

struct chordName {char *chord1; char *chord2; char *chord3; char *chord4; char *chord5; char *chord6; char *chord7; char *chord8;}
chordName [] = {
{"C", "Dm", "Em", "F", "G", "Am", "B°", "C"},//Cmaj (Am)
{"Db", "Ebm", "Fm", "Gb", "Ab", "Bbm", "C°", "Db"},//Db (Bbm)
{"D", "Em", "F#m", "G", "A", "Bm", "C#°", "D"},//D (Bm)
{"Eb", "Fm", "Gm", "Ab", "Bb", "Cm", "D°", "Eb"},//Eb (Cm)
{"E", "F#m", "G#m", "A", "B", "C#m", "D#°", "E"},//E (C#m)
{"F", "Gm", "Am", "Bb", "C", "Dm", "E°", "F"},//F (Dm)
{"Gb", "Abm", "Bbm", "Cb", "Db", "Ebm", "F°", "Gb"},//Gb (Ebm)
{"G", "Am", "Bm", "C", "D", "Em", "F#°", "G"},//G (Em)
{"Ab", "Bbm", "Cm", "Db", "Eb", "Fm", "G°", "Ab"},//Ab (Fm)
{"A", "Bm", "C#m", "D", "E", "F#m", "G#°", "A"},//A (F#m)
{"Bb", "Cm", "Dm", "Eb", "F", "Gm", "A°", "Bb"},//Bb (Gm)
{"B", "C#m", "D#m", "E", "F#", "G#m", "A#°", "B"}//B (G#m)
};

Why are you using chars and strings in the declaration of the struct instances, when the struct contains only ints?

There must be something obvious I am missing

The code does something. Only you can see what it actually does. What you are missing, besides proper values for the ints in the struct, or a proper definition of the struct, is an explanation of what actually happens.

Thank you guys for the help!

I used "chars" and not "int" in first place, and it did not work; then i planned a test with ints, but i had to wait to go home to test it (I am "working" now). I was defining chars without the "*", that was probably a first mistake.

PaulS, could you please elaborate your answer?

Why are you using chars and strings in the declaration of the struct instances, when the struct contains only ints?

(sorry for the noob mistakes and questions: i am not a programmer).

PaulS, could you please elaborate your answer?

Your code is, effectively, doing this:

   int chord1 = 'C';
   int chord2 = "Dm";

Does that seem reasonable?

No, it's not :blush:

Unfortunately even with the correct definition (char *) and the double quotes the code doesn't run as it "should"... any other idea?

I am at a dead end now :confused:

(thanks! ;) )

the code doesn't run as it "should"

The code you haven't posted does SOMETHING. You expect it to do SOMETHING. All that we now know is that the two SOMETHINGs are not the same thing.

Ok, no problem :slight_smile:

Here is a trimmed version of the code. Obviously I will release the full project (triads/seventh MIDI player and sample triggerer) as soon as it will be ready, as always.

What I am expecting it to to? It should display the 8 chords names of the first raw of the chordName struct (4 names on the display first row, 4 on the displaysecond row ).
What does it do so far? It shows only a couple of uppercase letters on the first row and a couple of upper case letters + some garbage on the second row.

// include the LCD library code:
#include <LiquidCrystal.h>

int key = 0;

struct chordName {char *chord1; char *chord2; char *chord3; char *chord4; char *chord5; char *chord6; char *chord7; char *chord8;} 
chordName [12] = {
{"C", "Dm", "Em", "F", "G", "Am", "B°", "C"},//Cmaj (Am)
{"Db", "Ebm", "Fm", "Gb", "Ab", "Bbm", "C°", "Db"},//Db (Bbm)
{"D", "Em", "F#m", "G", "A", "Bm", "C#°", "D"},//D (Bm)
{"Eb", "Fm", "Gm", "Ab", "Bb", "Cm", "D°", "Eb"},//Eb (Cm)
{"E", "F#m", "G#m", "A", "B", "C#m", "D#°", "E"},//E (C#m)
{"F", "Gm", "Am", "Bb", "C", "Dm", "E°", "F"},//F (Dm)
{"Gb", "Abm", "Bbm", "Cb", "Db", "Ebm", "F°", "Gb"},//Gb (Ebm)
{"G", "Am", "Bm", "C", "D", "Em", "F#°", "G"},//G (Em)
{"Ab", "Bbm", "Cm", "Db", "Eb", "Fm", "G°", "Ab"},//Ab (Fm)
{"A", "Bm", "C#m", "D", "E", "F#m", "G#°", "A"},//A (F#m)
{"Bb", "Cm", "Dm", "Eb", "F", "Gm", "A°", "Bb"},//Bb (Gm)
{"B", "C#m", "D#m", "E", "F#", "G#m", "A#°", "B"}//B (G#m)
};

// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
  
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.clear();
// set the cursor to column 0, line 0
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
lcd.print(chordName[key].chord1);
lcd.setCursor(0, 4);
lcd.print(chordName[key].chord2);
lcd.setCursor(0, 8);
lcd.print(chordName[key].chord3);
lcd.setCursor(0, 12);
lcd.print(chordName[key].chord4);
lcd.setCursor(1, 0);
lcd.print(chordName[key].chord5);
lcd.setCursor(1, 4);
lcd.print(chordName[key].chord6);
lcd.setCursor(1, 8);
lcd.print(chordName[key].chord7);
lcd.setCursor(1, 12);
lcd.print(chordName[key].chord8);

Serial1.begin(31250); //Baudrate for Midi 
//Serial.begin(9600); //Baudrate for Serial communication (Debugging)
}

void loop() {

}//loop close

Thank you for the help so far!

I'd suggest that you put the LCD in a box. Tape the box shut. Put a label on it, and send it to someone else for safe keeping. Use Serial.print() to test your struct initialization code instead of the LCD.

I can't really imagine what value there is in using a struct to emulate an array of pointers. I can't really see why a 2D array of strings isn't a better choice.

I will test via Serial.print if necessary. The LCD will stay on my desk ready :) Could you please show me how i could code that part with an array of strings? As i said, i am not a coder but i learn something new every time i post here ;)

Thanks!!

Could you please show me how i could code that part with an array of strings?

char *chordData[12][8] = {
{"C", "Dm", "Em", "F", "G", "Am", "B°", "C"},   //Cmaj (Am)
{"Db", "Ebm", "Fm", "Gb", "Ab", "Bbm", "C°", "Db"},   //Db (Bbm)
{"D", "Em", "F#m", "G", "A", "Bm", "C#°", "D"},   //D (Bm)
{"Eb", "Fm", "Gm", "Ab", "Bb", "Cm", "D°", "Eb"},   //Eb (Cm)
{"E", "F#m", "G#m", "A", "B", "C#m", "D#°", "E"},   //E (C#m)
{"F", "Gm", "Am", "Bb", "C", "Dm", "E°", "F"},   //F (Dm)
{"Gb", "Abm", "Bbm", "Cb", "Db", "Ebm", "F°", "Gb"},   //Gb (Ebm)
{"G", "Am", "Bm", "C", "D", "Em", "F#°", "G"},   //G (Em)
{"Ab", "Bbm", "Cm", "Db", "Eb", "Fm", "G°", "Ab"},   //Ab (Fm)
{"A", "Bm", "C#m", "D", "E", "F#m", "G#°", "A"},   //A (F#m)
{"Bb", "Cm", "Dm", "Eb", "F", "Gm", "A°", "Bb"},   //Bb (Gm)
{"B", "C#m", "D#m", "E", "F#", "G#m", "A#°", "B"}   //B (G#m)
};

Thankyou! you made my day! I had the feeling that those chord# could be better defined for future use in some "for" loop. Really, thanks.

About the LCD definitions: could it be that this line...

lcd.print(chordName[key].chord1);

now turned in...

lcd.print(chordName[key][0]);

... should be written like this:

lcd.print(*chordName[key][0]);

... to solve the issue? I will test it this evening.

(thanks)

lcd.print(chordName[key][0]);

Is correct to print “C”, when key is 0, assuming that you didn’t like chordData for the array name.

I can’t see, really, how {“C”, “Dm”, “Em”, “F”, “G”, “Am”, “B°”, “C”} is a chord name. YMMV.

chordData it will :slight_smile:

Thank you for the highlight

The API call to set the cursor is: setCursor(col,row) ;) https://www.arduino.cc/en/Reference/LiquidCrystalSetCursor

--- bill

BTW, you are likely to have issues with the degree character that is in some of your strings. Not sure how you entered it, but that character is outside of standard ascii so it is likely a unicode character. I'm not sure what happens to that when it gets compiled with avr-gcc, but it likely won't be what you want.

If you really need a degree symbol, many LCDs have one at code point 0xdf so to put it in a string you would use \xdf

i.e. "B\xdf" will be B with a degree symbol.

--- bill

You are correct Bill: I solved by replacing the degree simbol with a lower "o", but will check your suggestion this evening.

Good news: now the code works flawlessy! There was another, main error that made the display to show only the first two columns of characters: i had inverted the column and rows definition in setCursor! Dumb.

Thank you all for the help and patience :*