Lab 3 – Arduino LED with Potentiometer

Components
• 1 Arduino Uno
• 3 resistor (220 ohms)
• 2 potentiometers
• 3 LEDs (red, blue, green)
• 1 breadboard

Description
I have continued with the color fading code from Lab2. I chose option 1 where I have one pot controlling the brightness and another pot controlling the delay/wait time for the fading speed. For the pot that’s controlling the brightness, I have used the (pot value/4) as an additional parameters that will be added or subtracted from the fading loop. I’m aware that it will also increase or slow the speed of fading, which might conflict with the wait time control. However, it adds a bit more unpredictability and surprises to the results. Playing around both the delay pot and the brightness pot, I can sometimes get a very fast fading temple with low brightness or a slow fading temple with high brightness.

Youtube Video Link
Arduino LED with Potentiometer

Code

/*
* Code for cross-fading 3 LEDs, red, green and blue, or one tri-color LED, using PWM
* The program cross-fades slowly from red to green, green to blue, and blue to red
* The debugging code assumes Arduino 0004, as it uses the new Serial.begin()-style functions
* Clay Shirky
*/

char b_input[100];
int b_value = 0;

// Output
int greenPin = 9; // Green LED, connected to digital pin 9
int bluePin = 10; // Blue LED, connected to digital pin 10
int redPin = 11; // Red LED, connected to digital pin 11

// Program variables
int redVal = 1; // Variables to store the values to send to the pins
int greenVal = 1; // Initial values are Red full, Green and Blue off
int blueVal = 255;

// Sensor Setup
int sensorPin0 = A0; // select the input pin for the potentiometer
int sensorPin1 = A1; // select the input pin for the potentiometer
int sensorValue0 = 0; // variable to store the value coming from the sensor
int sensorValue1 = 0; // variable to store the value coming from the sensor

int i = 0; // Loop counter
int wait = 100; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial

void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT); // If we want to see the pin values for debugging...
Serial.begin(9600); // ...set up the serial ouput on 0004 style
Serial.println("just continue to send me b");
}

// Main program
void loop()
{
// read the value from the sensor:
sensorValue0 = analogRead(sensorPin0);
sensorValue1 = analogRead(sensorPin1);

i += 20; // Increment counter
if (i < 255) // First phase of fades { greenVal += 1 + sensorValue1/4; // Green up redVal -= 1 + sensorValue1/4; // Red low blueVal = 1 + sensorValue1/4; } else if (i < 509) // Second phase of fades { greenVal -= 1 + sensorValue1/4; // Green down redVal = 1 + sensorValue1/4; // red low blueVal += 1 + sensorValue1/4; // Blue up } else if (i < 763) // Third phase of fades { greenVal = 1 + sensorValue1/4; // Green low redVal += 1 + sensorValue1/4; // Red down blueVal -= 1 + sensorValue1/4; // Blue up } else // Re-set the counter, and start the fades again { i = 1; } memset(b_input, 0, 100); readSerialString(b_input); if(b_input[0] == 'b'){ b_value = b_value + 1; Serial.println(b_value); } if (b_value == 13) { b_value = 0; } analogWrite(redPin, redVal); // Write current values to LED pins analogWrite(greenPin, greenVal); analogWrite(bluePin, blueVal + b_value*20); Serial.print("red increment: wait time"); Serial.println(sensorValue0); Serial.print("red increment: brightness"); Serial.println(sensorValue1); delay(sensorValue0); // Pause for 'wait' milliseconds before resuming the loop // if (DEBUG) { // If we want to read the output // DEBUG += 1; // Increment the DEBUG counter // if (DEBUG > 10) // Print every 10 loops
// {
// DEBUG = 1; // Reset the counter
//
// Serial.print(i); // Serial commands in 0004 style
// Serial.print("\t"); // Print a tab
// Serial.print("R:"); // Indicate that output is red value
// Serial.print(redVal); // Print red value
// Serial.print("\t"); // Print a tab
// Serial.print("G:"); // Repeat for green and blue...
// Serial.print(greenVal);
// Serial.print("\t");
// Serial.print("B:");
// Serial.println(blueVal); // println, to end with a carriage return
// }
// }
}

void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
//Serial.print(strArray);
i++;
}
}

Leave a Reply