[Solved] #include files

I do a fair bit of programming in PHP and one can just put <?php include "myfile.php" ?> anywhere in the script to make editing the files easier. For my Arduino project I'm making some custom characters for an LCD display but putting #include "customchars.txt" doesn't work - I've tried with all sorts of file paths ("./customchars.txt" etc.) but no joy. Am I barking up the wrong tree?

...doesn't work...

Didn't protect your favourite belt from the wild turkeys?

I'm confused...

"doesn't work" is meaningless to everyone except you and someone capable of reading minds (potentially over great distances). I am neither.

My response was a guess at what you meant by "doesn't work".

Ah, OK. It doesn't compile (verify fails saying it can't find the file specified). This was under Windows XP

Post your code, not just "I did something and it didn't work" stuff.

It doesn't compile ...

Still post your code. What is this "it" of which you speak?

Just a heads-up. You have to tell the IDE about your files because it copies them all to another temporary folder. Just putting a file into the directory doesn't cut it. You have to make a new "tab" in the IDE.

OK, to demonstrate, I took the example sketch "CustomCharacter" (from the LCD bit) and saved it as "CustomCharacterTest". I added a new tab, named it "custchars". I took the custom character definitions from the main sketch and moved them to this new tab. I then added the line #include "custchars.ino"

Here's a screen grab of what happens when I click "Verify":

As you can see, it can't find the custom character definitions (which were unchanged from the example sketch) as it can't find the file "custchars.ino". I've tried putting "./" before the filename and even tried putting it in angular brackets (which oddly enough didn't cause a "no such file or directory" error, although the other errors were just the same

OK, I changed the extension of the "custchars" to .h and after adding "#define byte uint8_t" to the top of the file (because I was getting a "byte does not name a type" error) and modifying the ambiguous lcd.write call, it now compiles OK

#include <LiquidCrystal.h>
#include "custchars.h"

// initialize the library with the numbers of the interface pins



void setup() {
  // create a new character
  lcd.createChar(1, heart);
  // create a new character
  lcd.createChar(2, smiley);
  // create a new character
  lcd.createChar(3, frownie);
  // create a new character
  lcd.createChar(4, armsDown);  
  // create a new character
  lcd.createChar(5, armsUp);  

  // set up the lcd's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the lcd.
  lcd.print("I "); 
  lcd.write(1); // numbers above changed to stop ambiguous error call 
  lcd.print(" Arduino! ");
  lcd.write(2);
...

Just waiting for my LCD to arrive so I can start playing with it properly! ]:smiley:

spandit:
after adding "#define byte uint8_t" to the top of the file (because I was getting a "byte does not name a type" error)

I suggest replacing that with...

#include <inttypes.h>

If you say so! :smiley:

As you can see, it can't find the custom character definitions (which were unchanged from the example sketch) as it can't find the file "custchars.ino".

If you make a tab which is not a .h, .c or .cpp file (in other words, another .ino file) the IDE concatenates it with your main sketch and compiles it as a single unit. You don't need to include it. Because of this behaviour it has probably not actually copied this other .ino to the temporary directory, and even if it had, you would then have used it twice, which you wouldn't want.

You are better off sticking to the .h or .cpp paradigm (who includes .txt files into C source?) and work with the IDE on this one.

Actually, I only put a .txt file for this forum - I was trying to include a .ino file but you say they are concatenated together (in alphabetical or tab order?). I'll stick to the .h for the headers and rejig my code to use the tabbed bits

Tab order I think (or is it?) Anyway, if you close and re-open the sketch I think it loads the files in the order it finds them. Not sure. That's why I don't use that "feature".