r/arduino • • 27d ago

Mod's Choice! Interesting Arduino Behavior - Variables not in Context

Post image

I was doing a live debugging session and found an interesting behavior with variables. I'd declared this new structure, GameState, before I called it in any of my functions - standard C practice. However, Arduino C has a "helpful" feature where it will create a prototype of your function at the beginning of your file, right before the setup runs. So the structure ended up actually being defined /after/ it was called, which stopped the code from compiling. The solution was to move all my variable definitions to the beginning of the file.

This is kind of a pain, because I'm trying to build a template for an Arduino sketch that has a few MQTT topics built in, then you can build user code that makes use of those topics to communicate with an external device like your phone. I was intending for all the user code to end up at the end of the file in one place, but it looks like I'll have to chunk it out so that the end user can put their stuff in the right order. Of course, the real solution will be to put all this infrastructure into a header file and library, but one step at a time...

Anyway, if you're interested in the debugging session, it's here. You can also see how someone might use Claude to help them fix code. (And very importantly, that using Claude does not exempt you from needing to test your code!)

https://youtu.be/6yW2u79d7Ls?si=dRZP8xgChvEna6ok

If that sketch sounds handy, you can find a copy on this github. It's not finished yet, but I should be done testing it and updating it in a few days.

https://github.com/DocCarr/sproutsodyssey.git

4 Upvotes

8 comments sorted by

5

u/gm310509 400K , 500K , 600K , 640K , 750K 27d ago

This is one of the more annoying features of the Arduino IDE.

Actually what it is doing is inserting function prototypes into the ino file (it doesn't do it for CPP or C files, just ino files if memory serves).

The goal is to allow functions to be declared in any order (i.e. you can call it before you declare it). The function prototypes are inserted just before the first function declaration.

As you have observed if you want to define a custom type and you want to declare that in conjunction with the function that uses it (i.e. group the function and it's custom data type together in the code) and that custom data type is used as a parameter to the function, then you get a variant of an "undeclared type" of error.

When I first encountered this I remember struggling with the "X is not a datatype error" for a few hours - especially when I could see it right there in the code.

I had forgotten about the function prototype injection into ino files, but when I finally saw a reference to it (can't remember what that was), the penny dropped.

I then immediately checked the CPP file that is generated from the ino file and there it was, a prototype for my function inserted into the code before my datatype declaration.

Now a days I tend to just automatically add a .C or .CPP (with a .h) file to my project and put the function plus data types into those source files.

Thanks for sharing, it is a timely reminder and definitely a trap for new players.
The side affect is that people who learn on Arduino and then move to a "proper" C/C++ environment will get different problems when they follow the habit they learned of putting functions in random order in the source code.

2

u/gm310509 400K , 500K , 600K , 640K , 750K 27d ago

I set your flair to "mod's choice", which means your post will be in prime placement in the next Monthly Digest.

Again, thanks for sharing.

1

u/DocCarr 26d ago

Thank you! Yeah, it was an interesting error. It sounds like a lot of people have already hit on the header solution. If I update this post to include that fix, will it be included in the Monthly Digest?

1

u/gm310509 400K , 500K , 600K , 640K , 750K 24d ago

The digest is an index of interesting posts. It links to them, so yes, any changes you make will be made to the linked posts and thus visible to anyone who follows the link in the digest

1

u/Fluid-Let-2545 26d ago

The auto-prototype thing has bitten me too. I mostly just use.cpp/.h files now for anything nontrivial, saves the headache.

That MQTT template sounds useful though. Ill keep an eye on the repo.

1

u/DocCarr 26d ago

Thanks! I just updated the repo with a version that successfully compiles. I haven't done any I/O testing yet, but I should have that done in a couple of days. I'll also make a version that strips out the game code and just leaves a stub that you'll be able to use for your own projects.

-1

u/JGhostThing 26d ago

If you intend to share the code, please do not use the picture of the code, but rather paste it as formatted text.

1

u/DocCarr 26d ago

Thanks for the feedback. I'm not sure on the relevant portion, but I did include a link to the Github for those who'd like to follow up. Is there something specific that you think would be worth calling out?