Pages: [1]   Go Down
Author Topic: [Solved] #include files  (Read 525 times)
0 Members and 1 Guest are viewing this topic.
Sussex, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: December 13, 2012, 07:07:23 am by spandit » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...doesn't work...

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

Sussex, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...doesn't work...

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


I'm confused...
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


"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".
Logged

Sussex, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


"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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
It doesn't compile  ...

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Sussex, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
#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
Logged

Sussex, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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-twist
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

Code:
#include <inttypes.h>
Logged

Sussex, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

Code:
#include <inttypes.h>


If you say so! smiley-grin
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Sussex, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
Logged

Pages: [1]   Go Up
Jump to: