HW#3 LED fade with analogWrite

Using analogWrite( x ) and delay ( 100 ), I created a ‘fade’ for two LEDs — red and yellow. I varied x (over a range of 10-250) to create different brightnesses for the LED, and spread these out with delay in order to create the ‘fade.’ Initially I ran both LEDs in loop ( ) (see below video).

both LEDs in loop

I then moved the code for the yellow LED to setup ( ), which resulted in the code only running once, as opposed to ‘looping’ along with the red LED.

Yellow LED in setup

I did set up a serial.print for the output after defining the LEDs as said outputs, but the serial monitor only displayed 0. I am still unsure why this is the case, and hope to resolve this issue in our next class.

CODE:

int red = 3;
int yellow = 5;

int outputValue = 0;

 

void setup() {
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);

Serial.begin(9600);

analogWrite(yellow, 250);
delay(100);
analogWrite(yellow, 200);
delay(100);
analogWrite(yellow, 150);
delay(100);
analogWrite(yellow, 100);
delay(100);
analogWrite(yellow, 50);
delay(100);
analogWrite(yellow, 10);
delay(100);

analogWrite(yellow, 50);
delay(100);
analogWrite(yellow, 100);
delay(100);
analogWrite(yellow, 150);
delay(100);
analogWrite(yellow, 200);
delay(100);
analogWrite(yellow, 250);
delay(100);
}

void loop() {

Serial.print(“\t output = “);
Serial.println(outputValue);
delay(1);

analogWrite(red, 250);
delay(100);
analogWrite(red, 200);
delay(100);
analogWrite(red, 150);
delay(100);
analogWrite(red, 100);
delay(100);
analogWrite(red, 50);
delay(100);
analogWrite(red, 10);
delay(100);

analogWrite(red, 50);
delay(100);
analogWrite(red, 100);
delay(100);
analogWrite(red, 150);
delay(100);
analogWrite(red, 200);
delay(100);
analogWrite(red, 250);
delay(100);

 

}

Leave a Reply

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