I want to split my project with the functions in separate 'cpp' and accompanying 'h' files files, to make it tidier, but if I take this trivial function example and put it in a separate file, I get errors:
shutter_release.h: #pragma once;
extern int shutter release_pin;
void release_shutter();
//EOF
I didn't have to declare them in the pde file where the function originally 'lived', so why (and how) do I have to do it now, please?
You didn't have to because the IDE did it for you. The missing values are defined in WProgram.h or Arduino.h, depending on which version of the IDE you are using. You need to #include one of these files in your .h file.
By the way, the #pragma once directive is not the preferred way on the Arduino.
I didn't have to declare them in the pde file where the function originally 'lived', so why (and how) do I have to do it now, please?
You didn't have to because the IDE did it for you. The missing values are defined in WProgram.h or Arduino.h, depending on which version of the IDE you are using. You need to #include one of these files in your .h file.
I wondered if it was something like that. Where in the arduino documentation does it mention this sort of thing - I think I need to get up to speed with it?
By the way, the #pragma once directive is not the preferred way on the Arduino.
I though this might be so. I guess 'include guards' are the preferred way.
jimford:
In connection with this, can anyone point me to documentation relating to 'splitting' files with reference to the arduino IDE, please.
Jim
My blog post has something you might want to read:
Essentially you don't have to have the files compile separately like you did in the example. You just want the codes to reside in different tabs so you don't have to scroll a chunk of code that has already been polished to work perfectly. This requires you to create separate tabs with no extensions (arduino IDE will add .pde or .ino automatically). All codes ending with .pde and .ino are concatenated before compile so none of the code tabs will need to separately provide the .h files PaulS mentioned. Only when you write libraries and classes do you need to separately compile files with .h and .cpp.
Here is what Blink looks before it compiles into hex code:
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
#include "WProgram.h"
void setup();
void loop();
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
This following section is generated by the arduino IDE so you don't have to know what to do as a beginner:
#include "WProgram.h"
void setup();
void loop();
If you make a separate .cpp file, you will have to take care of that.
Thanks 'Luidr' - it looks like it's just what I need to know! Your blog is duly printed and I'm now off to have a coffee and read it!
BTW, how did you know about splitting the file into different tabs? It isn't in the standard works on C and C++. Is it mentioned in the arduino IDE docs somewhere?
Jim,
You are welcome! I think I learned the tabs from experimenting and asking questions on the forum just like you. I think it is a convenient feature that Arduino has. If the tabs are like C or C++, then each tab needs to include some headers for proper definitions and extern etc to share variables across tabs. That is too much for beginners, considering Arduino was developed for interactive artists. The Arduino IDE concatenates all non .cpp or .h files, add the function declarations so you don't have to define before use and it adds all the include files and creates the main.cpp. If you insist the .cpp and .h tabs, then you can use all you know from C and C ++ but have to know what you are doing:)