LED Blink & Fade Threads w/o Delay?

Hi everyone,

I know the “no Delay()” topic has been opened again and again, and I have read though all of them. Sorry to say but it just did not make a click for me.

I am exporting my code to an ATtiny85 and would like to combine the following:

  • 1 LED that fades on for a second, fades off, stays off for a half a second and then over again
  • 1 LED (Double-blink) blinks on for about 50ms, about 50ms off, blinks back on, and then off for about half a second
  • 1 LED (Blinking) 1 second on, 1 second off
  • 1 LED (Blinking) half a second on, half a second off

Here is a code that does a blinking and fading combo, but not quite like I am looking for…

int ledPin = 2;		     // LED connected to digital pin 13
int fadePin = 1;


int PWMunused = 0;                // PMW pin (Unused)
int PMWnav = 1;                   // PMW red/green NavLights
int LEDStrobe = 2;                // Blinking Strobe NaveLight
int LEDconstant = 3;              // Constant on Lights
int LEDdocking = 4;               // Docking Lights

unsigned long nextBlink, nextFade;
int fadeBrightness = 0;
char ledState = LOW, fadeDir = 10;

void setup()
{
    pinMode(ledPin, OUTPUT);	// sets the digital pin as output
    pinMode(fadePin, OUTPUT);
    
    pinMode(PWMunused, OUTPUT);      // sets the digital pin as output
    pinMode(PMWnav, OUTPUT);
    pinMode(LEDStrobe, OUTPUT);
    pinMode(LEDconstant, OUTPUT);
    pinMode(LEDdocking, OUTPUT);
    
    ledState = LOW;
    nextBlink = millis();
    nextFade = millis();
    
    digitalWrite(LEDconstant, HIGH);   // turn the constant lighting on
}



void blink(void)
{
    unsigned long now;
    now = millis();
    if (now >= nextBlink) {
	  nextBlink = now + 500;    // Next change in one second
	  if (ledState == LOW) {
		digitalWrite(ledPin, HIGH);
		ledState = HIGH;
	  }
	  else {
		digitalWrite(ledPin, LOW);
		ledState = LOW;
	  }
    }
}

void fade(void)
{
    unsigned long now;
    now = millis();
    if (now >= nextFade) {
	  nextFade = now + 25;    // Next change in one second
	  if (fadeDir > 0) {
		if (fadeBrightness > (255 - fadeDir)) {
		    fadeDir = - fadeDir;
		}
	  }
	  else {
		if (fadeBrightness < (-fadeDir)) {
		    fadeDir = - fadeDir;
		}
	  }
	  fadeBrightness += fadeDir;
	  analogWrite(fadePin, fadeBrightness);
    }
}

void loop()
{
    blink();
    fade();
}

I know there are some lines in the code that have no effect, they are leftovers from cuting & pasting and trying this and that.

The main problem (as I see it at lease) is that I do not really understand how to create multiple delays using the Timer method described in the Playground (AvoidDelay).
Thank you for any help or advice

An alternative way to do what you want is in the Multi_Blink example. You may need to add data to the data table for the fading, but the rest is already happening in the code with minor tweaks.

I would use some of these libraries http://arduino.cc/playground/Code/Metro http://arduino.cc/playground/Code/SimpleTimer

I would implement this in two parts.

1) For the fading I would use PWM. The Mega85 seems to have two PWM pins.

2) For the Blinking I would use interrupts every 50ms. For an example have a look here http://blog.blinkenlight.net/experiments/measurements/power-grid-monitor/. During the itnerrupt I would a) toggle one of the LEDs b) proceed a counter, if the counter equals 0 or 10 I would toggle another LED c) if the counter reached 10 I would toggle the last LED and reset the counter to -10 d) I would set the duty cycle of the PWM as the absolute value of the counter.