LED is on after program ends

taking the Blink exercise, it works perfectly.
modifying it from void loop() to int main() the LED is allways ON.
??? WHY ???


// set LED pin number:
const int ledPin = 13; // the number of the LED pin

void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}

int main() {
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW); // I do not even see that the LED goes of
delay(2000);
} // it still keeps ON


arduino 1.0.1
duemillanove

I presume you are overwriting the existing declaration of main as:

int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

which would mean setup() isn't called, so you wouldn't be setting the pin to an output - meaning that you are seeing the LED as being on only because of the internal pullup resistor being enabled by digitalWrite(pin,HIGH).

It also means that init() is not being called, which means the micros() timer is not being setup, which means that delay() never counts meaning the program will freeze after:

  digitalWrite(ledPin, HIGH);

Why are you trying to use main() instead of loop()? If you want the program to end just do this:

void loop(){
  //you code
  ...
  ...
  ...

  while(1); //end the program
}

or even this (if you want to save power):

#include <avr/sleep.h>
void loop(){
  //you code
  ...
  ...
  ...
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); //least power consumption version of sleep.
  sleep_enable(); //enable the ability to sleep.
  cli(); //disable interrupts to prevent the avr from waking back up.
  sleep_mode(); //put to sleep. no interrupts, so a power cycle would be required to wake it.
}

You can't use a user defined int main() function as the arduino IDE already inserts a hidden main() function to wrap around your startup and loop sketch functions prior to compilation.

Lefty