simple toggle fade I can't do

can't get my for loop to work or something - syntax maybe?

It's just supposed to fade a turned on LED to 0 when the button is pressed and vice versa.

thanks, Eric

int inPin = 2;
int outPin = 9;
int state = 0;
int reading;
int previous = LOW;
long time = 0;
long debounce = 200;

void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}

void loop()
{
reading = digitalRead(inPin);

if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == 255) {
for (int state=255; state >= 0; state -=5) {
analogWrite(outPin, state);
delay(20);
}
}
else if (state == 0) {
for (int state=0; state <= 255; state +=5) {
analogWrite(outPin, state);
delay(20);
}
}

time = millis();
}

previous = reading;
}

toggleFadeInOut.pde (646 Bytes)

You told what it is supposed to do, but not what it does.
Maybe too many variables called 'state' ?

syntax maybe?

If it was syntax then it would not compile.
if (state == 255) {
uses a different variable to
analogWrite(outPin, state);
Because you redefined it in the
for (int state=255; state >= 0; state -=5) {
instruction.
When posting code select it and hit the # icon

sorry guys let me try that again - right now when it runs the led is off and when i hit the button it fades to on (as intended)

but on every preceding button press the led fades from 0 to 255 again

should I try another variable for the analogWrite?

thanks again

int inPin = 2;
int outPin = 9;
int state = 0;
int reading;
int previous = LOW;
long time = 0;
long debounce = 200;

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == 255) {
      for (int state=255; state >= 0; state -=5) {
      analogWrite(outPin, state);
      delay(20);
      }
    }
    else if (state == 0) {
      for (int state=0; state <= 255; state +=5) {
      analogWrite(outPin, state);
      delay(20);
      }
    }

    time = millis();    
  }

  previous = reading;
}

here's trying it with a while loop

int led;
int inPin = 2;
int outPin = 9;
int state = 0;
int reading;
int previous = LOW;
long time = 0;
long debounce = 200;

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == 255) {
      while(state > 0); {
      analogWrite(outPin, state);
      state--;
      delay(20);
      }
    }
    else if (state == 0) {
      while(state < 255) {
      analogWrite(outPin, state);
      state++;
      delay(20);
      }
    }

    time = millis();    
  }

  previous = reading;
}
      while(state > 0); {

That semicolon forms the body of the while loop. The curly brace following it is a (useless) decoration. You probably want to lose the ;.

PaulS:

      while(state > 0); {

That semicolon forms the body of the while loop. The curly brace following it is a (useless) decoration. You probably want to lose the ;.

oh yeah thanks, but still won't work

oh yeah thanks, but still won't work

Yes, it does. It just doesn't do what you want it to. You need to describe what it does do, and what you want it to do.

got it! sorry guys, this was my first project

fades in when turned on, fades in and out with switch

so proud (first step in bigger project)

thanks, eric

int inPin = 2;
int outPin = 9;
int state = 0;
int reading;
int previous = HIGH;
long time = 0;
long debounce = 200;

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);

while(state < 255) {
      analogWrite(outPin, state);
      state++;
      delay(20); 
}
}

void loop()
{
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == 255) {
      while(state > 0) {
      state--;
      analogWrite(outPin, state);
      delay(20);
    }
    }
    else if (state == 0) {
      while(state < 255) {
      state++;
      analogWrite(outPin, state);
      delay(20);
    }
    }
    time = millis();    
  }

  previous = reading;
}

fades in when turned on, fades in and out with switch

So that is either what you want it to do OR what it does.
Either way there is still something missing.