3

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 with Serial.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> and typedef 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 scope

EDIT: Found a silly typo. Problem solved.

  • 2
    Did you try #include <Arduino.h> at the beginning? That should expose the Serial object. Otherwise you'll need to show us the code files in which the error occurs. – Maximilian Gerhardt Feb 25 '18 at 20:46
4

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 from PrintMessge to PrintMessage. This is the "professional" way to do it, seperate implementation and header files.

1

Tabs are actually quite simple:

  • If it's named <something>.ino it 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>.cpp or <something>.c it's a completely separate translation unit and has to be manually "enroled" into the Arduino system by including the Arduino.h header.

So it's simplest to just name them as INO files not CPP files and you don't have to do anything special:

enter image description hereenter image description here

  • That's not working out for me for some reason. I'm getting the compilation error No such file or directory at the line #include "drtiver.ino" – Bo Thompson Feb 25 '18 at 21:23
  • Why are you trying to include an ino file??? – Majenko Feb 25 '18 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 Feb 25 '18 at 21:29
  • Um... yes? You'd expect that when you have one calling PrintMessage and the other providing ReadMessage... – Majenko Feb 25 '18 at 21:31
  • 1
    I'll assume that was a typo too... – Majenko Feb 25 '18 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.