main()

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?

void setup() {

}

I'm pretty sure it's possible to override the implicit main() function that the Arduino IDE supplies. Try a little Goggle searching for that.

Which board do you have selected in the Tools > Board menu?

Arjun1212:
void setup() {

}

What the heck are we supposed to do with that?

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().

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.

void setup() {
pinMode(11, OUTPUT);
pinMode(5, OUTPUT);

}

int main() {

return 0;
}

void loop() {
digitalWrite(11, HIGH);
digitalWrite(5, HIGH);
}

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().

Should have the same effect for the normal setup() and loop().

If you actually need any hardware to work, it would be a good idea to call init() before setup().

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(;:wink:
  {
      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 :slight_smile:

And next the lecturer wants to know what init() does

And where it is implemented.

MOderator merged topics. Please keep one topic in one thread.

PaulS:
And where it is implemented.

Freakin' hell, took me an hour to find it :frowning: 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.