Description
For this lab we used potentiometers to control and speed and brightness of the LEDS. For the first part of the assignment I used 3 pots to control the brightness of each LED (code 1) and 2 pots to control the brightness and speed of all LED together (code 2).
For the second part of the assignment I controlled the 3 LEDS again with 2 potentiometers. The first pot simulated the "dimming leds" sketch, with the difference that the user controled the "phase"= color. The second pot controlled the speed of the blinking.
Here is a youtube link to a video of the third sketch: http://youtu.be/9agx8y2VyuY
Components
1 Arduino Uno
1 Breadboard
3 leds
3 potentiometers
3 220 Ohm resistors
cables
Code 1
/*
* Control 3 LEDs with 3 potentiometers
* Each pot fades one led
*/
// Analog pin settings
int porPinR = 0; // Potentiometers connected to analog pins 0, 1, and 2
int porPinG = 1;
int porPinB = 2;
// Digital pin settings
int ledPin = 9; // LEDs connected to digital pins 9, 10 and 11
int greenPin = 10;
int bluePin = 11;
// Values
int valR = 0; // Variables to store the input from the potentiometers
int valG = 0;
int valB = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
valR = analogRead(porPinR) / 4; // read input pins, convert to 0-255 scale
valG = analogRead(porPinG) / 4;
valB = analogRead(porPinB) / 4;
analogWrite(ledPin, valR); // Send new values to LEDs
analogWrite(greenPin, valG);
analogWrite(bluePin, valB);
}
Code 2
/*
* Control 3 LEDs with 2 potentiometers
* Each pot fades one led
*/
// Analog pin settings
int porPin1 = 0; // Potentiometer connected to analog pin 0 controls the brightness of the leds.
int porPin2 = 1; // Potentiometer connected to analog pin 1 controls the blinkness of the leds.
// Digital pin settings
int ledPin = 9; // LEDs connected to digital pins 9, 10 and 11
int greenPin = 10;
int bluePin = 11;
// Values
int valBright = 0; // Variables to store the input from the potentiometers
int valBlink = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
valBright = analogRead(porPin1) / 4; // read input pins, convert to 0-255 scale
valBlink = analogRead(porPin2); // read input pins, convert to 0-255 scale
analogWrite(ledPin, valBright); // Send new values to LEDs
analogWrite(greenPin, valBright);
analogWrite(bluePin, valBright);
delay(valBlink); // stop the program for some time
analogWrite(ledPin, 0); // Send new values to LEDs
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(valBlink); // stop the program for some time
}
Code 3
/*
* Control 3 LEDs with 2 potentiometers
* The first pot controls the colors and the second the blinkness
* I scaled the 0-1023 input values of the first pot to a 0-767 dimming scale, where 0-255 turns the color from red
* to green, 256-511 turns the green to blue and 512-767 turns the blue to red.
* It works just like the dimming leds sketch, with the difference that the user controls the "phase"= color.
* The second pot controls the blinking. 0 turns the blinking off. 1-1023 delays the blinking accordingly.
*/
// Analog pin settings
int porPin1 = 0; // Potentiometer connected to analog pin 0 controls the brightness of the leds.
int porPin2 = 1; // Potentiometer connected to analog pin 1 controls the blinkness of the leds.
// Digital pin settings
int ledPin = 9; // LEDs connected to digital pins 9, 10 and 11
int greenPin = 10;
int bluePin = 11;
// Values
int valBright = 0; // Variables to store the input from the potentiometers
int valBlink = 0;
int valFade = 0;
// Program variables
int redVal = 0; // Variables to store the values to send to the pins
int greenVal = 0; // Initial values are Red full, Green and Blue off
int blueVal = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600); // ...set up the serial ouput
}
void loop()
{
valBright = analogRead(porPin1); // read input pins
valFade = floor(valBright*3/4); // convert it to a 0-767 scale
Serial.print(valBright); //just for testing
valBlink = analogRead(porPin2);
if (valFade < 256) // First "phase" of fades
{
redVal = 256- valFade; // Red down
greenVal = valFade; // Green up
blueVal = 1; // Blue low
}
else if (valFade < 511) // Second "phase" of fades
{
redVal = 1; // Red low
greenVal = 511- valFade; // Green down
blueVal = valFade- 256; // Blue up
}
else if (valFade <= 767) // Third "phase" of fades
{
redVal = valFade - 511; // Red up
greenVal = 1; // Green low
blueVal = 767- valFade; // Blue down
}
if (redVal > 255) //check for overflow
redVal = 255;
if (greenVal > 255)
redVal = 255;
if (blueVal > 255)
redVal = 255;
analogWrite(ledPin, redVal); // Send new values to LEDs
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
Serial.println(valFade); //testing 2
if (valBlink!= 0){ // check if delay pot is ON
delay(valBlink); // stop the program for some time
analogWrite(ledPin, 0); // Send new values to LEDs
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(valBlink); // stop the program for some time
}
}
- Login to post comments