Go Down

Topic: LED is on after program ends (Read 521 times) previous topic - next topic


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);
  digitalWrite(ledPin, LOW);   // I do not even see that the LED goes of
}                                          // it still keeps ON

arduino 1.0.1

Tom Carpenter

Aug 12, 2012, 07:33 pm Last Edit: Aug 12, 2012, 07:40 pm by Tom Carpenter Reason: 1
I presume you are overwriting the existing declaration of main as:
Code: [Select]
int main(void)

for (;;)
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:
Code: [Select]
 digitalWrite(ledPin, HIGH);

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

Code: [Select]

void loop(){
 //you code

 while(1); //end the program

or even this (if you want to save power):
Code: [Select]

#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.


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131