Button Fade
We made a new Arduino project today. It’s a code to make a light on the Arduino fade on and off by pressing a button.
Here’s the circuit diagram.
This is the code.
int buttonPin = 2; int buttonValue = 0; int ledPin = 9; int ledValue = 0; void setup() { pinMode(buttonPin, INPUT); } void loop() { buttonValue = digitalRead(buttonPin); if (buttonValue) { ledValue += 1; } else { ledValue -= 1; } if (ledValue > 255) { ledValue = 255; } if (ledValue < 0) { ledValue = 0; } analogWrite(ledPin, ledValue); delay(10); }
The assembled project looks like this:
It was a very fun thing to make and looks really cool too. When you take your finger off the button, the light slowly fades.
The hardest part was figuring out the resistors. The resistors all look the same, but they’re not. You need to read the color code on the resistor to figure out which kind it is. We found this handy chart. With this, we could tell the 10,000 ohm pulldown resistors from the little ones used for the LED.