multiple file causes compilation error

Hello Forum,

I have write a code with one main.ino file one header file and a c file. All are in same folder. Now when i try to compile the code, i get random errors like Serial was not declared in this scope, byte was not declared in this scope such kind of. Please guys get me out from this thing.

Obviously, the code you haven't shown us is causing the compilation / linking errors that you haven't shown us.

Thank you for replying quick here is my code in attachment. please make change accordingly.

nishikanthingole:
i get random errors like Serial was not declared in this scope, byte was not declared in this scope such kind of

The declarations of the Arduino core library functions (and the typedef for byte) are brought in by Arduino.h. At the start of the compilation process, the Arduino IDE does some minimal preprocessing on your .ino files to make them valid C++, after which they are compiled as C++. Details here:

One of the things done to the .ino files during sketch preprocessing is adding this line if it isn’t already present:

#include <Arduino.h>

So you can use the Arduino core library functions in your .ino files without needing to add that #include directive. However, sketch preprocessing is only done to .ino files (and .pde files also). Files with other extensions, such as .h, .c, .cpp are left as is. So if you want to use the core library functions in those files you need to add the #include directive fore Arduino.h.

There is another issue though. Many of the Arduino library components are C++, but your .c files will be compiled as C. You can’t use C++ in C. Things like Serial.print(), Wire.beginTransmission() use classes, which are a C+±only thing. So if you want to use this code, you need to use the .cpp file extension, which will cause it to be compiled as C++.

Rename HRM_ON_OFF.c to HRM_ON_OFF.cpp and re-open the sketch. That will allow you to use Serial.print() in that part of your code.

With MAX set to 500 you are using a LOT of memory. What model Arduino are you planning to use? To compile it for an UNO I had to set MAX to 20 and even then it uses 98% of available memory.

Thank you guys for replying.
I have done as per suggestion.
Still got same error.
Renamed file with .cpp, include in .ino
please check attachment.

nishikanthingole:
include in .ino

You misunderstood what I said. There is no need to add the #include directive for Arduino.h in the .ino file because the Arduino IDE automatically adds that for you. However, the Arduino IDE does not add the #include directive for Arduino.h to .h and .cpp files. So you do need to add the #include directive for Arduino.h to those files if you use things from the Arduino core library in those files.

You also have a problem that you are calling functions before they are declared in your .cpp file. Another one of the things the Arduino IDE does to .ino files during sketch preprocessing is to add function prototypes to the top of the .ino file for every function in the file that doesn’t already have a prototype. This allows you to call the functions anywhere you like in your sketch. However, the .cpp file doesn’t get this sketch preprocessing, so in your .cpp file you must declare functions before you call them. This is commonly done by putting all the function definitions in a .h file, which you have done. What you forgot is that you also need to add an #include directive for that .h file to the .cpp file.

After that, I hit another error where you are trying to use a variable you declared in your .ino file in the .cpp file. You need to understand that the .ino files of your sketch are one translation unit and each .cpp file is a separate translation unit. They don’t share variables. You can use global variables, but this is not best practices. Better is to use functions to pass data between translation units. For example, you could add a function to your .cpp file:

void setResetzPin(byte pinNumber) {
  ResetzPin = pinNumber;
}

Then in your sketch, define the pin number and pass it to the .cpp file:

setResetzPin(ResetzPin);

That is good for when you want to make it easy for the user to configure the hardware from the sketch. In other cases, maybe the Resetz pin is fixed. In that case, it would make sense to define it in the library and provide some mechanism for the ino file to read that. Maybe it would be a macro in the .h file:

#define HRM_RESETZ_PIN 15

Thank you guys for solving the problem. With you suggestions and rearrangement the function sequence, problem get solve. Thank you once again.

You’re welcome. I’m glad to hear it’s working now.

nishikanthingole:
rearrangement the function sequence

This is not necessary. You have all the function declarations in your .h file. So all you needed to do is add an #include directive to your .cpp file. This is something that might be tricky for Arduino users at first because the Arduino IDE automatically generates function prototypes for the functions in your .ino files. I recommend you study this reference to learn more about function prototypes:
http://www.cplusplus.com/doc/tutorial/functions/#declarations
This knowledge is very important if you are planning to continue using non-.ino files.

Hi, Hows about posting your working code for everybody?

You deleted the attachment refered to in post #5, bad move, the threads on the forum are here to not only help the OP but others with similar problems. Buy deleting your code and not posting the working code you have made this thread useless.

It has only benefited you, and not everybody, which is the spirit of the forum.

Tom... :)

Hello @TomGeorge
Please find program in attachment

I will create a new thread related with same program.

Need to do multi-threading, means i want to run the three task parallel.
temp task should run after every 5 sec
Hrm should run after temperature task which lasts for 50sec and last acceleration should run after every 5 sec.

12032020.zip (9.92 KB)

This is link for new thread for same program

https://forum.arduino.cc/index.php?topic=671082.0

define.h|attachment (781 Bytes)

fun_def.cpp|attachment (26.4 KB)

Header.h|attachment (3.98 KB)

13032020.ino (5.33 KB)