Arduino Forum

Using Arduino => Programming Questions => Topic started by: hblair on Feb 15, 2018, 03:49 pm

Title: I2C_Keypad problem
Post by: hblair on Feb 15, 2018, 03:49 pm
I have been trying to get a I2C_Keypad sketch to compile, but keep getting the same error message.

Arduino: 1.8.3 Hourly Build 2017/07/25 11:12 (Mac OS X), Board: "Arduino/Genuino Uno"

/Users/herbblair/Documents/Arduino/HelloKeypad_I2C/HelloKeypad_I2C.ino:16:24: fatal error: Keypad_I2C.h: No such file or directory
 #include <Keypad_I2C.h>
                        ^
compilation terminated.
exit status 1

I have the .h file in the same folder as the sketch, along with the CPP file.

Any help would be appreciated.
Title: Re: I2C_Keypad problem
Post by: jbellavance on Feb 15, 2018, 03:50 pm
Try this:
Code: [Select]
#include "Keypad_I2C.h"

Jacques
Title: Re: I2C_Keypad problem
Post by: hblair on Feb 15, 2018, 04:12 pm
I have that in the sketch.

#include <Keypad_I2C.h>
#include <Keypad.h>
#include <Wire.h>
#define I2CADDR 0x27

Title: Re: I2C_Keypad problem
Post by: oqibidipo on Feb 15, 2018, 04:16 pm
No, you don't.

Change <Keypad_I2C.h> to "Keypad_I2C.h".
Title: Re: I2C_Keypad problem
Post by: jbellavance on Feb 15, 2018, 04:17 pm
Quote
I have the .h file in the same folder as the sketch, along with the CPP file.
That is why I suggested the quotes instead of the brackets.
Title: Re: I2C_Keypad problem
Post by: GolamMostafa on Feb 15, 2018, 05:00 pm
Why should the header file be within double quotes and not within angle brackets?
Title: Re: I2C_Keypad problem
Post by: jbellavance on Feb 15, 2018, 05:04 pm
Because you
Quote
have the .h file in the same folder as the sketch, along with the CPP file.
Title: Re: I2C_Keypad problem
Post by: GolamMostafa on Feb 15, 2018, 05:30 pm
When other two header files (keypad.h and Wire.h) work well being within angle brackets, the problem is with the keypad_I2C.h file.

Now, is there anything that the OP can do so that all these three header files work in an consistent way being within angle brackets?
Title: Re: I2C_Keypad problem
Post by: jbellavance on Feb 15, 2018, 05:49 pm
Quote
The use of angle brackets (<>) causes the compiler to look for the file on the default path for the library directory. Depending upon installation, this path is the Documents/Arduino/libraries directory on a Windows machine.

The use of double quotes ("") around the header file causes the compiler to look in the sketch working directory for the header file. The default sketch directory can be seen in the File --> Preferences menu option. If the file is not found there, it will also search the default library directory.
Title: Re: I2C_Keypad problem
Post by: GolamMostafa on Feb 15, 2018, 05:59 pm
Thanks with +1 for well clarification of the difference between keeping the header file within double quotes and angle brackets. 
Title: Re: I2C_Keypad problem
Post by: pert on Feb 15, 2018, 06:43 pm
The use of angle brackets (<>) causes the compiler to look for the file on the default path for the library directory. Depending upon installation, this path is the Documents/Arduino/libraries directory on a Windows machine.

The use of double quotes ("") around the header file causes the compiler to look in the sketch working directory for the header file. The default sketch directory can be seen in the File --> Preferences menu option. If the file is not found there, it will also search the default library directory.
Not a very good explanation. Where did you find that?

Quote
The use of angle brackets (<>) causes the compiler to look for the file on the default path for the library directory.
Actually there is a whole list of directories that are searched for the file:


Quote
Depending upon installation, this path is the Documents/Arduino/libraries directory on a Windows machine.
It might happen to be there but it's much more correct to say the libraries subfolder of the sketchbook folder.

Quote
causes the compiler to look in the sketch working directory for the header file.
It searches in the folder containing the file that contains the include statement. That file may or may not be a sketch.

Quote
The default sketch directory can be seen in the File --> Preferences menu option.
Which is completely irrelevant and also it's called the sketchbook directory.
Title: Re: I2C_Keypad problem
Post by: jbellavance on Feb 15, 2018, 10:45 pm
Quote
Where did you find that?
Found it here (https://forum.arduino.cc/index.php?topic=378506.new#new) from econJack

Thanks for making it cristal clear, Pert.

Jacques
Title: Re: I2C_Keypad problem
Post by: pert on Feb 16, 2018, 12:32 am
OK, that makes sense for a quick forum reply. I thought it might have been quoted from somewhere in the official documentation, in which case it would be worth improving.
Title: Re: I2C_Keypad problem
Post by: GolamMostafa on Feb 16, 2018, 06:33 am
I have executed a small program to monitor temperature using internal sensor of DS3231RTC and to display it using I2C 16x2 LCD. I may include the following two header files in any combinations of double quotes and angle brackets; the program always works.

LiquidCrystal_I2C.h
Wire.h

When I searched my PC, I found the these header files under the directories as indicated.
LiquidCrystal_I2C.h : Could not trace the directory!?
Wire.h : c:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire\src\Wire.h

My question: Is it really important for a programmer to care about the use of double quotes or angle brackets while including the header files? Traditionally, we are accustomed in the use of angle brackets.
Title: Re: I2C_Keypad problem
Post by: pert on Feb 16, 2018, 07:07 am
If you have those files installed to a standard libraries folder then you can use either syntax because both cause the standard libraries folders to be searched. However, if you know the file is not in the local folder then using the quotes syntax will perhaps slow compilation a bit as an extra folder is unnecessarily searched. Conversely if you use the angle brackets syntax to try to include a file that is not in the standard libraries search path then it won't work (as hblair discovered).

So if you wanted to be safe you could always use the quotes syntax. This would only be problematic in the rare case where there is a local file matching the included file name that you don't want to include, preferring a file of the same name located in the standard search path. For a beginner that might be a reasonable approach but also a beginner will practically never have a local file include so angle brackets will serve them just as well.

For anyone more experienced, I say tke a few minutes to understand the difference and then always use the most correct syntax. Unfortunately it's very common to see angle brackets used for including local files in Arduino libraries. This works fine when the library is installed normally but if you want to bundle the library with a sketch so you can distribute it as a self-contained package you need to fix all the instances of incorrect use of angle brackets. This is a good example of why it's best to just understand which syntax is correct and use it.

LiquidCrystal_I2C.h : Could not trace the directory!?
If you can include it then it must be somewhere on your computer.
To find any library that has example sketches do this:

Another way to find it is to enabling verbose output during compilation in File > Preferences and then examining the contents of the console after a compilation of a sketch that includes the library.
Title: Re: I2C_Keypad problem
Post by: LesserMole on Feb 16, 2018, 07:13 am
To find any library that has example sketches do this:
  • Tools > Examples > open any example from the library you want to find


I think you mean File > Examples

Title: Re: I2C_Keypad problem
Post by: pert on Feb 16, 2018, 07:16 am
Thanks for the correction LesserMole! That was in of my collection of text snippets I use to copy/paste into my forum replies so it's definitely important for me to fix it.
Title: Re: I2C_Keypad problem
Post by: sterretje on Feb 16, 2018, 11:22 am
I have executed a small program to monitor temperature using internal sensor of DS3231RTC and to display it using I2C 16x2 LCD. I may include the following two header files in any combinations of double quotes and angle brackets; the program always works.

LiquidCrystal_I2C.h
Wire.h

When I searched my PC, I found the these header files under the directories as indicated.
LiquidCrystal_I2C.h : Could not trace the directory!?
Wire.h : c:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire\src\Wire.h
Did you search complete drives? That library might well be in the documents folder.

My question: Is it really important for a programmer to care about the use of double quotes or angle brackets while including the header files? Traditionally, we are accustomed in the use of angle brackets.
Yes. Imagine you have a project with library A which is conflicting with another library B and your project requires both (usually interrupt conflicts).

You can modify one of those libraries in their original location, but that will affect all your previous projects and all future projects that use the modified version of the library and might result in errors in previously working sketches.

So you make a local copy of the library in your sketch directory, modify it, include using double quotes and you're done; no side effect. As pointed out by pert, you will have to check all files of the modified library as they all need to use double quotes when including the library specific files from the sketch directory.
Title: Re: I2C_Keypad problem
Post by: GolamMostafa on Feb 16, 2018, 11:41 am
@pert

Following the guidelines of your Post#14, I have been able to trace the locations of the library files as follows:

1. Library files that are automatically installed when the IDE (1.8.0) is installed are found here:
a. c:\Program Files\Arduino\libraries\Library folders for 19 devices ike: LiquidCrystal and etc.
b. c:\Program Files\Arduino\hardware\arduino\avr\libraries\Library folders for 5 devices ike: SPI, Wire and etc.

2. Library files that are automatically installed when we download them as zip files, and later on include them using the IDE (Add .ZIP Library...).
c:\Users\GM\Documents\Arduino\libraries\Library folders for custom devices like: LiquidCrystal_I2C and etc.

Now, we see that there are three libraries; when you say standard library (Post#14), which library folder (1a, 1b, or 2) are you referring?  

BTW: Enabling verbose does not provide the above information (1a, 1b, and 2).

Many many thanks along with karma point.
Title: Re: I2C_Keypad problem
Post by: GolamMostafa on Feb 16, 2018, 12:03 pm
@sterretje  
Quote
So you make a local copy of the library in your sketch directory, modify it, include using double quotes and you're done; no side effect.
About 27 years ago, as an Electrical Engineer, I wanted to learn DOS-C Programming and collected a book on C Programming where I encountered the case of the header file (s) being included using both double quotes and angle brackets. The question of the the then time -- what difference do they make -- is definitely better answered in Post#17. Thanks with +.

Quote
Did you search complete drives? That library might well be in the documents folder.
Yes! They are under c:\Users\GM\Documents folder; these Library Files are those files which we download as .zip files and later on include them using IDE (Add .ZIP Library...).
Title: Re: I2C_Keypad problem
Post by: sterretje on Feb 16, 2018, 03:40 pm
The example I gave applies to open source environments where you can freely modify the source code. In the older days, you included stdio.h and the likes using <>. But once you got further you also started splitting your project into multiple files.

In the Arduino environment you can do that with multiple tabs which will live in your sketch directory. One option is to have multiple ino files, the other one is to have multiple .h and .c/.cpp files. And to include those, you need to include using the double quotes, else the 'user' directory is not searched for these files.
Title: Re: I2C_Keypad problem
Post by: pert on Feb 16, 2018, 06:00 pm
Now, we see that there are three libraries; when you say standard library (Post#14), which library folder (1a, 1b, or 2) are you referring?  
Any of those locations. I listed them in #10:
Actually there is a whole list of directories that are searched for the file:
  • Sketchbook: {sketchbook folder}/libraries. The sketchbook folder location can be found (or changed) at File > Preferences > Sketchbook location:.
  • IDE: {Arduino IDE install folder}/libraries. This is for the libraries included with the Arduino IDE installation.
  • Core: Libraries bundled with hardware packages. This is dependent on the current selection in Tools > Board. Location will depend on if you're using a hardware core bundled with the IDE, installed via Boards Manager, or manually installed to {sketchbook folder}/hardware.
  • Toolchain: The compiler package also provides library files that can be included (e.g. avr/wdt.h).

So your 1a is the IDE's libraries folder. Your 1b is the hardware core's libraries folder (but note that c:\Program Files\Arduino\hardware\arduino\avr\cores and c:\Program Files\Arduino\hardware\arduino\avr\variants are also in the search path). Your 2 is the sketchbook libraries folder.
Title: Re: I2C_Keypad problem
Post by: GolamMostafa on Feb 16, 2018, 06:26 pm
Indeed, very good learning session that I have ever enjoyed in this Forum on such a complicated (conceptually) issue. Thanks to all contributors.