Cleaning up code

Hi,

I need some advice on how to clean up my code.

I currently have about 1500 lines of code, most of it is handled by functions.

Can I get those functions out of the main code and add them somewhere else? For example with Microchip/Atmel compilers I can have them in a separate file and include them on the beginning.

How would I do something similar on the Arduino IDE?

Regards

On the top-right is a drop-down menu. You can add files to your project there. You don't have to include them, they are part of the project.

Thank you.

What extension must the file have (.ino .txt)?
I was trying to create a new one, but as it appears the compiler only lets me add them.

It automatically sets them to .ino

Second what Peter said.

Careful about the "not including" them part though, if you understand what that means.

Had code that I wanted to port to the spark core and because I spared no thoughts toward dependences and had many of these "tabbed files" it became a nightmare.

If you know the proper way stick with it, just include the file extentions in the name if that sounds like giberish... well... you'll figure it out if necissary. Tab away!

Complexity breeds failure, small code is happy code! godspeed, Paul

[quote author=Paul Beaudet link=topic=269240.msg1897998#msg1897998 date=1411760621] Second what Peter said.

Careful about the "not including" them part though, if you understand what that means. [/quote]

Thanks Paul,

Sorry that wasn't very clear, does the IDE associate them in a way I can't undo later? What would you recommend as the best way?

Regards

Create a file without extension, it will also become a *.ino file. Move a lot of function into those file. You can always move them back into the main project file.

If you want to, you can also create *.cpp and *.h files.

casemod: Sorry that wasn't very clear, does the IDE associate them in a way I can't undo later? What would you recommend as the best way?

Regards

Its more an issue if you want to port the code later with multiple .ino file to another controler doing things the traditional way.

Say you have main.ino buttons.ino pagers.ino convertions.ino and those files are interdependent on one another, because of some odd function shared in between pagers and buttons with convertions.

The way the IDE sees it you can use function where ever you want with the files being child to the main sketch and eachother. Situations outside the Arduino enviornment need to define dependencies with #include or the compiler will see no relation as to where the function is supposed to come from.

Probably a bad explaintion, unsure the scope of your project or ambition. Might be more then you need to understand.

I should note porting code arduino to arduino will be okay to avoid confussion. Some of the arduino "compatibles" though that use a differant IDE suffer a lack of compatibilty if you have writen code in the way I explained above.

Thanks Paul. that cleared it up.

My code and libraries are written low level for the ATMega2650, therefore even if I wanted to use another arduino I would have to port it, hence the question. I should be using AVR studio long ago, just to lazy ;)

On the other hand having separate function files comes handy. For example if I need to use a particular IC such as a battery monitor or a digital pot I can just copy the relevant file, like a library, and call the function from the main loop without having to look it in endless code.

That is what I do. When I see a library I can use, but I want to adapt it, I copy the *.cpp and *.h file into my project. The #include <something.h> needs to be changed into #include “something.h”, and perhaps I have to change a few includes. After that, those files are part of my project files.

When you go to the project folder, you can see all those file. You can also copy files into that folder. When you start the Arduino IDE, the *.ino, *.cpp, *.h will open in tabs.

Thanks Peter.

I've been playing with this it turns out its really nice. For example, yesterday I added all the RFID dependencies to one file, including temp files, array initialization etc. This means, whenever i need to copy the RFID stuff to other project I just grab that particular file and i'm good to go ;)