Tutorial.DimmingLEDs History

Show minor edits - Show changes to markup

April 28, 2006, at 06:07 PM by Clay Shirky - Noted that the debugging code assumes 0004
Added line 6:
  • The debugging code assumes Arduino 0004, as it uses the new Serial.begin()-style functions
April 18, 2006, at 08:45 PM by Clay Shirky - PWM example using timer
Added line 2:
Changed lines 4-5 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 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
Changed lines 9-14 from:

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

to:

// Output

Changed lines 15-20 from:

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

to:

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

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

Changed lines 36-46 from:
  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)
to:
  i += 1;      // Increment counter
  if (i < 255) // First phase of fades
Changed lines 39-43 from:
    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
to:
    redVal   -= 1; // Red down
    greenVal += 1; // Green up
    blueVal   = 1; // Blue low
Changed line 43 from:
  else  // Upper third of potentiometer"s range (683-1023)
to:
  else if (i < 509) // Second phase of fades
Changed lines 45-49 from:
    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
to:
    redVal    = 1; // Red low
    greenVal -= 1; // Green down
    blueVal  += 1; // Blue up
  } 
  else if (i < 763) // Third phase of fades
  {
    redVal  += 1; // Red up
    greenVal = 1; // Green low
    blueVal -= 1; // Blue down
Changed lines 55-60 from:
  analogWrite(redPin,   redVal);   // Write values to LED pins
to:
  else // Re-set the counter, and start the fades again
  {
    i = 1;
  }  

  analogWrite(redPin,   redVal);   // Write current values to LED pins
Changed lines 65-66 from:
    DEBUG += 1;      // Increment the DEBUG counter
    if (DEBUG > 100) // Print every hundred loops
to:
    DEBUG += 1;     // Increment the DEBUG counter
    if (DEBUG > 10) // Print every 10 loops
Added lines 70-71:
      Serial.print(i);
      Serial.print("\t");    // Print a tab
Added line 82:
  delay(wait); // Pause for 'wait' milliseconds before resuming the loop
Changed lines 84-85 from:

@]

to:

@]

April 18, 2006, at 05:19 PM by Clay Shirky -
Added lines 1-84:
/*
* 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
int potPin = 3; // Potentiometer connected to analog pin 3
int potVal = 0; // Variable to store the input from the potentiometer

// Output
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