Lab 2: Ping Pong Diffuser

Submitted by elee on Wed, 02/13/2013 - 06:55
elee's picture

Description

Overall

A diffuser was created using a mixture of red, green, and blue LEDs. The code used the value of 25 times the number of times r/g/b is entered to set the color and keep fading.

We learned about the Arduino's abilities to communicate with Serial.

Diffuser

For the diffuser I used a ping pong ball. Out of all the material that I could find, I found it the most apt for our conditions. First I cut out a hole on one side of the ping pong ball to put the LEDs through until I realized the light was very harsh and instead of evenly distributing, was very unified at certain points. To alleviate this, I stuffed the ping pong ball with cotton filling and thin pieces of foam paper.

Components

  • 3 LEDs (Red, Green, and Blue)
  • 3 Resistors (220 Ohm)
  • Arduino Uno
  • Breadboard
  • Several wires
  • Battery pack power attachment
  • Potentiometer
  • Ping pong ball
  • Cotton filling
  • Thin foam paper

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 <clay.shirky@nyu.edu>
*/

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

// Program variables
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;

char serInString[100]; // array holding bytes of string - 100 characters
char colorCode;
int colorValue;

int r = 0;
int g = 0;
int b = 0;
int inputExists = 0;
int colorChanged = 0;

int i = 0;     // Loop counter    
// int wait = 10; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 0; // 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 (DEBUG) {           // If we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
  }
  Serial.begin(9600);
  Serial.println("Enter color command (ex. rrrgb)");
}

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

// Main program
void loop()
{
  // clear string
  memset(serInString, 0, 100);
  // read serial port in and create string of read
  readSerialString(serInString);
  int counter = 0;
 
  colorCode = serInString[0];
  while (colorCode == 'r' || colorCode == 'g' || colorCode == 'b') {
    if (inputExists == 0) {
      r = 0;
      g = 0;
      b = 0;
    }
    inputExists = 1;
    if (colorCode == 'r') {
      r += 1;
    }
    else if (colorCode == 'g') {
      g += 1;
    }
    else if (colorCode == 'b') {
      b += 1;
    }
    counter += 1;
    colorCode = serInString[counter];
  }
 
  if (inputExists == 1) {
    colorChanged = 1;
    if ( r <= 10 ) { r = 25*r; }
    else { r = 255; }
    if ( g <= 10 ) { g = 25*g; }
    else { g = 255; }
    if ( b <= 10 ) { b = 25*b; }
    else { b = 255; }
    Serial.print(' ');
    Serial.print(r);
    Serial.print(g);
    Serial.print(b);
    Serial.print(' ');
    analogWrite(redPin, r);
    analogWrite(greenPin, g);
    analogWrite(bluePin, b);
    inputExists = 0;
  }
  else {
    if (colorChanged == 1) { i = 1; colorChanged = 0; }
    i += 1;      // Increment counter
    if (i < 255) // First phase of fades
    {
      r   -= 1; // Red down
      g += 1; // Green up
    }
    else if (i < 509) // Second phase of fades
    {
      g -= 1; // Green down
      b  += 1; // Blue up
    }
    else if (i < 763) // Third phase of fades
    {
      r  += 1; // Red up
      b -= 1; // Blue down
    }
    else // Re-set the counter, and start the fades again
    {
      i = 1;
    }
    if (r < 0) { r = 0; } else if (g < 0) { g = 0; } else if (b < 0) { b = 0; }
    Serial.println();
    Serial.print(r);
    Serial.print(' ');
    Serial.print(g);
    Serial.print(' ');
    Serial.print(b);
    Serial.println();
    analogWrite(redPin,   r);   // Write current values to LED pins
    analogWrite(greenPin, g);
    analogWrite(bluePin,  b);
  }
 

  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
    }
  }
  sensorValue = analogRead(sensorPin);
 
  if (sensorValue < 1) {
    sensorValue = 1;
  }
 
  delay(sensorValue); // Pause for 'wait' milliseconds before resuming the loop
}

Video of it glowing: http://youtu.be/IFc_Gn2MsMs

Cotton and Foam in Ping Pong to Diffuse
Light Close Up
Light Up
Overall View
Tape to Diffuse Harsh Light
0
Your rating: None
Drupal theme by Kiwi Themes.