Offline
Newbie
Karma: 0
Posts: 5
|
 |
« on: December 28, 2012, 07:30:40 am » |
Hello! I am new to the world of Arduino. And I like it. I have some AVR assembly done more then 10 years ago.
But I like Arduino, because I must not do all the work for all the common things like LCD and so on. I had running displays and stepper motors within minutes. My first LC-Display driver in assembler (10 years ago) took me several days until it worked.
Now I can concentrate on my programms and not all of the details, that i can do but are boring to do.
Now here comes my question: Is there somewhere a good source of how to organize larger Arduiono projects? I found a lot of tutorials for small programms but nothing about this problems. I know I could make libraries. But thiss is pure C++, and I would like to stay in the world of normal sketches. How can i organize things like: declaring hardware pins, initialize display, get all the hardware in a controlled start-state. And so on. If I divide my programm in several files. In which order are the compiled. What is with inkluding libraries that I need in different files?
Thanks in advance for any help Jan
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #1 on: December 28, 2012, 07:39:55 am » |
If I divide my programm in several files. In which order are the compiled. It doesn't matter. All the individual files are compiled, producing object files. Then, the linker is called to link them together. It's a multipass linker, so the order of the files presented to it is irrelevant. What is with inkluding libraries that I need in different files? I'm not sure that I understand the question. It's a useful technique is all I can say.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 9
Posts: 1000
|
 |
« Reply #2 on: December 28, 2012, 09:02:41 am » |
Start with a blinking led, and try other examples. You will find the answers along the way.
Arduino use C++, but also the AVR C-code can be mixed with it.
You can create new C++ files (use just a name) and also header files (name them as *.h) to your project. Use the drop-down on the right of the tab bar in the Arduino IDE. You can even copy *.c and other *.h files in the folder of your project.
To use a library, the library must be present, and the include file must be included.
You don't have to include your own include files. But sometimes you do. If the compiler uses a wrong order, you can also include your own include file in the first sketch.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 73
Posts: 6638
Arduino rocks
|
 |
« Reply #3 on: December 28, 2012, 12:26:55 pm » |
You can break your code up into several .h files and #include them from the main sketch, simple but helps keep different bits of code separate, and allows simple cut/paste reuse in other sketches.
But often you want to properly package some stuff (a driver for a particular chip), and the library is the natural way to do this - in particular you don't need to copy it to use in another sketch, and it is the sensible way to share code with others to use/improve/extend.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #4 on: December 28, 2012, 12:34:52 pm » |
Thanks for the answers. But that was not really my question. I try again to make it clear.
I know how to blink an led. I know how to use (and write my own) library. What I talk about is a really large project (A controler for a special CNC-machine) with: - more then 10 steppers and servos. - other subsystems with serial connenction - a touchscreen with UI (menues and so on) - Lot of switches and sensors - On my Mega 2560 all IO-Pins are in use - ..........
So we talk about more than a few tousend lines of code. At startup everithing has to be initialised in the rigth order and the correct timing.
In other languages I would put things in classes to get it done.
How do you organize such a project with Arduino.
Thanks again Jan
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #5 on: December 28, 2012, 01:46:00 pm » |
In other languages I would put things in classes to get it done.
How do you organize such a project with Arduino. Why are you assuming that you would do something different because it's an Arduino? You wouldn't.
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Offline
Edison Member
Karma: 9
Posts: 1000
|
 |
« Reply #7 on: December 28, 2012, 04:43:33 pm » |
Isn't that the same as what we wrote ?
You have to think about how to split in different parts and add files to your project according to that, and give them proper names. About 20 or 30 files is no problem. But if you have 50 files, you could need a library.
I normally would make files like this: Menu, Menu_helper, Display, Keys, and so on. The 'helper' files are usefull functions. Sometime I have also files with _glue, with functions to 'glue' the different parts together.
This is just normal programming. So I still don't know why there is a problem.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #8 on: December 28, 2012, 07:51:58 pm » |
You can make a "new tab" in the IDE. Those tabs can contain .cpp and .h files. You can break up your project as much as you like into separate files. You can use classes. Just make sure that the main sketch (the .ino file) has an #include for each .h file so that the compiling system knows to include them in the building process.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: December 28, 2012, 07:52:48 pm » |
At startup everithing has to be initialised in the rigth order and the correct timing.
Well then, make sure that, in the setup function, you initialize things in the right order.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #10 on: December 29, 2012, 05:31:58 am » |
Thanks for all the answers. Some of them are helpfull.
But to say "its normal programming, where is the problem? You will find the answers along the way. and so on ....." is not very helpfull. If I had no problem I would not ask.
I know how to programm in more than 10 languages. Not all active. Modula2 or Pascal is no longer state of the art.
But I am new to Arduino. I like it. Things in Arduino are a little bit different. So even if I know how to organize Files in C++ or Java, I asked if there is a good tutorial for Arduino how to organize projects.
Now I have found what I have searched for.
Thanks Jan
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Online
Brattain Member
Karma: 277
Posts: 25569
Solder is electric glue
|
 |
« Reply #11 on: December 29, 2012, 05:59:01 am » |
Things in Arduino are a little bit different. No not really, at the end of the day it is just C++ so do it like any C++ project.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #12 on: December 29, 2012, 06:55:49 am » |
If you already know OOP, it's easy: two tabs per class, one .h one .cpp. One .ino tab only, the main sketch, Just remember to include all the .h files in the .ino too, even if they're already included in some class .h file.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #13 on: December 29, 2012, 08:26:13 am » |
If you already know OOP, it's easy: two tabs per class, one .h one .cpp. Sure, but the key is to know that you can create additional tabs. That is not all that apparent from the crappy icons on the IDE. Nor is it obvious, or well documented, what creating new tabs means. Of course, one would think that by the time the project has grown to the size where multiple tabs are going to be helpful, one would have become confident enough to experiment with all the buttons and menu items, and become familiar with the results.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #14 on: December 29, 2012, 08:34:37 am » |
Sure, but the key is to know that you can create additional tabs. That is not all that apparent from the crappy icons on the IDE. Yes, that's a key point.
|
|
|
|
|
Logged
|
|
|
|
|
|