Pages: [1]   Go Down
Author Topic: LED is on after program ends  (Read 426 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I presume you are overwriting the existing declaration of main as:
Code:
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:
Code:
 digitalWrite(ledPin, HIGH);

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

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

  while(1); //end the program
}

or even this (if you want to save power):
Code:
#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.
}

« Last Edit: August 12, 2012, 12:40:40 pm by Tom Carpenter » Logged

~Tom~

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: