Fade a LED only one time.

Hi I want to fade a LED from 0-255 ONLY ONE TIME. I found this but it runs back and forth fading down-fading up. I can't figure out how to fade up just ONE single time. I don't want to use a delay.

int value ;
int ledpin = 9;                           // light connected to digital pin 9
long time=0;

int periode = 2000;
int displace = 30;

void setup() 
{ 
 Serial.begin(9600);
} 

void loop() 
{ time = millis();
 value = 128+127*cos(2*PI/periode*(displace-time));

analogWrite(ledpin, value);           // sets the value (range from 0 to 255) 
 Serial.println(value);

}
boolean already_done  = false;

if (!already done)
{
  // do your stuff
  already_done = true;
}

Or do the fade up as part of setup()

hi again and thanks, that looks sense marco_c but i can't get it running. Or maybe i have to put it in a new "Void test"?

int value ;
int ledpin = 9;                           // light connected to digital pin 9
long time=0;

int periode = 2000;
int displace = 30;

void setup() 
{ 
 Serial.begin(9600);
 boolean already_done  = false;

} 

void loop() {
if (already_done=false)

 time = millis();
 value = 128+127*cos(2*PI/periode*(displace-time));
analogWrite(ledpin, value);           // sets the value (range from 0 to 255) 
 Serial.println(value);
already_done = true;
}

I believe what Crossroads was saying was, place the fade sequence in the setup() section like

int value ;
int ledpin = 9;                           // light connected to digital pin 9
long time=0;

int periode = 2000;
int displace = 30;

void setup() 
{ 
 Serial.begin(9600);
 boolean already_done  = false;
if (already_done=false)

 time = millis();
 value = 128+127*cos(2*PI/periode*(displace-time));
analogWrite(ledpin, value);           // sets the value (range from 0 to 255) 
already_done = true;

} 

void loop() {
 Serial.println(value);
}

This performs the fade action only once on startup. After that, if you want to do something else in the loop you are free to do so without the worry of the fade coming back around each iteration through the loop.

With that said, macro_c's snippet should work just fine.

Bill

You have scope issues with your variable and an = where == should be. I can’t believe it even compiles…

int value ;
int ledpin = 9;                           // light connected to digital pin 9
long time=0;

int periode = 2000;
int displace = 30;

void setup() 
{ 
 Serial.begin(9600);
} 

void loop() 
{
 static boolean already_done  = false;

 if (!already_done)
 {
   time = millis();
   value = 128+127*cos(2*PI/periode*(displace-time));
   analogWrite(ledpin, value);           // sets the value (range from 0 to 255) 
   Serial.println(value);
   already_done = (value == 255);  // if value ==255 then stop. As floats are being used in calcs above, this may need to be >=254 instead.
 }
}

marco_c, how many times have we wished compilers were clairvoyant. :-)

Bill

Thanks marco-c. But it don't work, the LED just lite full. But why do you put a "!" in "if (!already_done)"?

I explain what i want to happened. When variable x get "0" i want the LED to very slow light up (20 sec.)

int value ;
int ledpin = 9;                           // light connected to digital pin 9
long time=0;

int periode = 2000;
int displace = 30;

void setup() 
{ 
//
} 

void loop() 
{
 static boolean already_done  = false;

 if (!already_done)
 {
   time = millis();
   value = 128+127*cos(2*PI/periode*(displace-time));
   analogWrite(ledpin, value);           // sets the value (range from 0 to 255) 
   already_done = (value == 254);  // if value ==255 then stop. As floats are being used in calcs above, this may need to be >=254 instead.
 }
}

Hi, Have you tried Buildefish's sketch.

Sketches are divided into a number of parts. For your case void setup { } void loop { }

void setup is run just ONCE on startup. void loop runs after setup and runs continuously looping.

You need to put your successful fade program in void setup, not void loop.

Leave void loop as.

void loop { }

Simple... Tom.... :)

The code you have does not do what you describe. If you modify it like below:

int value ;
int ledpin = 9;                           // light connected to digital pin 9
long time=0;

int periode = 2000;
int displace = 30;

void setup() 
{ 
  Serial.begin(57600);
} 

void loop() 
{
  static boolean already_done  = false;

  if (!already_done)
  {
    time = millis();
    value = 128+127*cos(2*PI/periode*(displace-time));
    Serial.println(value);
    analogWrite(ledpin, value);      // sets the value (range from 0 to 255) 
    already_done = (value == 0); 
  }
}

You will see on the serial monitor that it prints starting at 254 (completely on) and ends at 1 (completely off). So checking for it to be 254 stops the action immediately.

You need to either change the formula for value (and then check for 254) or simply analogWrite(ledpin, 255-value). This works for me, but it is much faster than 20 seconds

void loop() 
{
  static boolean already_done  = false;

  if (!already_done)
  {
    time = millis();
    value = 128+127*cos(2*PI/periode*(displace-time));
    Serial.println(value);
    analogWrite(ledpin, 255-value);      // sets the value (range from 0 to 255) 
    already_done = (value <= 1); 
  }
}

Similarly, this code just in setup() does the same thing, as others have hinted (insisted?)

void setup() 
{ 
  Serial.begin(57600);
  while (value != 1)
  {
    time = millis();
    value = 128+127*cos(2*PI/periode*(displace-time));
    Serial.println(value);
    analogWrite(ledpin, 255-value);      // sets the value (range from 0 to 255) 
  }
}

The ! is a logical NOT. As we are dealing with a boolean value, then !false is true and !true is false. so !already_done is the same as writing already_done == false (or more precisely already_done != true).

Thank you very much for the help. I am not shure how the code works but it does. One thing i do not understand. Why you say i can have the code in the setup. Then i am not able to control it? If so how can you then turn it on/off?

Best regars
Ole

int value ;
int ledpin = 9;                           // light connected to digital pin 9
long time=0;

int periode = 20000;
int displace = 0;

void setup() 
{ 
  Serial.begin(57600);

}
void loop() 
{
  static boolean already_done  = false;
  if (!already_done)
  {
    time = millis();
    value = 128+127*cos(2*PI/periode*(displace-time));
    Serial.println(value);
    analogWrite(ledpin, 255-value);      // sets the value (range from 0 to 255) 
    already_done = (value <= 1); 
  }
}

thronborg: Hi I want to fade a LED from 0-255 ONLY ONE TIME. I found this but it runs back and forth fading down-fading up. I can't figure out how to fade up just ONE single time. I don't want to use a delay.

You didn't say anything about wanting to have any sort of control over it. You only said that you wanted it to happen once and once only. What sort of control do you need?

You didn’t say anything about wanting to have any sort of control over it. You only said that you wanted it to happen once and once only. What sort of control do you need?

Delta_G
Sorry for that. I am new to this and assumed that it was clear. I wrote this earlier.

I explain what i want to happened.
When variable x get “0” i want the LED to very slow light up (20 sec.)

Se attached Picture.

This is the case
I have built my private bar on a Titanic theme. I have the Adafruit mp3 card “MusicMaker” where i can start different sounds/songs from the Arduino and it works great.

I now want to sync the Titanic theme song with 3 sets of 20xLED that start illuminating the bar slowly at 3 different timestamps when the music plays.

So when i hit the button “Start Titanic” the Titanic theme song starts.
After 12 sec, the first set of LED slowly light up the chimneys. Fade time 20sec.
After 48 sec, the second set of LED light up the aft room. Fade time 6sec.
After 1 min 12 sec, the third set of LED light up the whole bar. Fade time 3sec.
When the song is end the 3 set LED slowly Fade down. Fade time 8sec.

Best regards
Ole