Snake Sanders
Snake Sanders

Reputation: 2689

How to include an already written library to a custom library in Arduino

I'm creating a new library to control the keypad and LCD together. Most of the code seems to compile, but when it reaches the line where I define the LiquidCristal variable, it says:

'LiquidCrystal' does not name a type when creating a custom library

This is the extract of content of my LCDKeypad.h

// Include types & constants of Wiring core API
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "WConstants.h"
#endif

// Include LCD library
#include <LiquidCrystal.h>

The error is in this line:

private:
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); // <<-- Error

Upvotes: 5

Views: 5274

Answers (2)

AMPS
AMPS

Reputation: 309

There are two ways of doing it

  • If you are writing your own code Just create header file .h extension and relevant c code as name_c.While adding into main program, you need to specify header file name in double quotes.

code:

#ifndef LCD_H
#define LCD_H

//Declaration of variable /functions
#endif
  • If you are trying to download from link. Then you need paste the code into D:\arduino\arduino\libraries

Error problem:

  • overlapping of multiple declaration of variable.

  • overlapping of library functions

Upvotes: 1

Snake Sanders
Snake Sanders

Reputation: 2689

Alright, I was reading a lot and I found a very interesting article about this subject:

Including multiple libraries

It says that the compiler does not search for libraries that are not included in the sketch file. The way to hack this is to force the compiler to link them before loading your libraries, including, in my case LiquidCrystal.h in the sketch.

Let's say my library "LCDkeypad" requires LiquidCrystal. My main program (sketch) needs to include LiquidCrystal in order to load it for my library "LCDKeypad".

Now, one interesting thing is using forward declaration, so in my LCDKeypad.h I will declare "Class LiquidCrystal" but not include the library. I will do it in LiquidCrystal.cpp and in the sketch. I hope this is clear.

Upvotes: 5

Related Questions