(Technically an Arduino question) using multiple .ino files

So I’m working on the SNUKE game (check it out in the Games section #ShamelessSelfPromotion) and I want to make it a bit neater by splitting the functionality into multiple files.

So my sketch looks a bit like this:

SNUKE/SNUKE.ino

void Setup() {
SnakeHead = new SnakeSegment();
}

SNUKE/SnakeSegment.ino

class SnakeSegment
{

};

Now when I look on the internet, I see a lot of answers akin to “This should just work” - the Arduino compiler should automatically put one in front of the other. Only, it doesn’t. If I paste the second file into the first, it works fine, but if I try 2 separate files, it can’t find the SnakeSegment class.

I know I could technically switch to .h/,cpp files, but before I do that, does anyone have any idea why this doesn’t work?

For whatever reason, I cannot for the life of me get the Arduino IDE to #include a .ino file, but a .c file works fine which is how I deal with multiple files normally. I know that the Arduino IDE will deal with including files automatically if you use its inbuilt tabs(which save as .ino files so it’s definitely possible) so you could potentially use those as a workaround, just click on the down arrow button in the top right of the IDE and select “New Tab”, then name it what you want and copy the appropriate code into the new tab.

Yeah the thing is: I did that. Didn’t work. I have the 2 tabs, both are .ino files, as far as I can tell it should just work… Guess it’s back to good ol’ cpp + h files then…

I had a similiar problem with splitting a project into two .ino files a while ago. For me it worked after changing something in the directory path…I don’t remember if a space was the problem or special characters.

I switched to a cpp/h file, and it recognises the class now, but it doesn’t recognise my functions.
I’m probably doing something really obvious wrong, but I can’t see it. Anyone have any idea?

My H file:
class SnakeSegment
{
public:
int X;
int Y;
void DrawSnake(int lastSnakeX, int lastSnakeY, int headrotation, bool firstSegment);
};

My CPP file:
class SnakeSegment
{
public:
int X;
int Y;
void DrawSnake(int lastSnakeX, int lastSnakeY, int headrotation, bool firstSegment) {
//code
}
};

My .ino file:
#include “SnakeSegment.h”
SnakeSegment* SnakeHead;
void setup() {
//This part works!
SnakeHead = new SnakeSegment();
SnakeHead->X = headpos_x;
SnakeHead->Y = headpos_y;
}

void loop() {
//This part gives D:\Gemberkoekje\Gamebuino\SNUKE/SNUKE.ino:161: undefined reference to `SnakeSegment::DrawSnake(int, int, int, bool)’

SnakeHead->DrawSnake(0, 0, headrotation, true);

}

Do you mind posting the full source code so I can have a fiddle with it? You can PM it to me if you don’t want it public yet).

I messaged it, I was like a quarter of the way to refactoring it, so sorry if legibility is not prime, but it should work barring the multi-file issue.

As far as I remember, other .ino files don’t have to be included. Tey just have to be in the same folder. Using .h/.cpp is much cleaner though. :slight_smile:

Yeah, I know that should happen. Problem was that it didn’t… oh well, I’ll figure it out eventually