putting lcd functions inside your own library

hi
I'm trying to clean up my main sketch by putting stable functions into a library.
I get a test library to work with a test sketch but the second I put in any reference to printing to the lcd 1602 screen everything goes haywire and removing the line of code does not help.
I want to try again but not without advice.
kendrick

You won't get anything better than guesses unless you post your code.

A better description of 'haywire' would be in order as well.

Don

I have since made a simple sketch modified from a youtube video I watched.

I altered the sketch name and the library name and directories, I altered the reference names in the files so they match but were different from the original files.

the new sketch worked.

I then renamed the sketch and added the lines to use a LCD 1602 shield, modified the sketch to output to the shield this worked. I first wrote a few lines in the sketch that printed on the screen then a function that printed a different line to the shield. all of this worked.

#include <MyLibrary.h>
#include <LiquidCrystal.h>
MyLib mlib(true);

void printToScreen(long num); // I want to move this o a .h file

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


void setup() {

 lcd.begin(16, 2);  // LCD SHIELD WITH 16X02 SCREEN

  randomSeed(analogRead(A0));
lcd.clear();
lcd.setCursor(0,1);
  lcd.print("display started");
  delay(3000);
}

void loop() {
  // put your main code here, to run repeatedly:
  long rndNo = mlib.getRandomNumber();
  
 // print number on lcd screen
 printToScreen(rndNo);

  delay(2000);
}
// I want to move below to a .cpp file
void printToScreen(long num){ // print number on lcd screen
  lcd.setCursor(0,0);
  lcd.print(num);
  lcd.print("     ");  
}

the library files to go with this sketch are below

//MyLibrary.h
#ifndef ml 
#define ml 

#if (ARDUINO >=100)
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

class MyLib  {
  public:
    // Constructor 
    MyLib(bool displayMsg=false);

    // Methods
   void begin(int baudRate=9600);
  // select the pins used on the LCD panel
//	void begin (int a=8,int b= 9,int c= 4,int d= 5,int e= 6,int f= 7);
 //   void begin(int a=16,int b= 2);  // LCD SHIELD WITH 16X02 SCREEN
    long getRandomNumber();
//	void printToScreen(long num);
  private:
    bool _msg;
    float getPi();
};
#endif
//MyLibrary.cpp
#include "MyLibrary.h"

MyLib::MyLib(bool displayMsg) {
  // Anything you need when instantiating your object goes here
  _msg = displayMsg;
}

// this is our 'begin' function
void MyLib::begin(int baudRate) {
}

// Pretend this is one or more complex and involved functions you have written
long MyLib::getRandomNumber() {

  unsigned long specialNumber = random(5, 1000);

  specialNumber *= getPi();

  specialNumber -= 5;

  return specialNumber;

}


// Private method for this class
float MyLib::getPi() {
  return 3.1415926;
}

all of this code works together, the problems come when I try to move the function printToScreen to the library.
kendrick

I have three choices leave all functions that references the lcd display in the main sketch: doable but not neat, I could work out how to to include lcd functions in my library :best option and will teach me a lot or I could make pseudo lcd functions in my main sketch that call lcd functions :less messy but is a work around not a solve the problem.
kendrick

There are several ways to "add functionality"

  1. simple C / C++ helper function.
  2. extend a C++ class
  3. write a C macro

For something this trivial I would just choose (1)
Any sequence that is repeated several times in a project will benefit from a helper function.

If you want to add several useful methods to a class, you can simply extend it by inheriting the existing class.

The C++ linker is pretty good at discarding uncalled methods. Making a class too complex might mean punters just get overwhelmed. After all, they seldom understand simple class methods.

You can experiment with all 3 approaches in the regular IDE. e.g. by putting your experimental "super" class in separate H and CPP tabs. When debugged, you can copy it to your User libraries.

The Arduino INO parser can get confused by macros. It can also get confused by mutiple INO tabs.
If you use CPP files you are guaranteed to remove parsing problems. But obviously have to obey the C++ semantics properly e.g. forward declarations.

David.

hi
I have come up with a work around it's a cop out but it works for me, as i start to use more lcd functions so I will add to the list I have made, when I come across a better method I will change to it.
i know it is a lot of a cheat but it works.

//blank.ino
#include <LiquidCrystal.h>




void lcdprintint(int num);
void lcdprintfloat(float num ,int d=2);
void lcdprintlong(long num);
void lcdprinttxt(char text[ ]);
void lcdclear(void);
void lcdsetcursor(int col,int row);


LiquidCrystal lcd(8, 9, 4, 5, 6, 7);



void setup() {


// the 4 different types of lcd screen make live the one you want to use
 lcd.begin(16, 2);  // LCD SHIELD WITH 16X02 SCREEN
// lcd.begin(20, 4);  // LCD SHIELD WITH 20X04 SCREEN 
// lcd.begin(40, 2);  // LCD SHIELD WITH 16X02 SCREEN
// lcd.begin(16, 4);  // LCD SHIELD WITH 20X04 SCREEN 




}

void loop() {
 //test code deleat
  lcdclear();
  lcdprinttxt("hello world");
  delay (2000);
  lcdsetcursor(0,1);
 // lcdprintint(4);
 // lcdsetcursor(6,1);
  lcdprintfloat(3.14129);
  delay (2000);
  lcdclear();
  delay(2000);
 //end test code 
  

}

//lcd pseudo functions
void lcdprintint(int num){
lcd.print(num);	
}
void lcdprintfloat(float num,int d=2){
lcd.print(num,d);
}
void lcdprintlong(long num){
lcd.print(num);	
}
void lcdprinttxt(char text[ ]){
lcd.print(text);	
}

void lcdclear(void){
lcd.clear();	
}

void lcdsetcursor(int col,int row){
lcd.setCursor(col,row);	
}

this will at least minimise the code that I cannot put in a library.

kendrick

Go on. Sit down with a nice cup of tea. Study your C++ textbook.

Or read Wikipedia about "overloaded functions"

There is little point in creating a helper function that does nothing more than contain a single class method statement.

David.