fade without delay?

Hoho! I'm trying to fade led in and out without using delay. I have checked the blinkwithoutdelay and fading examples but could not figure out anything working :cry: I,m using Arduino Duemilanove with ATmega328. also i tried using LED libs that i found on the website but could not get the result i wanted.

#include <LED.h>


int ledPin=9;

int ledState=0;
long previousMillis=0;

long interval=1000;

void setup()
{
}

void loop()
{
  if(millis()-previousMillis>interval)
  {
    previousMillis=millis();
  
  if (ledState==0)
  ledState=fadeIn(1000);
  else
  ledState=fadeOut(1000);
  
  analogWrite(ledPin,ledState);
  }
}

I have to ask. Why do you need the LED to fade without a delay?

The reason your code may not be working is your not using the library quite right. You need to declare the LED pin in the way the library wants it. Otherwise it can't perform the actions you want. You also need to use the fade commands correctly.

I'm at work right now so i can't run this to test it but see if this works.

#include <LED.h>

LED led = LED(9);



void setup()
{
  led.off();
}

void loop()
{
  if (led.getState()== true)
  {
  led.fadeIn(1000);
  }
  else
  {
  led.fadeOut(1000);
  }
}

You need to declare variable "led" as a LED object and then use the function within LED object to modify the object itself. Which is done by OBJECTNAME.FUNCTIONNAME and in your case led.fadeIn(int) like digimike has mentioned.

I have to ask. Why do you need the LED to fade without a delay?

i want two leds to fade in/out at the same time but at diffrent speed. delay messes it up so that they work one after another at diffrent speed or they work at the same time at the same speed...

and to digimikes code: it looks much better than mine :slight_smile: but it lights up instantly and fades only out..

#include <LED.h>

LED led = LED(9);



void setup()
{
  led.off();
}

void loop()
{
  if (led.getState()== false) //changed this to false, now it works
  {
  led.fadeIn(1000);
  }
  else
  {
  led.fadeOut(1000);
  }
}

still cant get them work at the same time though:(

Well the code is only addressing a single LED. Have you added a second LED to it?

Once again i'm at work so can't test it but see how this goes.

#include <LED.h>

LED led[1] = LED(9);
LED led[2] = LED(11);

void setup()
{
  led[1].off();
  led[2].off();
}

void loop()
{
  if (led[1].getState()== false && led[2].getState()== false)//changed this to false, now it works
  {
    led[1].fadeIn(1000);
    led[2].fadeIn(1000);
  }
  else
  {
    led[1].fadeOut(1000);
    led[2].fadeOut(1000)
  }
}

It may not fade the 2 LEDs at the same time. It all depends on how the library is set up. Weather or not the library will be able to fade 1 LED a step then the next and repeat. Or if it has to fade the first LED completely out before moving on to the second LED.

i tried to add second led mysef but it didn't work.. when trying your code it fails compilation saying: error: conflicting declaration 'LED led [2]

also you forgot to put ; after led[2].fadeOut(1000) :wink:

EDIT:
i took the [ ] thingys away from led1 and led2
now it compiles the code ok but they light up one after another, not at the same time

#include <LED.h>

LED led1 = LED(9);
LED led2 = LED(10);

void setup()
{
  led1.off();
  led2.off();
}

void loop()
{
  if (led1.getState()== false && led2.getState()== false)//changed this to false, now it works
  {
    led1.fadeIn(1000);
    led2.fadeIn(1000);
  }
  else
  {
    led1.fadeOut(1000);
    led2.fadeOut(1000);
  }
}

methinks you might be looking for something like this:

SoftPWM Library

The fading is autonomous. See the examples.

Try it out and see if that is what you're looking for.

b

That would be a limitation of the library. It will be hard to get them both to fade at the same time. A while back there was a guy who wanted to push a button to make one LED fade then push another one to get a second one to fade while the first was in the middle of its fade. Don't think he was ever able to pull it off. You may have to manually program each step of the fading proses. You may have to do something like this.

int ledPin1 = 9;
int ledPin2 = 10;
int fadeval1 = 0;
int fadeval2 = 0;
boolean fader1 = true;
boolean fader2 = true;


void setup()
{
  analogWrite(ledPin1, fadeval1);
  analogWrite(ledPin2, fadeval2);
}

void loop()
{
  if (fadeval1 < 255 && fader1 == true)
  {
    fadeval1 = fadeval1 + 5;
    if (fadeval1 >= 25)
    {
     fader1 = false;
    } 
    analogWrite(ledPin1, fadeval1);
  }
  
  if (fadeval2 < 255 && fader2 == true)
 {
   fadeval2 = fadeval2 + 5;
   if (fadeval2 >= 255)
   {
     fader2 = false;
   }
   analogWrite(ledPin2, fadeval2);
  }
  
  if (fadeval1 > 0 && fader1 == false)
  {
    fadeval1 = fadeval1 - 5;
    if (fadeval1 <= 0)
    {
     fader1 = true;
    } 
    analogWrite(ledPin1, fadeval1);
  }
  
  if (fadeval2 > 0 && fader2 == false)
 {
   fadeval2 = fadeval2 - 5;
   if (fadeval2 <= 0)
   {
     fader2 = true;
   }
   analogWrite(ledPin2, fadeval2);
  }
}

Its not as clean and efficient as using a for() statement but it doesn't lock you into one LED at a time fading. You can see the basic idea here. If you want it to fade faster you can change that but you might have to play around with it a bit so it will work right. It would be best to keep the increments that can be evenly divided by 255.

Re: fade without delay?
Reply #6 - Today at 22:30:57 |
methinks you might be looking for something like this:

SoftPWM Library

That library is great for making non PWM pin act like they have PWM. But i don't see how it would help in fading 2 LEDs at the same time.

Try this sketch. Pin 9 and 10 fade at different rates, at the same time.

#include <SoftPWM.h>

void setup(void)
{
  SoftPWMBegin();

  // Create on pins 9 and 10
  SoftPWMSet(9, 0);
  SoftPWMSet(10, 0);

  // Set full scale fade time of pin 9 (up in 500 ms, down in 1000 ms)
  SoftPWMSetFadeTime(9, 500, 1000);
  // Set full scale fade time of pin 10 (up in 200 ms, down in 500 ms)
  SoftPWMSetFadeTime(10, 200, 500);

  SoftPWMSet(9, 255);
  SoftPWMSet(10, 255);

  delay(1000);  // we could do other things here instead of delay()

  SoftPWMSet(9, 0);
  SoftPWMSet(10, 0);

  delay(1000);
}

void loop(void)
{
}

That's a perfect solution. Thanks

To bhagman's code: it has two problems 1. is that it runs only once, no loop.. 2. is that the leds fade in at the same speed eaven after i modified the fade in delays to 1000 and 100

Sorry hevitikku - it was just a one shot demo for digimike.

Here's how you can loop it over and over again.

#include <SoftPWM.h>

void setup(void)
{
  SoftPWMBegin();

  // Create on pins 9 and 10
  SoftPWMSet(9, 0);
  SoftPWMSet(10, 0);

  // Set full scale fade time of pin 9 (up in 500 ms, down in 1000 ms)
  SoftPWMSetFadeTime(9, 500, 1000);
  // Set full scale fade time of pin 10 (up in 200 ms, down in 500 ms)
  SoftPWMSetFadeTime(10, 200, 500);
}

void loop(void)
{
  SoftPWMSet(9, 255);
  SoftPWMSet(10, 255);

  delay(1000);  // we could do other things here instead of delay()

  SoftPWMSet(9, 0);
  SoftPWMSet(10, 0);

  delay(1000);
}

With regards to the 2nd problem you're seeing, can you post your code so I can see what you changed?

b

well in the latest code, problem is that the led at the pin 9 doesent fade out completely.it fades out but in the middle of fading out it starts to fade in again..

2:nd problem code

#include <SoftPWM.h>

void setup(void)
{
  SoftPWMBegin();

  // Create on pins 9 and 10
  SoftPWMSet(9, 0);
  SoftPWMSet(10, 0);

  // Set full scale fade time of pin 9 (up in 500 ms, down in 1000 ms)
  SoftPWMSetFadeTime(9, 1000, 1000);
  // Set full scale fade time of pin 10 (up in 200 ms, down in 500 ms)
  SoftPWMSetFadeTime(10, 100, 500);

  SoftPWMSet(9, 255);
  SoftPWMSet(10, 255);

  delay(1000);  // we could do other things here instead of delay()

  SoftPWMSet(9, 0);
  SoftPWMSet(10, 0);

  delay(1000);
}

void loop(void)
{
}

Well, hevitikku, I'm afraid your eyes are playing tricks on you. If you have both LEDs close together, it will appear as though both LEDs turn on instantaneously because our eyes have a logarithmic response to light. If you separate the LEDs, by say about 3 or more inches, you will see a vast difference.

Allow me to demonstrate. I will increase the delays to overemphasize the situation.

#include <SoftPWM.h>

void setup(void)
{
  SoftPWMBegin();

  // Create on pins 9 and 10
  SoftPWMSet(9, 0);
  SoftPWMSet(10, 0);

  // Set full scale fade time of pin 9 (up in 2000 ms, down in 2000 ms)
  SoftPWMSetFadeTime(9, 2000, 2000);
  // Set full scale fade time of pin 10 (up in 100 ms, down in 100 ms)
  SoftPWMSetFadeTime(10, 100, 100);
}

void loop(void)
{

  // Fade the LEDs at the same time
  SoftPWMSet(9, 255);
  SoftPWMSet(10, 255);

  delay(4000);  // we could do other things here instead of delay()

  SoftPWMSet(9, 0);
  SoftPWMSet(10, 0);

  delay(4000);

  // Now fade the LEDs separately
  SoftPWMSet(9, 255);
  delay(4000);
  SoftPWMSet(9, 0);
  delay(4000);

  SoftPWMSet(10, 255);
  delay(4000);
  SoftPWMSet(10, 0);
  delay(4000);
}

If you still aren't convinced that the LED on pin 9 fades in over 2 seconds, try removing the LED on pin 10 while the code is running and see the difference.

Your eyes are deceiving you :slight_smile:

b

Well it seems to be close enough to what i wanted. Thanks for help everyone!!!! :slight_smile: