(SOLVED) How to #include my snippets and functions ( xxxx.ino)

I have searched playground, and other arduion.cc sections, and googled. But I have not found how to include a sketch/function I have previously create. I did see info on tabs, but it appears to just break a project apart, not allow for usage in new projects.

I want to have several common functions(sketch) such as blink.ino, that I can easily include in a new sketch. And of course, if I make improvements to blink.ino, I only have to compile and upload the programs that include it.

There is probably a link that covers this, but I have had no luck finding it. Thanks for any suggestions.

Jack

I want to have several common functions(sketch) such as blink.ino, that I can easily include in a new sketch

But "blink.ino" has a "setup()" and a "loop()", doesn't it?
You cannot have multiple setups and loops in a single sketch.

No, I removed all but the function blink() before I saved it as blink. I can open blink with the IDE, and this is what is in it.

void blink(){
  static byte lastState ;
  pinMode ( 13, OUTPUT );
  if (lastState != HIGH){
    digitalWrite ( 13, HIGH ); 
    lastState = HIGH;
  }
  else {
    digitalWrite ( 13, LOW ); 
    lastState = LOW;
  }
}

I try (no success) to include it like:

#INCLUDE <blink.ino>

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly: 
  blink();
  delay (500);
}

I try (no success) to include it like:

You'll need to provide a lot more information about what happened/failed to happen. Perhaps its as simple as the fact that INCLUDE and include are different keywords (only one of which is supported).

I have changed #INCLUDE to #include.
I have tried 3 varieties as shown in my new sketch.

I get a compile error, saying 'blink' was not declared in this scope
The highlighted error line is the blink(); inside the loop() function.

#include "blink.ino"
#include <blink.ino>
#include blink.ino
void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly: 
 blink();
  delay (500);
}

There is a blink.ino saved in the sketchbook directory (where all the sketches I make are).
It is:

void blink(){
  static byte lastState ;
  pinMode ( 13, OUTPUT );
  if (lastState != HIGH){
    digitalWrite ( 13, HIGH ); 
    lastState = HIGH;
  }
  else {
    digitalWrite ( 13, LOW ); 
    lastState = LOW;
  }
}
#include "blink.ino"

Includes files that live in one or both of the libraries directories.

#include <blink.ino>

Includes files that live in one or both of the libraries directories.

(One set of characters causes reading from both. The other set causes reading from only one. I can never remember which is which.)

#include blink.ino

Causes reading ONLY from the current directory, which is unnecessary, as the IDE already combines ALL .ino files in the current directory into one .cpp file.

The path to the directory blink is here, and within that directory is blink.ino
C:\Documents and Settings\Kevin Benson\My Documents\Arduino\blink

#include blink.ino
Causes reading ONLY from the current directory, which is unnecessary, as the IDE already combines ALL .ino files in the current directory into one .cpp file.

So, when I click on compile in the IDE, where is the current directory?

Are you telling me what I want: "include another sketch into the one I am compiling", can't be done with this IDE?

Are you telling me what I want: "include another sketch into the one I am compiling", can't be done with this IDE?

No, I'm not. But, the .ino file must either be in a libraries folder OR you must use the EXPLICIT path to the file. The IDE will not guess for you.

jackwp:
So, when I click on compile in the IDE, where is the current directory?

That's an interesting question. As I understand it, the IDE scrapes up all the source files it thinks are needed to compile your sketch, and dumps them all into a temporary directory. In practice that means all the source files in the sketch directory (with the ".ino" to ".cpp" munging that has already been mentioned), and all the source files in any library that is referenced by your code. A library is deemed referenced if a ".ino" file in the sketch directory #includes a header file that is in the library directory.

This scraping and munging and copying-into-temporary-directories is one of the things I like least about the Arduino development environment - it's poorly done and IMO misguided, and means that the actual behaviour of C++ code in an Arduino environment doesn't follow the conventional behaviour that you could expect to find in any other C++ environment.

It's because of these differences that I'm inclined to view the Arduino programming language as distinct from C++ although closely related - given that the Arduino documentation explicitly refers to the Arduino programming language, I suspect the Arduino developers had the same view.

jackwp:
The path to the directory blink is here, and within that directory is blink.ino
C:\Documents and Settings\Kevin Benson\My Documents\Arduino\blink

Move blink.ino to here...
C:\Documents and Settings\Kevin Benson\My Documents\Arduino<mark>libraries\blink\

... and name it blink.cpp not blink.ino, add #include <Arduino.h> at the top, and create a blink.h file with extern references to your functions. Then you include blink.h in your sketch and you have access to all your blink functions.

That's called writing a library.

http://playground.arduino.cc/Code/Library

I was going to create a library called snippets and #include that in any new sketch I created. If I saw a function somewhere that I thought might come in handy for re-use (eg blink), I would just declare it in snippets.h and define it snippets.cpp. And that way it would be available for me if I wanted. The way I understand it is that the compiler is smart enough to just ignore everything in my snippets library if I don't use any of the functions in there and if I do use one of the functions, it only includes the appropriate sections from snippets library. So my snippets library may grow to be quite a sizeable bunch of unrelated functions but not impose significantly on my sketch size.

Thanks for all the help guys!. So I think i understand that:

  1. #include , can't include any xxx.ino files.
  2. xxxx.ino files will be included without asking for them if they reside in the same directory ( created by tab, or copied there).

I do not want to create xxx.f files or learn c again (I used c in 1975, then forgot it).

I tried copying a xxx.ino file (blink.ino) into a sketch directory, and it got included ( worked as a tab).
I tried to copy a xxx.ino file into the directory as a short cut (alias), but the compile did not like it being a short cut (I am using windows, maybe it would work if I was using linux).

So since I don't want to use xxx.f files, my best option is to copy my xxx.ino file to each new directory. If I modify my xxx.ino library file, then I have to copy to all the directories again If I want the latest version.

Do you think if I ask, they may add an include option to include xxx.ino files in a future IDE release? Am I the only one that thinks it would be useful?

Thanks again guys.
Jack

jackwp:
Thanks for all the help guys!. So I think i understand that:

  1. #include , can't include any xxx.ino files.
  2. xxxx.ino files will be included without asking for them if they reside in the same directory ( created by tab, or copied there).

I do not want to create xxx.f files or learn c again (I used c in 1975, then forgot it).

Tough - you're using C whether you like it or not.

An INO file is just a CPP file that has #include <Arduino.h> and function prototypes dumped at the top of it by the IDE.

Just create a library folder named "snippets". Then in there create two files.

snippets.cpp:

#include <snippets.h>

void blink(){
  static byte lastState ;
  pinMode ( 13, OUTPUT );
  if (lastState != HIGH){
    digitalWrite ( 13, HIGH ); 
    lastState = HIGH;
  }
  else {
    digitalWrite ( 13, LOW ); 
    lastState = LOW;
  }
}

snippets.h:

#ifndef _SNIPPETS_H
#define _SNIPPETS_H
#include <Arduino.h>

extern void blink();

#endif

Then just add your snippet functions to snippets.cpp (just as if it were a .ino file), and make sure you add the new function prototypes (a copy of the first line of your function with a semicolon after it and the word "extern" brefore it) to snippets.h where the existing one is.

Then just #include <snippets.h> in any sketch and AWOL's your uncle.

#include <snippets.h>

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  blink();
  delay(500);
}

No copying around the place, no redundant or old versions, just one central collection of snippets.

Surely that's gotta be easier than copying .ino files around the place...?

THANKS majenko! Those are details I think I can follow. I will give that a try.

majenko, you are the greatest!
I used your detail and got it working easily.
I even added a new snippet, and updated the snippets.h file, and it worked well to.
Here is the short sketch that blinks and beeps, in only 8 lines of code.

#include <snippets.h>
void setup() {}
void loop() {
  blink();
  delay(90);
  beep();
  delay(999);
}

Thanks all, and especially majenko.
Even tho I couldn't get it to work with xxx.ino, the details majenko provided, made it easy to do.

Pretty much all set out here

@AWOL, thanks for the link (a couple days late), but it is interesting. We (majenko) have solved the problem, and I think majenko has a more understandable example (no class needed), than that link. I just wanted a library of my commonly used functions.
Thanks
Jack