ASSIGNMENT
Connect 3 LEDs (red, green, and blue) and resistors to the Arduino breadboard. Solder wires to connect 2 analog potentiometers to the breadboard. Create a custom way to control the brightness and blink rate of the 3 LED's using analog input from the potentiometers.
MATERIALS USED
3 LEDs (red, green, blue), 3 220 ohm resistors, 2 Potentiometers
ARDUINO CODE
/*
* One pot changes the brightness of all 3 LED's and one pot changes the blink rate of all 3 LED's
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int potPin_brightness = 2; // select the input pin for the pot for brightness
int val_brightness = 0; // variable to store the value coming from the brightness pot
int brightness = 0; // how bright the LED is
int potPin_blink = 3; // select the input pin for the pot for brightness
int val_blink = 0; // variable to store the value coming from the blink pot
void setup() {
// declare pin 9 to be an output - RED:
pinMode(9, OUTPUT);
// declare pin 10 to be an output - GREEN:
pinMode(10, OUTPUT);
// declare pin 11 to be an output - BLUE:
pinMode(11, OUTPUT);
}
void loop() {
val_brightness = analogRead(potPin_brightness); // read the value from the pot, between 0 - 1024
brightness = val_brightness / 4; // Set the brightness--LED brightness can be 0-254:
if (brightness >= 255)
{
brightness = brightness - 2; //If the pot value is 1024 then divide by 4 and subtract 2.
}
val_blink = analogRead(potPin_blink); // read the value from the pot, between 0 - 1024
// Set the LED's on
digitalWrite(9, HIGH); // set the RED LED on
digitalWrite(10, HIGH); // set the GREEN LED on
digitalWrite(11, HIGH); // set the BLUE LED on
// set the brightness of pin 9 - RED:
analogWrite(9, brightness);
// set the brightness of pin 10 - GREEN:
analogWrite(10, brightness);
// set the brightness of pin 11 - BLUE:
analogWrite(11, brightness);
delay(val_blink); // wait for the delay amount
digitalWrite(9, LOW); // set the RED LED off
digitalWrite(10, LOW); // set the GREEN LED off
digitalWrite(11, LOW); // set the BLUE LED off
delay(val_blink); // wait for the delay amount
}