Continuous Blinking Led

Hey guys

I am wondering if i can have a continuous blinking led while my main sketch is running.

Thanks

Ian

Yes you can since you program it.

What is your project?

See blink without delay and the look up Finite Sate Machines (FSM) in the playground/examples.

Mark

Yes, but probably not in the way you are thinking. That is, the LED cannot just blink by itself. However, you also don't need to put a delay in your main loop (for the blinking LED)

Try something like this:

int led = 13;
bool ledOn = false;
int blinkRate = 500; // blink every 1/2 second
unsigned long blinkTimer;

void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  blinkTimer = millis();
}

void loop() {

// include this call anywhere in your main loop
  BlinkMyLed();
}

void BlinkMyLed() {
  if (millis() + blinkTimer > blinkRate) {
    blinkTimer = millis();
    digitalWrite(led, ledOn);
    ledOn = !ledOn;
  }
}

ianscott-arduino:
Hey guys

I am wondering if i can have a continuous blinking led while my main sketch is running.

an

if (millis() + blinkTimer > blinkRate) {

you will want to stay away from addition with unsigned long math, you will have rollover/overflow problems...

a better approach would be subtraction...

int ledPin = 13;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  blinkMyLed();
}

void blinkMyLed()
{
  static unsigned long lastBlinkTime = 0;
  if (millis() - lastBlinkTime >= 50UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin)); //toggles the led
    lastBlinkTime = millis();
  }
}