Main function

Okay, so I've been looking at some of the library and header files used by the Arduino and I found the Main() function, upon studying it sevral questions came to mind.

  1. If I edit the Main() file will it carry out code i put in, or likewise if i remove parts will it not carry out parts?

  2. Is it possible to call setup() or loop() from another function in the program?

  3. Can you call Main from another function?

  4. (depending on if the answer to #2 is yes) What would happen if you called setup() a second time and it had functions (such as lcd.begin()) that had already been called.

  5. (depending on if the answer to #1 is yes) Could you make it so that you the Arduino won't receive serial communication unless a pin (say analog In #5 (digital pin #17)) has a HIGH signal on it? moreover how would you do this, does the Main() function obey the same commands as setup() and loop()?

If you know the answers please just post them here and don't give me any links to external websites. if you don't know the answers please don't pretend like you do.

Thanks

just post them

don't give me any links

don't pretend

We will work on it.

  1. yes

  2. yes, but be aware of the consequences. Setup() and loop() are just functions, recursion is just around the corner.

  3. yes, Main() is just a function. But you will definitely get recursion and without a proper end condition and maybe some more you're pretty soon out of stack.

  4. that really depends on the individual functions in setup(), the timing and the state of almost every part in SW/HW
    in short that can only be deduced for specific code.
    e.g. some objects need a call to .end() before a call to .begin()

  5. yes, can be done. I would patch the HardwareSerial library I guess.

does the Main() function obey the same commands as setup() and loop()?

I do not understand what you mean. functions do not obey commands.

Hope this helpes,

One question back: What project do you have in mind that triggered these questions?

staressent:
Okay, so I've been looking at some of the library and header files used by the Arduino and I found the Main() function, upon studying it sevral questions came to mind.

  1. If I edit the Main() file will it carry out code i put in, or likewise if i remove parts will it not carry out parts?

Yes. It is recompiled whenever you hit the compile button.

  1. Is it possible to call setup() or loop() from another function in the program?

Yes, they are just functions like any other.

  1. Can you call Main from another function?

Again, yes, but you wouldn't want to.

  1. (depending on if the answer to #2 is yes) What would happen if you called setup() a second time and it had functions (such as lcd.begin()) that had already been called.

That depends entirely on the libraries. Some will just re-initialize themselves - setting IO pins to the state that they already are. Some will try and reinitialize the hardware they control, which might be fine, or might not.

  1. (depending on if the answer to #1 is yes) Could you make it so that you the Arduino won't receive serial communication unless a pin (say analog In #5 (digital pin #17)) has a HIGH signal on it? moreover how would you do this, does the Main() function obey the same commands as setup() and loop()?

main() is just a function. setup() and loop() are just functions. What you can put in one you can put in another - depending of course on scope and whether or not it's actually a good idea.

You wouldn't want to do that in main(). main() just sets everything up and gets it going. You'd probably want to tweak the HardwareSerial.cpp file to either ignore or accept serial data into the rx ringbuffer depending on the state of a pin.

In general there'd be no point in modifying main(), as main calls setup(), so just put your code in setup(). main() calls setup(), setup() calls your code.

The basic sequence for main() is:

  1. Set up internal system and objects.
  2. Call setup()
  3. Repeatedly call loop() followed by serialEvent() code.

Of course, some things happen even before main is called - any static objects (such as Serial, SPI, etc, plus any defined in the sketch) are created and their constructors executed.

staressent:

  1. If I edit the Main() file will it carry out code i put in, or likewise if i remove parts will it not carry out parts?

  2. Is it possible to call setup() or loop() from another function in the program?

  3. Can you call Main from another function?

  4. (depending on if the answer to #2 is yes) What would happen if you called setup() a second time and it had functions (such as lcd.begin()) that had already been called.

  5. (depending on if the answer to #1 is yes) Could you make it so that you the Arduino won't receive serial communication unless a pin (say analog In #5 (digital pin #17)) has a HIGH signal on it? moreover how would you do this, does the Main() function obey the same commands as setup() and loop()?

All of these are possible, but none of them are sensible.

The main() function does what is needed to initialise the Arduino runtime environment and make standard calls to your setup() and loop() function. I suggest you leave it alone.

Yes, setup() and loop() are just functions, and you can call them whenever you want. Since they form the interface that the Arduino runtime uses to call your code, it would probably be best if you didn't call them yourself though - whatever you're trying to achieve, there is almost certainly a better way of achieving it.

The semantics of setup() are to perform one-off initialisation. Typically, libraries that require one-off initialisation have corresponding functions which are intended to be called from setup(). People implementing those libraries may not have considered the possibility that you would call the initialisation functions multiple times so in general it is not a good thing to do, although it may happen to be OK with some libraries.

If you don't want to process serial input until an input pin goes high, write your sketch so it doesn't process the serial input until the input pin goes high. There's no magic involved, and no need to do unusual things with main(), setup() or loop() - just write your code in setup() and loop() and your functions called from them to provide the behaviour you want.

staressent:
Okay, so I've been looking at some of the library and header files used by the Arduino and I found the Main() function, ...

If you are going to program in C you may as well get the capitalization right. It is main() not Main().

  1. (depending on if the answer to #1 is yes) Could you make it so that you the Arduino won't receive serial communication unless a pin (say analog In #5 (digital pin #17)) has a HIGH signal on it? moreover how would you do this, does the Main() function obey the same commands as setup() and loop()?

Editing main() is not the way to go about whatever-it-is you are wanting to do.

Thank you for all the input and as for your suggestions and advise it is duly noted,but as for my ideas i'm sure that there is a way to do it through setup() and loop() and mostly why I was asking was academic rather than application. Still I can see some advantages for going through main();

If you are going to program in C you may as well get the capitalization right. It is main() not Main().

Oh and Mr gammon Mr. Gammon there is no need to be so critical of my capitalization usage so I would thank you to lay off it in the future. And just so you know I am a perfectly fine programmer even if I'm not the best.

Oh, sorry.

critical of my capitalization usage

You are taking that totally out of context!
C++ is a case sensitive language - if you're going to talk the talk, you need to use proper terminology!

LAY OFF!

staressent:
LAY OFF!

Arrogance like that is going to win you a lot of friends arround here. You ask a question so by definition you don't know. So you lay off being so ungreatful.
No doubt I am going on your black list. You are going on mine.

I am not ungrateful; I have gladly accepted the answer to my question and was all ready to be done with this thread. What I'm so upset about is the fact that people (two Global Moderators no less) are criticizing my punctuation. I did not come here to ask about capitalization in programming so therefor telling people to lay off is not ingratitude, it is an expression of annoyance.
and if your going to black list me Mike, over something so trivial as Main() and LAY OFF then so be it.

Now my question has been answered so i'd thank you all to stop.

Thank you.

Oh and to the Global Moderators many forums have buttons the original poster can click to mark their problem as solved and prevent further posting on a thread, you may consider looking into adding such a button so that threads like this don't occur.

A very large audience reads these threads ex post facto. By correcting your mistake, someone reading this thread will know that Main is not correct. One very important purpose of this forum is to discuss and spread correct technical content. Not just to you but also to future readers.

In addition, Nick corrected your mistake to help you avoid problems in case you do decide to implement your own main.

Please try to show some empathy for others who will read this thread.

Okay Coding Badly duly noted, and thank you for putting it in a reasonable manor and not just calling me on it. I will do better in the future.

Thank you.

you may consider looking into adding such a button

You are responsible do that by adding RESOLVED etc. to your subject line.

Oh, okay.

Still I can see some advantages for going through main();

Well, please just post them here and don't give us any links to external websites.
If you don't see the advantages, please don't pretend you do.

Well, please just post them here and don't give us any links to external websites.

Don't worry i'll keep my ideas to myself!
And as for some advantages, yes I do see some, thank you very much!

Ah! Someone else who doesn't get irony. :stuck_out_tongue_closed_eyes:

Oh ha ha very funny!
You know just because someone makes a small mistake or misinterpretation doesn't give you any right to harp on them about it. All these endless comments are stating to bug me and I'd really appreciate if you stop.

NOW FOR THE LAST TIME LAY OFF!