Split up funcitons to header-file and .cpp file

Hi, I recently went from a single file at about 300 lines, to 4 files with different compontents. I’ve understood that the common practise is to have all the pins and functions declared in a header-file, and then have the full function in a .cpp file (with the same name as the header-file), and then include the header-file, in the .cpp file, and then again include the header-file in the arduino file. This is done to abstract the hardware from the software, so that I can easily use the same software on multiple robots. Correct, right?

I had some issues when about splitting the file. My current issue is that I have my function declared in the header-file, but when I’m calling the function in loop(), I get an error: expected primary-expression before 'dir’
. when I put in void before the function, it passes, but is this the way it should be?

The files that I uploaded are simplified. Propultion_slider() is unfinished, sorry for that. I just couldn’t figure out why my fuction propulsion() didn’t work, but I found the solution.

propultion(direction_t dir, int power);
direction_t is a enum. A new datatype, like int or char. It can be three values.
STOP = 3
I think the problem was that I didn’t spesify what dir and power should be.

Sorry for a messy thread. New to posting on forums.
Thanks for some good replies

Best regards
Sebastian from Norway

example.ino (175 Bytes)

motorController.cpp (1.24 KB)

motorController.h (1.63 KB)

This is one mistake in your cpp file

void check propultion_slider()

After fixing that, possibly others will follow.

Errors/warnings that I get

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard" "C:\Users\sterretje\AppData\Local\Temp\arduino_build_95009\sketch\motorController.cpp" -o "C:\Users\sterretje\AppData\Local\Temp\arduino_build_95009\sketch\motorController.cpp.o"
motorController.cpp:36: error: expected initializer before 'propultion_slider'

 void check propultion_slider() {


In file included from C:\Users\sterretje\AppData\Local\Temp\arduino_build_95009\sketch\motorController.cpp:1:0:

C:\Users\sterretje\AppData\Local\Temp\arduino_build_95009\sketch\motorController.h:31:3: warning: 'RemoteXY' defined but not used [-Wunused-variable]

 } RemoteXY;


exit status 1
expected initializer before 'propultion_slider'

First, only put into the .h file declarations, not definitions. See: What's the difference between declaring and defining in C and C++ - Cprogramming.com. Your .h file incorrectly contains definitions for the Motor Pin variables.

Second, only put into the .h file things that are needed in the associated .cpp AND OTHER places (.ino, other .cpp, or other .h). I doesn’t look like the Motor Pin variables are needed in the .ino file, so they don’t belong in the .h file. Get them out and put them in the motorController.cpp file. AND, make them static so they are ONLY available to the motorController.cpp file.

Also, this is wrong:

propulsion(direction_t dir, int power);

Don’t include the variable type when you CALL a function. Should be something like this:

propulsion(FORWARD,  2);

I think there are other problems too. But, fix those and then come back with additional compiler errors.

Finally, please POST your code using code tags. See ITEM #6 here: https://forum.arduino.cc/index.php?topic=97455.0