Blink an LED within a loop

Hi,

I am currently playing with my photon and I have the idea that I want to blink an LED, easy enough :wink:

but how do I do this within an existing loop?:

void loop() {
}
        
int setStatus(String status) {
  if(status == "DoNotDisturb"){
    RGB.color(0, 0, 255);
    digitalWrite(ledR, HIGH);
    // Well the LED should be blinking 
    digitalWrite(ledG, LOW);
    digitalWrite(ledY, LOW);
  }
  else if(....

any Ideas (I assume I am blocked somewhere, the question looks so stupid) or is this just not possible, I cannot put in another loop (as it will of course be a loop of death :slight_smile: )

Regards,

Jan

@sixpack

you could try something like this, allowing you to set which led you want to blink in your RGB led:

  void loop()
  {
    if(status == "DoNotDisturb")
    {
      blinkRGB(true, false, false, 250);
    }
  }
  
  blinkRGB(bool red, bool green, bool blue, int rate)
  {
    static unsigned long lastBlinkTime = 0;
    if(millis() - lastBlinkTime > rate)
    {
      if(red)
        digitalWrite(redPin, !digitalRead(redPin);
      else
        digitalWrite(redPin, LOW);
      if(green)
        digitalWrite(greenPin, !digitalRead(greenPin);
      else
        digitalWrite(greenPin, LOW);
      if(blue)
        digitalWrite(bluePin, !digitalRead(bluePin);
      else
        digitalWrite(bluePin, LOW);  
      lastBlinkTime = millis();
    }
  }

a down and dirty way…

1 Like