Digital Input
Serial Monitor
pressing button changes fading animation speed
html
const int buttonpin = 3; const int ledpin = 10; int brightness = 0; int fadeamount =5; int buttonstate = 0; void setup() { pinMode(ledpin,OUTPUT); pinMode(buttonpin,INPUT); } void loop() { buttonstate = digitalRead(buttonpin); if (buttonstate == HIGH){ analogWrite(ledpin, brightness); brightness = brightness + fadeamount; if(brightness = 255){ fadeamount = -fadeamount; } delay(30); } else { analogWrite(ledpin, brightness); brightness = brightness + fadeamount; if(brightness = 255){ fadeamount = -fadeamount; } delay(100); } }
Interesting strategy! There were more than one way to solve this exercise, and I see you’re using the button to control the delay rather than fadeAmount, like some of your classmates. Nice work!