led fade from 0 to 225 then hold at 255


Im trying to create a bit of code that when a button is pressed its fades in a led to full brightness and then the led stays on.

Ive got the getting started with arduino book and adapted a bit of code that will turn an led on and off with a button press to now fade in, but the loop when it gets to the 255 value just resets to 0 and fades in again.

Ive been looking at breaks and trying to include them in my code but had no luck so far.

this is the bit of code i have so far

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  9;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int fade = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    for (fade = 0; fade <= 255; fade++){
    analogWrite(ledPin, fade);
  else {
    // turn LED off:
    for (fade = 255; fade > 0; fade--){
    analogWrite(ledPin, fade);

Thanks in advance, Phil

I think we need a bit more information. The LED can be made to fade from 0 to 255 when the button is pressed. But, should it do that every time the button is pressed, or just once?

Currently, you are not debouncing the switches. You should. There is a switch library (called Button (which is a pet peeve of mine. Buttons are on the front and sleeves of shirts. Switches allow/prevent the flow of electrons) that will make this easy for you.

One of the methods of that class is uniquePress, which might be just what you are looking for.

intially the led would be set at 0 when pressed and held it would go from 0 to 255 and once there stay at 255 when pressed again it would go from 255 to 0

but also what I would like it to do is, say the button was pressed and it started to brighten, if the button was pressed again before the led got to 255 it would take the current value and got back down to 0.

The project Im looking to do is control a series of leds with in IR sensor, when the user comes into range of the sensor this triggers the leds to fade in, if the were to stay they would see all the leds come on. if they were to walk away the leds would fade out. A quick swipe with the hand would start to activate the leds but then they would fade out again quickly

Hope this helps

Ahh ill try use switches from now on, pretty new to all this

Yeah further on in the book it talks about debouncing I thought it would be better to leave it out for now to make it more simple for myself ::)

With any kind of distance sensor, the distance does not get smaller, and then stop. It continues to return smaller and smaller values as the object gets closer, then larger and larger values as the object moves away.

This is quite different from a switch that is either on or off.

Expecting to write code that will work for either a switch or a distance sensor is not particularly realistic.

The problem with your code is that after fading up the LED the loop() function returns and is immediately re-run, and the fade-up loop executes again from the beginning.

Simple fix is after the fading up, use a while-loop to wait for the button to be released.

Similarly for the fading-down, can wait after the loop for the button to be pressed again.

You should also check in the loops for the button state changing and respond by exiting the loops immediately with break;

There are more sophisticated approaches of course, and you'll need them to do anything else besides controlling the LED. And of course debouncing is also a good idea as has been mentioned.