HW 2: Origami diffuser and key press LED control

jennifer_wang's picture

Description:

I designed a diffuser using a piece of white paper, folding it into an origami ball.  I filled the inside with some cotton to help diffuse the light some more.

My LED control uses processing code to continually accept keystrokes without having to press enter/return after each line.  The color abbreviation (the keys 'r', 'g', or 'b') with the up and down arrow keys increase and decrease the LED, respectively.  The color keys in combination with the shift key and the tab key turns off or (all the way) on the respective LED.

 

Components used:

  • red LED
  • green LED
  • blue LED
  • 3 220 ohm resistors
  • 8 1/2 x 11 in. paper
  • cotton

Code:

arduino code

 

/*
 * Jennifer Wang
 * HW2 arduino code to be used with keystroke_LED_control.pde
 * Adapted from 2006 Tod E. Kurt <tod@todbot.com> http://todbot.com/
 *
 * Load this code onto the arduino, then run keystroke_LED_control on processing
 * and press commands described below.
 *
 * RGB LED control commands:
 * 'r', 'g', or 'b' + up arrow increases by 10
 * 'r', 'g', or 'b' + down arrow decreases by 10
 * shift + 'r', 'g', or 'b' sets LED to OFF (0 brightness)
 * tab + 'r', 'g', or 'b' sets LED to ON (full 255 brightness)
 *
 * Code received from processing keystroke_LED_control.pde via USB serial port:
 ** The color abbreviation and + or - means increase or decrease the color
 *      (e.g. 'r+' means increase red and 'g-' means decrease green)
 ** The color abbreviation with 0 or 1 means turn off or on the LED
 *      (e.g. 'b0' means turn off blue and 'r1' means turn red all the way on)
 * 
 */
 
 
char serInString[100];  // array that will hold the different bytes of the string. 100=100characters;
                        // -> you must state how long the array will be else it won't work properly
char colorCode;
int redVal;
int greenVal;
int blueVal;
//int colorVal;
char colorCommand;
 
int colorInc = 10; // color increment is 10, so LEDs increase or decrease by 10
 
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
 
void setup() {
  redVal = 0;
  greenVal = 0;
  blueVal = 0;
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin,   redVal);   // set them all to OFF
  analogWrite(greenPin, greenVal);   // set them all to OFF
  analogWrite(bluePin,  blueVal);   // set them all to OFF
  Serial.println("Click the processing window and enter color command (e.g. 'r' + up arrow, 'r' + shift or tab) :");  
}
 
void loop () {
  // clear the string
  memset(serInString, 0, 100);
    
    //read the serial port and create a string out of what you read
    readSerialString(serInString);
    colorCode = serInString[0];
    
    /* red LED with valid code */
    if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
      colorCommand = serInString[1];
      serInString[0] = 0;                   // indicates we've used this string
      if(colorCode == 'r' & (colorCommand == '+' || colorCommand == '-' || colorCommand == '0' || colorCommand == '1')) {
        if(colorCommand == '+') {
          if(redVal > 245) redVal = 255; // maximum is 255
          else redVal += colorInc;
        }
        else if (colorCommand == '-') {
          if(redVal < 10) redVal = 0; // minimum is 0
          else redVal -= colorInc;
        }
        else if (colorCommand == '0') redVal = 0;
        else if (colorCommand == '1') redVal = 255;
        analogWrite(redPin, redVal);
      }
      
      /* green LED with valid code */
      else if(colorCode == 'g' & (colorCommand == '+' || colorCommand == '-' || colorCommand == '0' || colorCommand == '1')) {
        if(colorCommand == '+') {
          if(greenVal > 245) greenVal = 255; // maximum is 255
          else greenVal += colorInc;
        }
        else if (colorCommand == '-') {
          if(greenVal < 10) greenVal = 0; // minimum is 0
          else greenVal -= colorInc;
        }
        else if (colorCommand == '0') greenVal = 0;
        else if (colorCommand == '1') greenVal = 255;
        analogWrite(greenPin, greenVal);
      }
      
      /* blue LED with valid code */
      else if(colorCode == 'b' & (colorCommand == '+' || colorCommand == '-' || colorCommand == '0' || colorCommand == '1')) {
        if(colorCommand == '+') {
          if(blueVal > 245) blueVal = 255; // maximum is 255
          else blueVal += colorInc;
        }
        else if (colorCommand == '-') {
          if(blueVal < 10) blueVal = 0; // minimum is 0
          else blueVal -= colorInc;
        }
        else if (colorCommand == '0') blueVal = 0;
        else if (colorCommand == '1') blueVal = 255;
        analogWrite(bluePin, blueVal);
      }
  }
  
  delay(100);  // wait a bit, for serial data
}
 
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
  int i = 0;
  if(!Serial.available()) {
    return;
  }
  while (Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}
 

-----------------------------------------------------------

processing code

 

/**
 * Jennifer Wang
 * HW2 processing code to accept keystrokes for LED control
 * Use with arduino code hw2_LED_control.pde
 *
 * Adapted from Option 2 on http://wiki.processing.org/w/Multiple_key_presses
 *
 * Polls keyboard for strokes to increase or decrease red, green, blue LEDs
 * and feeds this data into the USB serial for the arduino by sending codes.
 *
 * The codes sent to the arduino USB serial port are:
 ** The color abbreviation and + or - means increase or decrease the color
 *      (e.g. 'r+' means increase red and 'g-' means decrease green)
 ** The color abbreviation with 0 or 1 means turn off or on the LED
 *      (e.g. 'b0' means turn off blue and 'r1' means turn red all the way on)
 * 
 * Keyboard strokes accepted by this processing code (and used with 
 * hw2_LED_control.pde arduino code are:
 ** 'r', 'b', or 'g' with the up arrow increase brightness
 ** 'r', 'b', or 'g' with the down arrow decrease brightness
 ** 'r', 'b', or 'g' with shift key turns off the LED
 ** 'r', 'b', or 'g' with tab key turns LED all the way on
 */
 
 
import processing.serial.*;
 
Serial port;
boolean[] keys = new boolean[526];
 
void setup() {
  println(Serial.list()); // List COM-ports
 
  //select second com-port from the list
  port = new Serial(this, Serial.list()[0], 9600); 
}
 
void draw(){}
 
boolean checkKey(int k)
{
  if (keys.length >= k) {
    return keys[k];  
  }
  return false;
}
 
void keyPressed()
  keys[keyCode] = true;
  println(KeyEvent.getKeyText(keyCode));
  
  /* red LED */
  if(checkKey(KeyEvent.VK_R) && checkKey(UP)) { // 'r' and up arrow pressed
    println("increasing red LED");
    port.write("r+");
  }
  else if(checkKey(KeyEvent.VK_R) && checkKey(DOWN)) { // 'r' and down arrow pressed
    println("decreasing red LED");
    port.write("r-");
  }
  else if(checkKey(KeyEvent.VK_R) && checkKey(SHIFT)) { // 'r' and shift pressed
    println("turning red LED OFF");
    port.write("r0");
  }
  else if(checkKey(KeyEvent.VK_R) && checkKey(TAB)) { // 'r' and tab pressed
    println("turning red LED 100% ON");
    port.write("r1");
  }
  
  /* green LED */
  else if(checkKey(KeyEvent.VK_G) && checkKey(UP)) { // 'g' and up arrow pressed
    println("increasing green LED");
    port.write("g+");
  }
  else if(checkKey(KeyEvent.VK_G) && checkKey(DOWN)) { // 'g' and down arrow pressed
    println("decreasing green LED");
    port.write("g-");
  }
  else if(checkKey(KeyEvent.VK_G) && checkKey(SHIFT)) { // 'g' and shift pressed
    println("turning green LED OFF");
    port.write("g0");
  }
  else if(checkKey(KeyEvent.VK_G) && checkKey(TAB)) { // 'g' and tab pressed
    println("turning green LED 100% ON");
    port.write("g1");
  }
  
  /* blue LED */
  else if(checkKey(KeyEvent.VK_B) && checkKey(UP)) { // 'b' and up arrow pressed
    println("increasing blue LED");
    port.write("b+");
  }
  else if(checkKey(KeyEvent.VK_B) && checkKey(DOWN)) { // 'b' and down arrow pressed
    println("decreasing blue LED");
    port.write("b-");
  }
  else if(checkKey(KeyEvent.VK_B) && checkKey(SHIFT)) { // 'b' and shift pressed
    println("turning blue LED OFF");
    port.write("b0");
  }
  else if(checkKey(KeyEvent.VK_B) && checkKey(TAB)) { // 'b' and tab pressed
    println("turning blue LED 100% ON");
    port.write("b1");
  }
}
 
void keyReleased()
  keys[keyCode] = false; 
}
hw 2 diffuser
hw 2 circuit
0
Your rating: None