Process:
I decide to use the red, yellow, green resistors (with 1.8V drop).
The minimum voltage requirement is 1.8V, 3.2V > 1.8V. So it's working.
We need to find 3 resistors that close to 160Ω
What if we choose 220Ω:
The maximum current requirement is 30mA, 14.5mA smaller than 30mA. So 220Ω's resistors are working well.
Therefore, I decide to use the 220Ω resistors
Code:
Set the pin 13, 11, 10 as output and pin 2 as input(pin 2 could use digitalRead).
The yellow LED will light up when the user presses the button.
Using the for loop, the green LED will light up and fade. Then, the red LED will light up and fade.
Loop these steps
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
// initialize serial communication at 9600 bits per second:
pinMode(13, OUTPUT);
// make pin 13 as an output
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
// make pin 10 as an output
pinMode(2, INPUT);
// make pin 2 as an input
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(13, HIGH);
//the pin 13 can be connected to power by writing “HIGH” at beginning
int buttonState = digitalRead(2);
// read the input pin:
Serial.println(buttonState);
// print out the state of the button:
delay(200);
// delay 0.2s
digitalWrite(10, LOW);
//the pin 10 can be connected to ground by writing “LOW” at beginning
digitalWrite(11, LOW);
for (int i = 0; i < 25; i++) { //for loop: the i value generally increase from 0 to 25
analogWrite(10, i);
// set the brightness of pin 10 is value i
delay(100);
// delay 0.1s
}
for (int i = 25; i >= 0; i--) {//for loop: the i value generally descrease from 25 to 0
analogWrite(10, i);
// set the brightness of pin 10 is value i
delay(100);
// delay 0.1s
}
for (int i = 0; i < 25; i++) { //for loop: the i value generally increase from 0 to 25
analogWrite(11, i);
// set the brightness of pin 10 is value i
delay(100);
// delay 0.1s
}
for (int i = 25; i >= 0; i--) {//for loop: the i value generally descrease from 25 to 0
analogWrite(11, i);
// set the brightness of pin 10 is value i
delay(100);
// delay 0.1s
}
}