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;
}