How to convert Int to String for LCD?

I have been looking around for the tutorials that I used to study. I am having a little trouble finding them and I don't understand the new ones. I apologize if my questions may seem to be beginner stuff. On Linux C programming for terminal based programs the "printf" item uses a conversion character to insert a variable int into a string. How is this done for the liquidcrystal.h library?

My code is currently as fallows

#include <MIDI.h>  // Add Midi Library
#include <LiquidCrystal.h>

//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();
// Create an LCD object. Parameters: (RS, E, D4, D5, D6, D7):
LiquidCrystal lcd = LiquidCrystal(2, 3, 4, 5, 6, 7);

void setup() {

  MIDI.begin(MIDI_CHANNEL_OMNI); // Initialize the Midi Library.
  // OMNI sets it to listen to all channels.. MIDI.begin(2) would set it 
  // to respond to notes on channel 2 only.
  MIDI.setHandleNoteOn(MyHandleNoteOn); // This is important!! This command
  // tells the Midi Library which function you want to call when a NOTE ON command
  // is received. In this case it's "MyHandleNoteOn".
  MIDI.setHandleNoteOff(MyHandleNoteOff); // This command tells the Midi Library 
  // to call "MyHandleNoteOff" when a NOTE OFF command is received.

  // Specify the LCD's number of columns and rows. Change to (20, 4) for a 20x4 LCD:
  lcd.begin(16, 2);
  // Set the cursor on the third column and the first row, counting starts at 0:
  lcd.setCursor(4, 0);
   // Print the string '16x2 LCD':
  lcd.print("16x2 LCD");
  // Set the cursor on the third column and the second row:
  lcd.setCursor(2, 1);
  // Print the string 'Hello World!':
  lcd.print("Hello World!");
}

void loop() { // Main loop
  MIDI.read(); // Continuously check if Midi data has been received.
}

// MyHandleNoteON is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.write("Channel is", " ", channel, "Pitch is", " ",  pitch, "Velocity is", " ", velocity" );
  
}

// MyHandleNoteOFF is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { 

}

I am mashing together NotesNVolts with example code for a traditional LCD on 4 data lines. I would like to use 8 bits instead of 4, but I think it would be better if I did this one step at a time. First off I would like a point in the correct direction for working with the variables Channel, Note, and Velocity. I will be setting this up with sample hold circiuts using the 742 operational amplifier. My idea is to use the onboard pulse width modulation pins to set a control voltage value. Then this can be patched to a voltage controlled oscilator and other synthesizer modules. This entire project is a work in progress and it will be a lot more complex then your raditional MIDI to CV module you would buy from say ummm.... Synthrotek.
Thanks for any help you guys can give. I will be sure to make all this into a tutorial for a simplistic design. The advanced polished design will be on normal Atmel chips without an arduino boot loader. This advanced version will be in my non-fiction book series. Rest assured the basic design will be available to anyone who would like to use the schematics and open source code for their own purposes.

You just use the same "overloaded" print() function. e.g.

    lcd.print("hello");
    lcd.print(123);
    lcd.print(456, HEX);
    lcd.print(789, BIN);
    lcd.print(123.456, 1);
    lcd.print('c');

Read your C++ textbook. re overloaded functions.

Note that many Arduino libraries inherit from the Print.h class. e.g. Serial

    Serial.print("hello");
    Serial.print(123);
    Serial.print(456, HEX);
    Serial.print(789, BIN);
    Serial.print('c');

Experiment with Serial first.

David.

Thanks for the point in the correct direction. When you say text book are you sayinga book on C++ or will something like TutorialsPoint? I have a book on C, but I might need to get an actual book on C++. Until then I will have to use free stuff on the net unless you know of a PDF I could download. I will be sure to update on this project when I have something going.

C has a separate name for each function. "Overloaded functions" are a feature of C++.

Arduino (C++) uses print() for simple untidy output.
In C you would use printf() to produce nicely formatted output.

You can use sprintf() to create nicely formatted text in a buffer.
Then use Serial.print(buffer) or lcd.print(buffer) to display it.

Formatted output with the Arduino overloaded print() is really painful.
You will find sprintf() much easier.

I suggest that you look for Arduino tutorials. And experiment e.g. with Serial.

David.

If you are using an Arduino board that uses a 3rd party platform core like teensy, chipkit, esp8266, esp32 and a few others, then a printf() method is part of the Print class.
This would allow to you to do xxprintf style formating in your code like this:

lcd.printf("this is a number: %d", value);

This is not available on the boards that use the Arduino platforms from Arduino.cc like the AVR, sam and samd (boards like UNO, mega2560, DUE, etc...) as the Arduino.cc developers have refused to add support for it.

If you want something easy that can add to your sketches to offer printf() style output,
take a look at this sample sketch I wrote.
It offers a function called Pprintf() that works kind of like fprintf() but instead of passing FILE pointer you pass in a Print class object.
This allows outputting formatted text on devices that use the Arduino Print class, which is any device that supports the print() functions.
It is only a few lines of code (just below the loop() code) and can be easily added to your sketch.

See the code below for an example of how to use it.
To use it on your lcd, simply use the name of your object which is "lcd" instead of "Serial".

--- bill

void setup(void)
{
 Serial.begin(115200);
 Pprintf(Serial, "Pprintf...\n");
}
void loop(void)
{
 Pprintf(Serial, "Seconds up: %04ld\n", millis()/1000);
 delay(1000);
}

#ifndef PPRINTF_BUFSIZE
#define PPRINTF_BUFSIZE 64
#endif
size_t Pprintf(Print &outdev, const char *format, ...)
{
char buf[PPRINTF_BUFSIZE];
 va_list ap;
 va_start(ap, format);
 vsnprintf(buf, sizeof(buf), format, ap);
 va_end(ap);
 return(outdev.write(buf));
}

This topic was automatically closed after 76 days. New replies are no longer allowed.