Running a looped function just once? Fading an LED up and leaving it at that!

Hi

I am trying to create a lighting effect where an LED or set of LED fade up to full brightness and remain at that brightness. Whichever example i find there is a loop involved and i understand that. Obviously i need to go from a value to another value so being in an increasing value loop makes sense.

What i want to do is stop the fade at the brightest value and leave it at that, i t would not change again until the sketch is restarted. So, how do interrupt the loop or stop it dead so the LED doesn't go back through the fade loop? I have done some googling and seen mentions of putting the loop in the main body of the sketch and not the loop section and also using while (1) to stop the loop. I couldn't get either to work. To start with i am using the standard Fade sketch in the examples but i'm guessing there is a totally different, and probably quite simple way to do this? Once i have this sorted im going to put some other LEDs switching on at various times, probably using delay or millis but without this bit the effect will not be what i need. Any help would be greatly appreciated.

Thank you.

Jim

Put your fade code in setup() instead of loop().

Setup runs only once at startup.

mmmmmmmmmm, Thank you very much for the quick reply but i tried that as i had read it somewhere else, i have tried it and cant seem to make it work. The standard fade sketch:

/* Fade

This example shows how to fade an LED on pin 9 using the analogWrite() function.

This example code is in the public domain. */

int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); }

// the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness);

// change the brightness for next time through the loop: brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); }

Works fine, so i changed it to this (Just copying out the loop to the setup section)

/* Fade

This example shows how to fade an LED on pin 9 using the analogWrite() function.

This example code is in the public domain. */

int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT);

// set the brightness of pin 9: analogWrite(led, brightness);

// change the brightness for next time through the loop: brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); }

// the loop routine runs over and over again forever: void loop() {

} And absolutely nothing happens,the LED doesn't light at all. So, what amazingly simple mistake have i made because there is bound to be one. My logic says that the loop cannot complete because it doesn't go back into the loop once it gets to the end to check the value, but that's just my logic of course. Also i want it to stop once it gets to max brightness but i found that if i take out the fade back down bits of the code i still get nothing.

/* Fade

This example shows how to fade an LED on pin 9 using the analogWrite() function.

This example code is in the public domain. */

int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT);

// set the brightness of pin 9: analogWrite(led, brightness);

// change the brightness for next time through the loop: brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade: //if (brightness == 0 || brightness == 255) { //fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect //delay(30);

// the loop routine runs over and over again forever: void loop() {

}

As you can see i'm no expert here......................

If the code to fade up the LED needs to be in loop() because it is triggered by time, a switch input, a sensor input or whatever, then you could do this

start of loop()
  if alreadyMax is false
    fade up the LED
    set alreadyMax to true
  end of if
  other code to be executed each time through loop() goes here
end of loop()

HeliBob, i guess the issue is that i do not know how to do what i need to. It's an effect i'm after and really do not mind how that is achieved. I just need to be able to fade an LED,or series of LED from 0-255 and keep them there for the remainder of time if need be. I had read that i can move the contents of the loop from loop to setup and that's what bulldog had suggested too so i'm guessing it can be done; i just don't know how. Everything i see about fading LED seems to be in a loop (and then fade back down again afterwards) but i just want mine to fade up, slowly and stay there. i have no idea (yet) how to do this. The trigger is one power switch on a model kit that will cause the complete sketch to run with no interrupting until i switch it off again, i don't even need the switch in the sketch as i am interrupting power supply to the board to do it. Any ideas on the best way to achieve this would be very much appreciated.

Jim

Following is the first piece of code from your earlier post - see how much easier it is to read when you use code tags (the # button)

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()  {
  // set the brightness of pin 9:
  analogWrite(led, brightness);   

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }     
  // wait for 30 milliseconds to see the dimming effect   
  delay(30);                           
}

This code depends on the repetitive nature of loop() and obviously it will not work unchanged when you move it into steup().

This is the part that needs to go into setup() - I have taken out the bit that reverses the direction

  analogWrite(led, brightness);   

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // wait for 30 milliseconds to see the dimming effect   
  delay(30);

I hope you can see that there is nothing to cause this to repeat - so you have to add that (something like this)

brightness = 0; // to start things off
while (brightness < 255) {  // this loops until the condition is true
    // other code goes here
}

And the completed piece (which can be popped into setup() is

brightness = 0; // to start things off
while (brightness < 255) {  // this loops until the condition is true
    analogWrite(led, brightness);   

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // wait for 30 milliseconds to see the dimming effect   
  delay(30); 
}

…R

I'll swap you a capacitor and couple of resistors for your arduino. They'll do the job nicely :)

Robin2, thank you very much,i see how that works and that is just as important as getting the effect i guess. Everyday does seem to be a school day!

Ken, i'm not quite sure what you mean but i guess it something like why the hell have you got an Arduino? I have used to great effect in other areas but the loop thing just gets me every time! Thanks everyone for the help.

jimpeachey: Ken, i'm not quite sure what you mean but i guess it something like why the hell have you got an Arduino? I have used to great effect in other areas but the loop thing just gets me every time! Thanks everyone for the help.

No worries, I was just joshing.

I kinda guessed you were :)

The trigger is one power switch on a model kit that will cause the complete sketch to run with no interrupting until i switch it off again

In that case putting the fade up routine in setup() is the way to go. Follow the advice given by Robin and you will be OK. Personally I would have used a for loop instead of while

for (int brightness = 0; brightness <= 255; brightness += fadeAmount)
{
  analogWrite(led, brightness);   
  delay(30); 
}

but they are functionally equivalent so either way is good

Thank you all, i have got Robins working now and am very happy, i guess the next question would be where do i put the next section so that a further LED will fade up after this initial one, i have two of them going at the same time so i am guessing there needs to be some 'nesting' done somewhere but it would seem that the delay at the end of this section is stopping something happening? I'm going to have a go at helibobs version now and see how that works out. You are all stars, every one of you.

i have two of them going at the same time so i am guessing there needs to be some 'nesting' done somewhere but it would seem that the delay at the end of this section is stopping something happening?

Can you describe exactly the sequence of events that you are looking to create ? If any of the LED fading/flashing happens in parallel but at a different frequency from one another or they have different start or stop times then you will need to eliminate all delay()s from that section of the program as while delay() executes nothing else can.

It is time for you to look at the BlinkWithoutDelay example I think.

Helibob, thanks. Im just playing around at the moment. Ideally the overall sketch will have two fade effects, one just after the other,ie as one fade finishes, wait a sec or two and then fade the second one up. Also there are going to be multiple LED that come on in a set sequence some will flash, some wont. Im pretty sure i can manage all of the static and flashing LED, i've done that before. Its just the fade effects that are doing my head in. One to fade up as you have shown me,wait as ec or two (I can play with timings) and then start the second fade, neither will fade back down.

jimpeachey: where do i put the next section so that a further LED will fade up after this initial one

You probably need to look at the demo several things at a time which uses the BWoD technique.

This Thread Planning and Implementing a Program may also have some useful stuff.

...R