Tutorial.LEDCross-fadesWithPotentiometer History

Hide minor edits - Show changes to markup

April 28, 2006, at 08:04 PM by Clay Shirky -
Changed lines 3-4 from:
  • Code for making one potentiometer control 3 LEDs, red, green and blue, or one tri-color LED
  • The program cross-fades from red to green, green to blue, and blue to red
to:
  • Code for making one potentiometer control 3 LEDs, red, grn and blu, or one tri-color LED
  • The program cross-fades from red to grn, grn to blu, and blu to red
Changed lines 15-18 from:

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

to:

int redPin = 9; // Red LED, connected to digital pin 9 int grnPin = 10; // Green LED, connected to digital pin 10 int bluPin = 11; // Blue LED, connected to digital pin 11

Changed lines 20-23 from:

int redVal = 0; // Variables to store the values to send to the pins int greenVal = 0; int blueVal = 0;

to:

int redVal = 0; // Variables to store the values to send to the pins int grnVal = 0; int bluVal = 0;

Changed lines 28-30 from:
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT); 
to:
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT); 
Changed lines 46-48 from:
    redVal   = 256 - potVal;  // Red from full to off
    greenVal = potVal;        // Green from off to full
    blueVal  = 1;             // Blue off
to:
    redVal = 256 - potVal;  // Red from full to off
    grnVal = potVal;        // Green from off to full
    bluVal = 1;             // Blue off
Changed lines 54-56 from:
    redVal   = 1;            // Red off
    greenVal = 256 - potVal; // Green from full to off
    blueVal  = potVal;       // Blue from off to full
to:
    redVal = 1;            // Red off
    grnVal = 256 - potVal; // Green from full to off
    bluVal = potVal;       // Blue from off to full
Changed lines 62-64 from:
    redVal   = potVal;       // Red from off to full
    greenVal = 1;            // Green off
    blueVal  = 256 - potVal; // Blue from full to off
to:
    redVal = potVal;       // Red from off to full
    grnVal = 1;            // Green off
    bluVal = 256 - potVal; // Blue from full to off
Changed lines 66-69 from:
  analogWrite(redPin,   redVal);   // Write values to LED pins
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin,  blueVal);  
to:
  analogWrite(redPin, redVal);   // Write values to LED pins
  analogWrite(grnPin, grnVal); 
  analogWrite(bluPin, bluVal);  
Changed lines 79-80 from:
      Serial.print("G:");    // Repeat for green and blue...
      Serial.print(greenVal);
to:
      Serial.print("G:");    // Repeat for grn and blu...
      Serial.print(grnVal);
Changed line 83 from:
      Serial.println(blueVal); // println, to end with a carriage return
to:
      Serial.println(bluVal); // println, to end with a carriage return
April 28, 2006, at 06:11 PM by Clay Shirky -
Changed line 74 from:
                             // Serial output in 0004 format
to:
                             // Serial output using 0004-style functions
April 28, 2006, at 06:10 PM by Clay Shirky - Noted that debugging code assumes 0004
Added line 5:
  • Debugging code assumes Arduino 0004, as it uses Serial.begin()-style functions
Changed line 32 from:
    Serial.begin(9600);  // ...set up the serial ouput
to:
    Serial.begin(9600);  // ...set up the serial ouput in 0004 format
Changed line 74 from:
to:
                             // Serial output in 0004 format
April 19, 2006, at 03:33 AM by Clay Shirky -
Changed line 40 from:
  if (potVal < 342)  // Lowest third of the potentiometer's range (0-341)
to:
  if (potVal < 341)  // Lowest third of the potentiometer's range (0-340)
Changed line 48 from:
  else if (potVal < 682) // Middle third of potentiometer's range (342-682)
to:
  else if (potVal < 682) // Middle third of potentiometer's range (341-681)
Changed line 56 from:
  else  // Upper third of potentiometer"s range (683-1023)
to:
  else  // Upper third of potentiometer"s range (682-1023)
Changed lines 58-59 from:
    potVal = ( (potVal-682) * 3) / 4; // Normalize to 0-255
to:
    potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255
April 19, 2006, at 03:30 AM by Clay Shirky -
Changed lines 44-46 from:
    redVal   = 255 - potVal;  // Red from full to zero
    greenVal = potVal;        // Green from zero to full
    blueVal  = 0;             // Blue off
to:
    redVal   = 256 - potVal;  // Red from full to off
    greenVal = potVal;        // Green from off to full
    blueVal  = 1;             // Blue off
Changed line 48 from:
  else if (potVal < 683) // Middle third of potentiometer's range (342-682)
to:
  else if (potVal < 682) // Middle third of potentiometer's range (342-682)
Changed lines 52-54 from:
    redVal   = 0;            // Red off
    greenVal = 255 - potVal; // Green from full to zero
    blueVal  = potVal;       // Blue from zero to full
to:
    redVal   = 1;            // Red off
    greenVal = 256 - potVal; // Green from full to off
    blueVal  = potVal;       // Blue from off to full
Changed lines 60-62 from:
    redVal   = potVal;       // Red from zero to full
    greenVal = 0;            // Green off
    blueVal  = 255 - potVal; // Blue from full to zero
to:
    redVal   = potVal;       // Red from off to full
    greenVal = 1;            // Green off
    blueVal  = 256 - potVal; // Blue from full to off
April 18, 2006, at 08:46 PM by Clay Shirky -
Added lines 1-85:
/*
* Code for making one potentiometer control 3 LEDs, red, green and blue, or one tri-color LED
* The program cross-fades from red to green, green to blue, and blue to red
* Clay Shirky <clay.shirky@nyu.edu> 
*/

// INPUT: Potentiometer should be connected to 5V and GND
int potPin = 3; // Potentiometer output connected to analog pin 3
int potVal = 0; // Variable to store the input from the potentiometer

// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED's cathodes should be connected to digital GND
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

// Program variables
int redVal   = 0;   // Variables to store the values to send to the pins
int greenVal = 0;
int blueVal  = 0;

int DEBUG = 1;          // Set to 1 to turn on debugging output

void setup()
{
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT); 
  if (DEBUG) {           // If we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput
  }
}

// Main program
void loop()
{
  potVal = analogRead(potPin);   // read the potentiometer value at the input pin

  if (potVal < 342)  // Lowest third of the potentiometer's range (0-341)
  {                  
    potVal = (potVal * 3) / 4; // Normalize to 0-255

    redVal   = 255 - potVal;  // Red from full to zero
    greenVal = potVal;        // Green from zero to full
    blueVal  = 0;             // Blue off
  }
  else if (potVal < 683) // Middle third of potentiometer's range (342-682)
  {
    potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255

    redVal   = 0;            // Red off
    greenVal = 255 - potVal; // Green from full to zero
    blueVal  = potVal;       // Blue from zero to full
  }
  else  // Upper third of potentiometer"s range (683-1023)
  {
    potVal = ( (potVal-682) * 3) / 4; // Normalize to 0-255

    redVal   = potVal;       // Red from zero to full
    greenVal = 0;            // Green off
    blueVal  = 255 - potVal; // Blue from full to zero
  }
  analogWrite(redPin,   redVal);   // Write values to LED pins
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin,  blueVal);  

  if (DEBUG) { // If we want to read the output
    DEBUG += 1;      // Increment the DEBUG counter
    if (DEBUG > 100) // Print every hundred loops
    {
      DEBUG = 1;     // Reset the counter

      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
    }
  }
}

Share