It was hard to write a code but since Arduino showed which part of my code was wrong, I was able to fix it and able to finish it:) It was challenging but also was very interesting. Thank you for the detailed instruction!
Below I included links to my homework and my code.
Pressing button changes fading animation speed
const int buttonPin = 2;const int ledPin = 13;
int brightness = 0;
int fadeAmount = 5;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
analogWrite(ledPin, brightness);
brightness = brightness + fadeAmount;
if (brightness = 255) {
fadeAmount = -fadeAmount;
}
delay(5);} else {
analogWrite(ledPin, brightness);
brightness = brightness + fadeAmount;
if (brightness = 255) {
fadeAmount = -fadeAmount;
}
delay(60);
}
}
Nice work, Shin Young! As for the code, I’ll ask you the same thing I asked Yufei and Elvis: Can you think of any way to avoid repeating the analogWrite and brightness adjustment portions? Perhaps the button could be made to control fadeAmount?