You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.0 KiB
48 lines
1.0 KiB
float brightness = 0; // our main variable for setting the brightness of the LED
|
|
const int ledPin = 9; // we use pin 9 for PWM
|
|
const int sensorPin = 4; // we use pin D4 for the button
|
|
|
|
int buttonState = 0;
|
|
float counter = 0;
|
|
// the ratio between counter increment and fade should be tweaked
|
|
float counter_inc = 1.0;
|
|
float counter_fade = 0.99;
|
|
int counter_threshold = 90;
|
|
|
|
|
|
void setup() {
|
|
// put your setup code here, to run once:
|
|
Serial.begin(9600);
|
|
pinMode(ledPin, OUTPUT);
|
|
pinMode(sensorPin, INPUT);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
buttonState = !digitalRead(sensorPin);
|
|
// digitalWrite(ledPin,buttonState); // don't do this
|
|
|
|
Serial.println(counter);
|
|
|
|
// fading pattern that requires 'effort'
|
|
if (buttonState == HIGH) {
|
|
|
|
counter += counter_inc;
|
|
}
|
|
|
|
counter = counter*counter_fade;
|
|
|
|
if (counter > counter_threshold){
|
|
brightness ++;
|
|
// here you could also switchCase(myCase)
|
|
}
|
|
else {
|
|
brightness = brightness * 0.99;
|
|
}
|
|
|
|
brightness = constrain(brightness, 0, 255);
|
|
|
|
analogWrite(ledPin, brightness);
|
|
|
|
delay(5);
|
|
}
|
|
|