New to programming and Arduino

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Hookster
 
Posts: 1
Joined: Sun May 12, 2013 1:06 pm

New to programming and Arduino

Post by Hookster »

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.

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: New to programming and Arduino

Post by tldr »

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

#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;
    }
  }
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”