Lab 3 - Controlling a color fader using two potentiometers

Nicholas's picture

Description

I modified my color fader from the second lab by using potentiometers instead of the keyboard to control the fading parameters. The color fader starts from blue and fades to another color, which can be pure red, pure green, or a blend of the two. One potentiometer controls the fading speed, and the other controls the red/green blending level. The fade color can be changed while the fader is running and the fader will smoothly adapt to the new color.

Materials

  • Arduino
  • Three LEDs (Red, green, blue)
  • Two potentiometers
  • Origami diffuser
  • Three 220k resistors
  • Assorted wires

Code

 

/* 
 * INFO262 - S7. Sensing: Potentiometers
 * 
 * Author: Nicholas Kong
 * Date: September 20, 2011
 */
 
#define MIN_DELAY 10
#define MAX_DELAY 70
 
int fadeSpeedPot = 0;
int colorPot = 1;
 
int redPin   = 9;   // Red LED,   connected to digital pin 9
int bluePin  = 10;  // Blue LED,  connected to digital pin 10
int greenPin = 11;  // Green LED, connected to digital pin 11  
 
float inc;
 
typedef struct {
  int r;
  int g;
  int b;
} RGBval;
 
RGBval aRGB = {0,0,255};
RGBval bRGB = {0,255,0};
RGBval curRGB = {0,0,255};
boolean aToB  = true;
 
struct {
  boolean r;
  boolean g;
  boolean b;
} incStruct;
 
int loopDelay = MAX_DELAY;
 
void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  
  Serial.begin(9600); // Start serial input
  
  initIncStruct();
  
  // Set pins to the start RGB value
  analogWrite(redPin,   curRGB.r);
  analogWrite(greenPin, curRGB.g);
  analogWrite(bluePin,  curRGB.b);
}
 
void loop () {
 
  setLoopDelay();
  setBColor();
  
  if(aToB) {
   if(curRGB.r == bRGB.r && curRGB.g == bRGB.g && curRGB.b == bRGB.b) {
     aToB = false;
   }
  } else {
   if(curRGB.r == aRGB.r && curRGB.g == aRGB.g && curRGB.b == aRGB.b) {
     aToB = true;
   }    
  }
  
  initIncStruct();
  increment();
  analogWrite(redPin,   curRGB.r);
  analogWrite(greenPin, curRGB.g);
  analogWrite(bluePin,  curRGB.b);
  
  delay(loopDelay);
}
 
// Set fading speed using one pot
void setLoopDelay() {
  inc = ((float)analogRead(fadeSpeedPot))/((float)1024);
  loopDelay = MIN_DELAY+inc*(MAX_DELAY-MIN_DELAY);
}
 
// Set the color that blue fades to using the other pot.
// Extremes are pure red and pure green.
void setBColor() {
  inc = ((float)analogRead(colorPot))/((float)1024);
  bRGB.r = 255*inc;
  bRGB.g = 255*(1-inc);
  bRGB.b = 0;
}
 
// Set the fade increment for colors
void initIncStruct() {
  incStruct.r = aRGB.r < bRGB.r;
  incStruct.g = aRGB.g < bRGB.g;
  incStruct.b = aRGB.b < bRGB.b;
  
  if(!aToB) {
    incStruct.r = !incStruct.r;
    incStruct.g = !incStruct.g;
    incStruct.b = !incStruct.b;
  }
}
 
// Change the current color
void increment() {
  curRGB.r = incStruct.r ? min(curRGB.r + 5, aToB ? bRGB.r : aRGB.r) : 
                           max(curRGB.r - 5, aToB ? bRGB.r : aRGB.r);
  curRGB.g = incStruct.g ? min(curRGB.g + 5, aToB ? bRGB.g : aRGB.g) :
                           max(curRGB.g - 5, aToB ? bRGB.g : aRGB.g);
  curRGB.b = incStruct.b ? min(curRGB.b + 5, aToB ? bRGB.b : aRGB.b) :
                           max(curRGB.b - 5, aToB ? bRGB.b : aRGB.b);
}
 
Lab3-colorfader-nkong.jpg
0
Your rating: None