for my college project I am writing code for a programmable robot however my lecturer is persistant on the fact the I use a main() function. so i attempted to include it, their was no syntax error however once i downloaded the code to the robot the code the previously worked stopped working. can someone please explain why this could have possibly happened?
The IDE-added main() does some other stuff, like checking for serial data on each of the Serial objects, and calling a serialEvent() callback if one is defined, but that is not necessary. Handle serial data in loop().
im trying to add a main() function into my code. their is no syntax error showing however when I try to run the program on the arduino it will not allow for the code to work which without a main() did work. my lecturer is persistant on their being a main() within the code. can someone please help with this.
When you have a main() function, you override the one that the IDE adds. You must make your main function do everything that the one that the IDE adds does.
In normal C++ programs the main() is the function that's run automatically, it's the starting point of your program. The compiler simply looks for that name, and then creates the appropriate code.
In Arduino's IDE implementation it's a bit different. An Arduino sketch uses the setup() and loop() combo, and doesn't need a main() function.
So your lecturer is correct when it comes to regular C++ programming (maybe he never really worked with the Arduino IDE?), and I'm quite sure you can make it work on Arduino as well. If you must, try adding a main() like this:
int main() {
setup();
while (true) loop();
}
Should have the same effect for the normal setup() and loop().
PaulS:
The Arduino IDE adds a main() function, if you don't have one. The one that is added calls init(), setup() and then calls loop() in an endless loop.
int main()
{
init();
setup();
for(;
{
loop();
}
return 0;
}
The IDE-added main() does some other stuff, like checking for serial data on each of the Serial objects, and calling a serialEvent() callback if one is defined, but that is not necessary. Handle serial data in loop().
And next the lecturer wants to know what init() does
Freakin' hell, took me an hour to find it And I have digged that file a couple of times in the past. Must probably pay better attention to what grep gives me. Found some other things, so it was not wasted time.