Description:
I connected three potentiometers to my previous set up of three LEDs in parallel, one to each LED. And then I uploaded my code that would adjust the delay of each LED with the potentiometer. I played around with it to finally get the lights to turn on and off sequentially. (video here)
The next step was to dim the LEDs using the potentiometers. I modified and uploaded that code separately and it allowed me to use each individual potentiometer to change the brightness of each LED.
Components:
- 1 Arduino
- 1 Breadboard
- 3 LEDs (blue, red, green)
- 3 Resistors (220 ohms each)
- 3 Potentiometers
- 18 connecting wires
- 1 USB Cable
- 1 Laptop
Code (Pattern):
int redSensorPin = A2; // select the input pin for the potentiometer
int greenSensorPin = A1;
int blueSensorPin = A0;
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int redSensorValue = 0; // variable to store the value coming from the sensor
int greenSensorValue = 0;
int blueSensorValue = 0;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// read the value from the sensor:
redSensorValue = analogRead(redSensorPin);
greenSensorValue = analogRead(greenSensorPin);
blueSensorValue = analogRead(blueSensorPin);
// turn the ledPin on
digitalWrite(redPin, HIGH);
delay(redSensorValue);
digitalWrite(greenPin, HIGH);
delay(greenSensorValue);
digitalWrite(bluePin, HIGH);
delay(blueSensorValue);
// stop the program for <sensorValue> milliseconds:
// turn the ledPin off:
digitalWrite(redPin, LOW);
delay(redSensorValue);
digitalWrite(greenPin, LOW);
delay(greenSensorValue);
digitalWrite(bluePin, LOW);
delay(blueSensorValue);
// stop the program for for <sensorValue> milliseconds:
}
Code (Dimming):
int redSensorPin = A2; // select the input pin for the potentiometer
int greenSensorPin = A1;
int blueSensorPin = A0;
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int redSensorValue = 0; // variable to store the value coming from the sensor
int greenSensorValue = 0;
int blueSensorValue = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the ledPin as an OUTPUT:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// read the value from the sensor:
redSensorValue = analogRead(redSensorPin);
greenSensorValue = analogRead(greenSensorPin);
blueSensorValue = analogRead(blueSensorPin);
analogWrite(redPin, redSensorValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(greenPin, greenSensorValue/4);
analogWrite(bluePin, blueSensorValue/4);
Serial.println(“hello”); // print the pot value back to the debugger pane
delay(10); // wait 10 milliseconds before the next loop
}