Foreword:
The purpose of this assignment was to experiment with the setup function and loop function and also manipulating the brightness of a LED using analogWrite to vary the amount of voltage in the circuit. The circuit itself was simple to set up, only requiring two resistors, two LEDs and four wires connecting it all.
Basically, the results of the circuit indicated that the setup function runs once before everything else and then the loop function runs repeatedly indefinitely afterwards. The analogWrite varies the voltage in the circuit which allows for variable brightness in the LED.
Code:
/*Author: Russell Coke
* Testing analogWrite on LEDs
*/
int setupLED = 12, loopLED = 11;
void setup() {
//setting up serial connection
Serial.begin(9600);
//setting the pins as outputs
pinMode(setupLED, OUTPUT);
pinMode(loopLED, OUTPUT);
//turning on the setupLED
digitalWrite(setupLED, HIGH);
delay(4000);
//turning it back off
digitalWrite(setupLED, LOW);
}
void loop() {
//setting initial voltage value for the analog output
//and time of the delay
int voltage = 255, delayTime = 250;
analogWrite(loopLED, voltage);
Serial.println(voltage);
//decreasing the voltage value so brightness changes
voltage = voltage – 50;
delay(delayTime);
analogWrite(loopLED, voltage);
Serial.println(voltage);
//decreasing the voltage value so brightness changes again
voltage = voltage – 50;
delay(delayTime);
analogWrite(loopLED, voltage);
Serial.println(voltage);
//decreasing the voltage value so brightness changes again
voltage = voltage – 50;
delay(delayTime);
analogWrite(loopLED, voltage);
Serial.println(voltage);
//decreasing the voltage value so brightness changes again
voltage = voltage – 50;
delay(delayTime);
analogWrite(loopLED, voltage);
Serial.println(voltage);
//decreasing the voltage value so brightness changes one more time
voltage = voltage – 50;
delay(delayTime);
analogWrite(loopLED, voltage);
Serial.println(voltage);
delay(delayTime);
}
Circuit Schematic: