I'm trying to split up a large project of mine into separate tabs in the Arduino IDE and I'm having more than a few troubles with it. My main trouble is finding a way to have routines in extra tabs access the Serial object; because it's really hard to debug things withSerial.println() otherwise. All I get, though, is "'Serial' was not declared in this scope".There appears to be some kind of trick to this? I've noticed that "byte" or "bool" needs to be declared in .cpp tabs and .h tabs but not on the main one?Is there some kind of easy way to set this up so that I don't need to constantly#include <stdbool.h> andtypedef unsigned char byte;?
Current issue: Main file:
void setup() { Serial.begin(500000); PrintMessage();}void loop() {}driver.ino file:
byte PrintMessge () { Serial.println("printin");}and on compiling:
error: 'PrintMessage' was not declared in this scopeEDIT: Found a silly typo. Problem solved.
- 2Did you try
#include <Arduino.h>at the beginning? That should expose theSerialobject. Otherwise you'll need to show us the code files in which the error occurs.Maximilian Gerhardt– Maximilian Gerhardt2018-02-25 20:46:04 +00:00CommentedFeb 25, 2018 at 20:46
2 Answers2
This question is more about C/C++ header basics rather than Arduino. Anyways, you should have the following file structure: Code in.cppfiles, Header in.h files. Main sketch either.ino or.cpp (for cpp remember that you must explicitly write out the function prototypes!)
So for your small example that would be:
main.ino:
#include "driver.h"void setup() { Serial.begin(500000); PrintMessage();}void loop() {}driver.cpp
#include "driver.h"byte PrintMessage () { Serial.println("printin"); return 0; //previously had no return although returning 'byte'.}driver.h
//Always use include guards #ifndef DRIVER_H #define DRIVER_H #include <Arduino.h> //All function prototypes from driver.cpp. byte PrintMessage(); #endif /* DRIVER_H */Also corrected a typo fromPrintMessge toPrintMessage. This is the "professional" way to do it, seperate implementation and header files.
Tabs are actually quite simple:
- If it's named
<something>.inoit will get merged with the main tab. It's just like working in one big file and you don't need to do anything special. - If it's named
<something>.cppor<something>.cit's a completely separatetranslation unit and has to be manually "enroled" into the Arduino system by including theArduino.hheader.
So it's simplest to just name them as INO files not CPP files and you don't have to do anything special:
- That's not working out for me for some reason. I'm getting the compilation error
No such file or directoryat the line#include "drtiver.ino"Bo Thompson– Bo Thompson2018-02-25 21:23:20 +00:00CommentedFeb 25, 2018 at 21:23 - Why are you trying to include an ino file???Majenko– Majenko2018-02-25 21:24:28 +00:00CommentedFeb 25, 2018 at 21:24
- Because if I don't then I get errors that make less sense. Main file:
void setup() { Serial.begin(500000); PrintMessage(); }driver.ino file:byte ReadMessge () { Serial.println("printin"); }Error 'PrintMessage' not declared in this scope.Bo Thompson– Bo Thompson2018-02-25 21:29:17 +00:00CommentedFeb 25, 2018 at 21:29 - Um... yes? You'd expect that when you have one calling
PrintMessageand the other providingReadMessage...Majenko– Majenko2018-02-25 21:31:14 +00:00CommentedFeb 25, 2018 at 21:31 - 1I'll assume that was a typo too...Majenko– Majenko2018-02-25 21:47:46 +00:00CommentedFeb 25, 2018 at 21:47
Explore related questions
See similar questions with these tags.

