New to programming and Arduino

by Hookster on Sun May 12, 2013 12:22 pm

I,m trying a small project with the blinking led code, how would i add a timer so the led blinks say for 30 sec? I will be triggering it with a photo cell and a laser pointer.
Hookster
 
Posts: 1
Joined: Sun May 12, 2013 12:06 pm

Re: New to programming and Arduino

by tldr on Sun May 12, 2013 3:30 pm

ok. so assuming you get ledPin and CDSPin defined and initialized in startup and get CDSTHRESHOLD defined to a reasonable value, this might work. depending on whether you want the led to blink on a make or break of the light circuit, you may have to change the comparison on the analog read.

the definition of BLINKPERIOD will give you a blink rate of 1/2 second on and 1/2 second off. the definition of BLINKTIME will get you thirty seconds of blinking.

Code: Select all | TOGGLE FULL SIZE
#define BLINKPERIOD 500
#define BLINKTIME 30000

long stopTime, blinkPeriod;
uint8_t blinking, ledState;

void loop (void) {
  if (blinking) {
    if {millis() >= stopTime) {
      blinking = 0;
      ledState = 0;
      digitalWrite (ledPin, 0);
      blinkTime = 0;
    }
    else {
      if (millis() >= blinkTime) {
        ledState = ~ledState;
        digitalWrite (ledPin, ledState);
        blinkTime = millis() + BLINKPERIOD;
      }
    }
  }
  else {
    if (analogRead (CDSPin) > CDSTHRESHOLD) (
      blinking = 1;
      stopTime = millis() + BLINKTIME;
    }
  }
}
"If I had known it was harmless, I would have killed it myself." - Phillip K. Dick, A Scanner Darkly
User avatar
tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 12:34 am