int outputPin = 1;
int buttonPin = 0;
int fadeAmount;
int potVal;
bool wasPressed = false;
void setup() {
pinMode(outputPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(outputPin, LOW);
}
void loop() {
potVal = analogRead (2); // read pot on attiny pin 3
fadeAmount = map (potVal, 0, 1023, 20, 500);
if (digitalRead(buttonPin) == LOW) {
delay(15);
if (digitalRead(buttonPin) == LOW) {
wasPressed = true;
if (wasPressed == true) {
fadeLights();
}
if (wasPressed == false) {
digitalWrite(outputPin, LOW);
}
}
}
}
void fadeLights() {
for ( int i = 0; i <= 255; i++) {
analogWrite(outputPin, i);
for ( int y = 0; y < fadeAmount; y++) {
// if button is released
if (digitalRead(buttonPin) == HIGH) {
return;
}
}
}
for ( int i = 255; i >= 0; i--) {
analogWrite(outputPin, i);
for ( int y = 0; y < fadeAmount; y++) {
// if button is released
if (digitalRead(buttonPin) == HIGH) {
digitalWrite (outputPin, LOW);
return;
}
}
}
}