0

I'm using the LCD library and some custom characters on an Arduino.

The line

lcd.print(char(0) + char(1) + char(6) + char(1) + char(1) + char(2) + ' ' + char(0) + char(1) + char(1) + ' ' + char(0) + char(1) + char(1) + char(2) + ' ' + char(0) + char(1) + char(1) + char(2));

should display a series of custom characters, but instead just shows

117

What have I done wrong. I'm fairly new to C/C++ but have used C# a lot.

radders
  • 923
  • 8
  • 29
  • You *do* know that `1 != '1'` (with or without casting)? What are you actually trying to print? – Some programmer dude Sep 11 '17 at 11:11
  • 1
    Even so, that's not how you concatenate characters in C++. With Arduino (or C++ in general) you can't actually concatenate single characters, you need a *string*. Or rather, a string *object*, which in Arduino is an instance of [the `String` class](https://www.arduino.cc/en/Reference/StringObject). – Some programmer dude Sep 11 '17 at 11:15
  • If you want to learn C++ in general, I recommend you find [a good beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read. – Some programmer dude Sep 11 '17 at 11:16
  • As lcd.print() accepts a string, I thought I was creating a string of concatenated single chars.... Obviously not. – radders Sep 11 '17 at 11:19
  • What you're doing is basically just adding integers together, and then printing the result of the additions. Remember that this is C++, where you have function *overloads*. There is an overload taking a `String` object as argument yes, but there are also overloads that takes numbers (e.g. `float`or (in this specific case) `int`). – Some programmer dude Sep 11 '17 at 11:21
  • If I use lcd.print(char(0)) it works - it is trying to send lots of chars which is causing the problem. When I tried to use String(char(0) + char(1)) I ended up with an empty string... because char(0) terminates a string. I don't really want to have to call lcd.print() [or lcd.write()] lots of times, but will if it is the only way. – radders Sep 11 '17 at 11:34

1 Answers1

0

Ok, I solved this by dint of creating an array of chars, and passing that to a function which calls lcd.write() in a loop using each element of the array. Not ideal, but it works.

radders
  • 923
  • 8
  • 29