Homework 3: fading LEDs in setup and loop

 

For this circuit, I used:

  • Redboard
  • Breadboard
  • 2 LEDs – red and green
  • 3 wires
  • 2 resistors

For the code, I initially used the circuit provided in Arduino examples, but I noticed that the red LED light did not light up. After looking through the code, I realised that since the brightness initially was 0 and the first step would have increased it by 5 within 30 miliseconds, my eye did not catch the difference. So I changed the code to 9 analogWrite() steps, varying the brightness.

In this case, the red LED light up and faded first and then a loop started with green LED fading in and out. This is because red LED was part of the setup and once the program runs each line of the setup, it proceeds to the loop with green LED.

 

/*Laine Una Melkerte
* September 12, 2017
*/
int green = 11, red = 10; //green pin will be connected to pin11, and red to pin 10
void setup() {
// five values of brightness to cause fade effect: 0, 50, 100, 150, 200, 255
analogWrite(red, 0); //set the brightness to 0
delay(100); //delay
analogWrite(red,50); //set the brightness to 50, etc.
delay(100);
analogWrite(red,100);
delay(100);
analogWrite(red,150);
delay(100);
analogWrite(red,200);
delay(100);
analogWrite(red,255);
delay(100);
analogWrite(red,200);
delay(100);
analogWrite(red,150);
delay(100);
analogWrite(red,100);
delay(100);
analogWrite(red,50);
delay(100);
}

void loop() {
//make a loop for a fading green LED using the same values as for red LED: 0, 50, 100, 150, 200, 255
analogWrite(green, 0);
delay(100);
analogWrite(green,50);
delay(100);
analogWrite(green,100);
delay(100);
analogWrite(green,150);
delay(100);
analogWrite(green,200);
delay(100);
analogWrite(green,255);
delay(100);
analogWrite(green,200);
delay(100);
analogWrite(green,150);
delay(100);
analogWrite(green,100);
delay(100);
analogWrite(green,50);
delay(100);
}

Leave a Reply

Your email address will not be published. Required fields are marked *