Memorial day is 05/25/2015 – Orders will ship out on May 26th. There will not be any deliveries or shipping on Monday. Any order placed after 11am ET on Friday May 22nd will not start to ship out until Tuesday May 26th.

New to programming and Arduino
Moderators: adafruit_support_bill, adafruit

New to programming and Arduino

by Hookster on Sun May 12, 2013 1: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.
Posts: 1
Joined: Sun May 12, 2013 1:06 pm

Re: New to programming and Arduino

by tldr on Sun May 12, 2013 4: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
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am