Fading led with button

Hello,

I've been trying to write a program which starts with led off (brightness = 0). It should wait until a button is pushed once (so the button state is not constant high), then it should fade in until the brightness is 255 and keep the brightness there.

When the brightness is 255 and the button is pushed again, it begins to fade out and stops when it reaches the zero.

My program just ignores the button altogether, fades in the led and that's about it.

I've studied Arduino books, different online tutorials and Jeremy Blum's tutorials, which have been the most informative, but I can't still get this thing work the way I want to. Debounce works with the same component layout.

Here's the code:

//Setup the Button
int buttonInt = 0; //digital pin 2

//Setup the LED
int faderLED = 11;
int brightness = 0;

void setup()
{
pinMode (faderLED, OUTPUT);
attachInterrupt(buttonInt, fadeIn, RISING);
}
void fadeIn()
{
if (brightness < 255)
brightness == brightness++;
delay(10);
}

void loop()
{
for (brightness = 0; brightness < 255; brightness++) {
analogWrite(faderLED, brightness);
delay(10);
}
for (brightness = 255; brightness > 0; brightness--) {

if (brightness < 255)
brightness == brightness++;
delay(10);

//Control LED Brightness
analogWrite(faderLED, brightness);
delay(10);
}
}

interrupt.ino (665 Bytes)

You don't need an interrupt handler to spot a human pushing a button, the urgency
doesn't warrant it.

You need to implement a state machine, the button changes the state from 'idle' to 'brighter', and
'full' to 'dimmer'. During 'brighter' and 'dimmer' the LED brightness is changed everytime 10ms
has elapsed, and state is changed to 'full' or 'idle' as appropriate.

Thus loop () contains code to check for state changes, and for 10ms having elapsed since the
last fade action (rather than using delay()).

So perhaps start here:

#define idle_state 0
#define brighter_state 1
#define full_state 2
#define dimmer_state 3

byte state = idle_state ;

void loop ()
{
  if (digitalRead (button))
  {
    if (state == idle_state)
      state = brighter_state;
    if (state == full_state)
      state = dimmer_state ;
  }
   ......
}