The Spectrumizer !

Brief :

Welcome to the Spectrumizer. In this assignment i used three LEDs to generate 360 colors in the color spectrum wheel. To go towards the red side of the spectrum one can press ‘r’ through the serial input and press ‘b’ to go towards the blue side of the spectrum. I now use this setup to create mood lighting in my room !

Components:

  • Arduino Uno
  • Breadboard
  • 3 LEDs (rgb)
  • 3 220Ω Resistors
  • Jumper Wires
  • USB cable

Code :

/*
Spectrumizer

This program uses arrow keys to change colors on a lamp.
The colors are within the color spectrum

I have used the following code to map HSV values to RGB
The r and b change values from 0 to 360 and vice versa to show 360 different colors

*/

// The three primary colour LEDs, driven as analgue outputs (actually PWM, but
// close enough for our analogue eyes).

int ledPinRed = 9; // Red LED connected to analogue out pin
int ledPinGrn = 10; // Green LED connected to analogue out pin
int ledPinBlu = 11; // Blue LED connected to analogue out pin
// Constants to define the ranges.

const int hueRedLow = 0;
const int hueRedHigh = 255;
const int hueBlue = 170;

// The size of the angle of one sector (1/6 of a colour wheel), and of a complete
// cycle of the colour wheel.

const int angleMin = 0;
const int angleSector = 60;
const int angleMax = 360;
//range of brighness from 0 to 255

const int brightMin = 0;
const int brightMax = 255;

// The hue is the range 0 (red) to 255 (blue)
// The brightness ranges from 0 (dark) to 255 (full brightness)

int hue, brightness;

// The saturation is fixed at 255 (full) to remove blead-through of different
// colours.
// It could be linked separately if a demonstration of hue is desired.

const int saturation = 255;

// The brightess of each LED (0 to 255).
//defining the start of the color wheel
int color = 0;

unsigned int r, g, b;

void setup() {
// Setting a baud rate for communication via USB.
Serial.begin(9600);
Serial.println("Welcome to the Spectrumizer");
Serial.println("Use the 'r' key to go towards red in the color wheel");
Serial.println("Use the 'b' key to go towards blue in the color wheel");

// Set LED pins to output.
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinGrn, OUTPUT);
pinMode(ledPinBlu, OUTPUT);
}

void loop() {

int key = 0;
// The brightness is fixed at full for the colour wheel.
brightness = 255;
//using serial.read function to read keystrokes from the keyboard
if (Serial.available() > 0) {
// read the incoming byte:
key = Serial.read();
}

if(key == 114)
{
if(color != 0)
{
for( int i = 0 ;i <10;i++)
{
color = color - 1;
// Colour wheel (red to red, wrapped around in a cycle).
hue = map(color, angleMin, angleMax, hueRedLow, hueRedHigh);
// Do the conversion.
HSBToRGB(hue, saturation, brightness, &r, &g, &b);

analogWrite(ledPinRed, r);
analogWrite(ledPinGrn, g);
analogWrite(ledPinBlu, b);
delay(50);
}
}
}
else if(key == 98)
{
if(color != 360)
{
for( int i = 0 ;i <10;i++)
{
color = color + 1;
// Colour wheel (red to red, wrapped around in a cycle).
hue = map(color, angleMin, angleMax, hueRedLow, hueRedHigh);
// Do the conversion.
HSBToRGB(hue, saturation, brightness, &r, &g, &b);

analogWrite(ledPinRed, r);
analogWrite(ledPinGrn, g);
analogWrite(ledPinBlu, b);
delay(50);
}
}
}
}

// This function taken from here:
// http://eduardofv.com/read_post/179-Arduino-RGB-LED-HSV-Color-Wheel-

void HSBToRGB(
unsigned int inHue, unsigned int inSaturation, unsigned int inBrightness,
unsigned int *oR, unsigned int *oG, unsigned int *oB )
{
if (inSaturation == 0)
{
// achromatic (grey)
*oR = *oG = *oB = inBrightness;
}
else
{
unsigned int scaledHue = (inHue * 6);
unsigned int sector = scaledHue >> 8; // sector 0 to 5 around the color wheel
unsigned int offsetInSector = scaledHue - (sector << 8); // position within the sector
unsigned int p = (inBrightness * ( 255 - inSaturation )) >> 8;
unsigned int q = (inBrightness * ( 255 - ((inSaturation * offsetInSector) >> 8) )) >> 8;
unsigned int t = (inBrightness * ( 255 - ((inSaturation * ( 255 - offsetInSector )) >> 8) )) >> 8;

switch( sector ) {
case 0:
*oR = inBrightness;
*oG = t;
*oB = p;
break;
case 1:
*oR = q;
*oG = inBrightness;
*oB = p;
break;
case 2:
*oR = p;
*oG = inBrightness;
*oB = t;
break;
case 3:
*oR = p;
*oG = q;
*oB = inBrightness;
break;
case 4:
*oR = t;
*oG = p;
*oB = inBrightness;
break;
default: // case 5:
*oR = inBrightness;
*oG = p;
*oB = q;
break;
}
}
}

Check out the video here

Whimsical light!

Description:

The brightness of the R, G, B LEDs is controlled via serial commands. Once we have compiled and uploaded the sketch in our Arduino, we open the Serial Monitor (tools  → serial monitor). When the Arduino is ready we will read the message “WELCOME to the WHIMSICAL light!” in the serial monitor window, and then we will be prompted to enter the color command.

The structure of the color command is “<colorCode><colorCode>…<colorCode>”, where “colorCode” is one of “r”,”g”,or “b”. The intensity of each light is determined by the number of times the color code is typed (i.e., once: LED off, twice: low, three times: medium low, four times: medium, five times: medium high, and six times: high).

E.g:

“r”    turns the red LED off.
“gg”   turns the green LED to low brightness
“bbbb” turns the blue LED to medium brightness
If an unrecognized character is received, you will get a surprise!

Components

  • 1 Arduino Uno
  • 3 resistor (220 ohms)
  • 1 red LED, 1 blue LED, 1 green LED
  • 1 USB cable
  • Solid core wire
  • Bubble wrap
  • White plastic piece (see photo)
  • Laptop

Code

char serInString[100];  // array that will hold the different bytes of the string. 100=100characters; [char data type that takes up 1 byte of memory that stores a character value]

// -> you must state how long the array will be else it won’t work properly

char colorCode;         // “colorCode” is one of “r”,”g”,or “b”
int colorVal;           // “colorVal” is a number 0 to 255
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
int flag = 1;
int cont = 0;

 

void setup() {            //********* Why don’t we declare all the variables here of before?
pinMode(redPin,   OUTPUT);   // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin,  OUTPUT);
Serial.begin(9600);           // Sets the data rate in bits per second (baud) for serial data transmission.
analogWrite(redPin,   127);   // set them all to mid brightness
analogWrite(greenPin, 127);   // set them all to mid brightness
analogWrite(bluePin,  127);   // set them all to mid brightness

Serial.println(“WELCOME to the WHIMSICAL ligth!”);
Serial.print(‘\n’);
}

void loop () {
if (flag == 1){
Serial.println(“Please enter the 6 digit color command.”);
Serial.println(“(e.g. ‘r’ for turning off the red ligh, rr for a low red light, ggg for a medium green light, or ‘bbbb’ for a high blue light):”);
flag = 0;
}
memset(serInString, 0, 100);   //set all bytes in the buffer to 0. Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
readSerialString(serInString);  //read the serial port and create a string out of what you read
colorCode = serInString[0];

if( colorCode == ‘r’ || colorCode == ‘g’ || colorCode == ‘b’ ) {
Serial.print(“colorCode: “);
Serial.println(colorCode);
for (int x=0; x<7; x++){
if(serInString[x] == colorCode){
cont++;
}
}
Serial.print(“Setting color “);
Serial.print(colorCode);
if (cont == 1){
Serial.println(” to 0″);
Serial.print(‘\n’);
colorVal=0;
}
else if(cont == 2){
Serial.println(” to LOW”);
Serial.print(‘\n’);
colorVal=51;
}
else if(cont == 3){
Serial.println(” to MEDIUM LOW”);
Serial.print(‘\n’);
colorVal=102;
}
else if(cont == 4){
Serial.println(” to MEDIUM”);
Serial.print(‘\n’);
colorVal=153;
}
else if(cont == 5){
Serial.println(” to MEDIUM HIGH”);
Serial.print(‘\n’);
colorVal=204;
}
else if(cont == 6){
Serial.println(” to HIGH”);
Serial.print(‘\n’);
colorVal=255;
}
cont=0;
serInString[0] = 0;                   // indicates we’ve used this string. Reseting the value to 0 (we have already backed up its value in colorCode and colorVal.? WHY DO YOU NEED TO INDICATE THIS? AT THE BEGINNING OF THE LOOP WE ARE SETTING ALL THE ARRAY TO 0
if(colorCode == ‘r’)
analogWrite(redPin, colorVal);
else if(colorCode == ‘g’)
analogWrite(greenPin, colorVal);
else if(colorCode == ‘b’)
analogWrite(bluePin, colorVal);
flag = 1;
}
if (colorCode != ‘r’ && colorCode != ‘b’ && colorCode != ‘g’ && colorCode != 0){
int aleatorio=random(15);
for(int y=0;y<255;y=y+random(10,30)){
analogWrite(redPin, y);
delay(80);
}
for(int y=0;y<256;y=y+random(4,25)){
analogWrite(bluePin, y);
delay(80);
}
for(int y=0;y<256;y=y+random(12,30)){
analogWrite(greenPin, y);
delay(80);
}
analogWrite(redPin, 0);
analogWrite(bluePin, 0);
analogWrite(greenPin, 0);
serInString[0] = 0;
}
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;
//available(): Get the number of bytes (characters) available for reading from the serial port.
//Syntax: Serial.available()
//Returns: the number of bytes available to read
if(!Serial.available()) {  //gets into the if when there’s no data available through the serial port. If there’s 0 bytes to be read, !Serial.available()=1 (the negation), and enters to the if and returns to the main loop.
Return;
//Serial.print(” Serial available value: “);
//Serial.println(Serial.available());
while (Serial.available()>0) { // while the value being returned by serial.available is different from 0 (meaning there are bytes to be read), reads the info coming through the port and stores it in the specified array
strArray[i] = Serial.read(); //Serial.read(): Returns the first byte of incoming serial data available (or -1 if no data is available). The first byte received is stored in posiiton [0]
i++;
}
}

Lab 2 – Tree, LED, and Arduino

For this assignment, I used a miniature tree as the diffuser. I place all three LEDs onto the miniature tree while using leaves as background. I’ve taken the cross-fading code and added some personal flavor to it. By allowing the user to control the blue color while alternating the speed of fading through out the loops. There are a total of three different fading stages with varying speed. The outcome is not as ideal as some other types of diffuser that will actually allow you to see the fading and the mixing of colors. But I wanted to see how the LED lights would look on a miniature tree and see if it can resemble a christmas tree.

Components:

  • Arduino Uno
  • Breadboard
  • 3 LEDs (rgb)
  • 3 220Ω Resistors
  • jumper wires
  • USB cable
  • laptop

 

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

char b_input[100];
int b_value = 0;

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

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

int i = 0;     // Loop counter    
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 1; // 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 we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
    Serial.println("just continue to send me b"); 

}

// Main program
void loop()
{
  i += 20;      // Increment counter
  if (i < 255) // First phase of fades
  {
    greenVal += 1; // Green up
    redVal   = 1; // Red low
    blueVal += 1; // Blue up
  }
  else if (i < 509) // Second phase of fades
  {
    greenVal -= 5; // Green down
    redVal  += 5; // red up
    blueVal -= 5; // Blue up
  } 
  else if (i < 763) // Third phase of fades
  {
    greenVal = 10; // Green low
    redVal -= 10; // Red down
    blueVal += 10; // Blue up
  }
  else // Re-set the counter, and start the fades again
  {
    i = 1;
  }  
  memset(b_input, 0, 100);   
      
  readSerialString(b_input);

  if(b_input[0] == 'b'){
    b_value = b_value + 1;
    Serial.println(b_value); 
  }
  if (b_value == 13) {
    b_value = 0;
  }
  
  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin,  blueVal + b_value*20);  
  
//  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
//    }
//  }
  delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}

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

Video:

Tree & LED

Diffuser and Changing Lights

IMG_1514Description:

In this assignment, we expanded our 1 blinking LED light circuit to be 3 LED blinking lights, and then explored how different diffusers change how we perceive the light, and how we can modify the code to take user input.

I looked through past student’s presentations for examples (http://courses.ischool.berkeley.edu/i290-13/f07/assignments/39.html) and I decided to play around with users increasing/decreasing the LED light brightness. You increase the light with a capital letter (R = red, B = blue, G = green) and decrease with a lowercase letter.

For the diffuser I tried a few different techniques. I initially used the cotton provided in class, and decided that I wanted to see how colored diffusers made a difference. I found some black tissue paper and wrapped it around, and put all of it in a uniqlo bag. It worked really well if the brightness was at max.

Components:

  • 1x Arduino Uno
  • 1x Breadboard
  • Breadboard wires
  • 3x LEDs
    • 1 Red
    • 1 Green
    • 1 Blue
  • 3x 220Ω Resistors
  • USB cable

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
* Clay Shirky &lt;clay.shirky@nyu.edu&gt;
*
* Adapted by Ken-ichi Ueda &lt;kueda@ischool.berkeley.edu&gt;
* Adapted by Michelle Carney from http://courses.ischool.berkeley.edu/i290-13/f07/assignments/39.html
*/

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

// Program variables
int redVal= 127;
int greenVal = 10;
int blueVal = 10;
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;

int wait = 5; // millesconds to wait btwn fade increments
int inc = 10; // increment value for brightening or dimming LEDs
int i = 0; // Loop counter
char cmd; // holds a command character
char tled; // target LED of a command ('r', 'g', 'b')

void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
writeAllValues();

Serial.println("Commands:");
Serial.println(" r -- decreases red LED brightness by 10");
Serial.println(" RRR -- increases red LED brightness by 30");
Serial.println(" fr -- fade out red LED");
Serial.println(" Fr -- fade in red LED to max brightness");
Serial.println();
Serial.println("Enter a command: ");
}

// Main program
void loop()
{
readSerialString(serInString, 100);
cmd = serInString[0];

// fade controls
if (cmd == 'f') {
//Serial.println("Fade out requested...");//test
tled = serInString[1];
fadeOut(tled);
}
else if (cmd == 'F') {
//Serial.println("Fade in requested...");//test
tled = serInString[1];
fadeIn(tled);
}
// brightness control: look for rgb chars, uppercase brightens and lowercase
// dims
else if (cmd == 'r' || cmd == 'R' || cmd == 'g' || cmd == 'G' || cmd == 'b' || cmd == 'B') {
Serial.println("Changing brightness...");//test
for(i=0; i &lt; 100 &amp;&amp; serInString[i] != '\0'; i++) {
//Serial.println("Looping over brightness commands...");//test
switch (serInString[i]) {
case 'r':
redVal -= inc;
break;
case 'R':
redVal += inc;
break;
case 'g':
greenVal -= inc;
break;
case 'G':
greenVal += inc;
break;
case 'b':
blueVal -= inc;
break;
case 'B':
blueVal += inc;
break;
}
}

trimValues();
writeAllValues();
}

resetSerialString(serInString, 100);
delay(100); // wait a bit, for serial data
}

// ensure brightness remains within 0-255 range
void trimValues() {
if (redVal &gt; 255) {
redVal = 255;
}
else if (redVal &lt; 0) {
redVal = 0;
}
if (greenVal &gt; 255) {
greenVal = 255;
}
else if (greenVal &lt; 0) {
greenVal = 0;
}
if (blueVal &gt; 255) {
blueVal = 255;
}
else if (blueVal &lt; 0) {
blueVal = 0;
}
}

// write all brightness values to the pins
void writeAllValues() {
//Serial.println("Writing values...");//test
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}

// fade in LED in by pin number
void fadeIn(int pin) {
Serial.print("Fading in pin ");
Serial.print(pin);
Serial.println("...");
switch (pin) {
case 9:
while (redVal &lt; 255) {
redVal += 1;
analogWrite(pin, redVal);
delay(wait);
}
break;
case 10:
while (greenVal &lt; 255) {
greenVal += 1;
analogWrite(pin, greenVal);
delay(wait);
}
break;
case 11:
while (blueVal &lt; 255) {
blueVal += 1;
analogWrite(pin, blueVal);
delay(wait);
}
break;
}
}

// fade in LED in by color
void fadeIn(char colorCode) {
Serial.print("Fading in ");
Serial.print(colorCode);
Serial.println(" pin...");
switch (colorCode) {
case 'r':
fadeIn(redPin);
break;
case 'g':
fadeIn(greenPin);
break;
case 'b':
fadeIn(bluePin);
break;
}
}

// fade out LED in by pin number
void fadeOut(int pin) {
Serial.print("Fading out pin ");
Serial.print(pin);
Serial.println("...");
switch (pin) {
case 9:
while (redVal &gt; 0) {
redVal -= 1;
analogWrite(pin, redVal);
delay(wait);
}
break;
case 10:
while (greenVal &gt; 0) {
greenVal -= 1;
analogWrite(pin, greenVal);
delay(wait);
}
break;
case 11:
while (blueVal &gt; 0) {
blueVal -= 1;
analogWrite(pin, blueVal);
delay(wait);
}
break;
}
}

// fade out LED in by color
void fadeOut(char colorCode) {
switch (colorCode) {
case 'r':
fadeOut(9);
break;
case 'g':
fadeOut(10);
break;
case 'b':
fadeOut(11);
break;
}
}

// full input arr with null values
void resetSerialString (char *strArray, int length) {
for (int i = 0; i &lt; length; i++) {
strArray[i] = '\0';
}
}

//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray, int maxLength) {
int i = 0;

if(!Serial.available()) {
return;
}
while (Serial.available() &amp;&amp; i &lt; maxLength) {
strArray[i] = Serial.read();
i++;
}
}

Who you gonna call? Ghostbusters

For this assignment, I created a diffuser using my LEGO Ghostbusters Car. I wrapped the 3 LED’s within a semi-transparent paper packet and placed the wrapped LED’s within the main section of the car which was surrounded LEGO glass pieces.

I set up the commands as weapon modes of the Ghostbuster Car, in case ghosts were to attack. Each attack had a different effect through the LED.

The three weapon modes were:
ProtonGun – Emitted radiating pulses of Red, Green and Blue light
IonCannon – Alternates between Violet and Yellow
NeutronaGun – The interior of the car becomes Orange

Components:

  • Arduino Uno
  • Breadboard
  • 3 LEDs (rgb)
  • 3 220Ω Resistors
  • jumper wires
  • USB cable
  • laptop

/* 
* Ghostbusters
* ---------------
* Serial commands control the brightness of R,G,B LEDs 
*
* Command structure is "&lt;WeaponMode&gt;", where "WeaponMode" is
* one of "ProtonGun","IonCannon",or "NeutronaWand"
* E.g. "ProtonGun" Emitted radiating pulses of Red, Green and Blue light 
* "IonCannon" Alternates between Violet and Yellow
* "NeutronaWand" The interior of the car becomes Orange
*
* Created 11 September 2016
*/
String weaponmode;
char serInString[200]; // array that will hold the different bytes of the string. 100=100characters;
// -&gt; you must state how long the array will be else it won't work properly
char weaponcolor;
int colorVal;
int wait = 5;
int j = 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;

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

void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT); 
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 0); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 0); // set them all to mid brightness
Serial.println("Ghosts are coming !! Choose your weapon mode:"); 
}

void loop () {
// clear the string
memset(serInString, 0, 200);
readSerialString(serInString);
//read the serial port and create a string out of what you read
 
weaponmode = String(serInString);
weaponcolor = serInString[0];
Serial.println(weaponmode); 
if( weaponcolor == 'p' || weaponcolor == 'i' || weaponcolor == 'n')
{
if(weaponcolor == 'p')
{
 
for(int i=0; i&lt;=5; i++)
{
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(200);
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
delay(200);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
delay(200);
}
 
}
else if(weaponcolor == 'i')
{
for(int i =0; i&lt;=10; i++)
{
analogWrite(redPin, 153);
analogWrite(greenPin, 51);
analogWrite(bluePin, 255);
delay(500);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
delay(500);
}
}
else if(weaponcolor == 'n')
{
analogWrite(redPin, 255);
analogWrite(greenPin, 128);
analogWrite(bluePin, 0);
}
}
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++;
}
}


IMG_7295 IMG_7298 IMG_7311 IMG_7312

Jelly Diffuser

I used a water bottle and played around with different materials. I wanted a material that had varying reflective/diffusive properties. Water alone was too clear, shaken with dish soap, too cloudy. Bits of foil floated to the top. Finally, I went with crumpled cling wrap stuffed into a water bottle and filled with paper, to get a translucent, “jelly” feel. I modified the code with the examples from the site so that each additional r entered increases the brightness of the red LED, resetting once maxed at 255.

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

char r_input[100];
int r_value = 0;

// 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 = 255;   // Initial values are Red full, Green and Blue off
int blueVal  = 255;

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

// Main program
void loop()
{
  i += 5;      // Increment counter
  if (i < 255) // First phase of fades
  {
    greenVal += 1; // Green up
    blueVal   = 1; // Blue low
  }
  else if (i < 509) // Second phase of fades
  {
    greenVal -= 1; // Green down
    blueVal  += 1; // Blue up
  } 
  else if (i < 763) // Third phase of fades
  {
    greenVal = 1; // Green low
    blueVal -= 1; // Blue down
  }
  else // Re-set the counter, and start the fades again
  {
    i = 1;
  }  
  memset(r_input, 0, 100);   
      
  readSerialString(r_input);

  //Serial.print(r_input[0]); 
  if(r_input[0] == 'r'){
    r_value = r_value + 1;
    Serial.println(r_value); 
  }
  if (r_value == 5) {
    r_value = 0;
  }
  
  
  analogWrite(redPin,   r_value*51);   // Write current 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 > 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
//    }
//  }
  delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}

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

I had some trouble converting/uploading the video file (I tried mp4, mov and wmv) so I uploaded it here:

LED Diffusion

Description:

After experimenting with and testing out the serial monitor functionality of the Arduino environment and watching the fading color change program work, I chose cone coffee filters as my diffuser of choice. This was after much experimentation–initially, I’d had the three LEDs spaced a few rows apart on the breadboard, but this didn’t allow them to mix very well. So first, I modified my breadboard so that all the LEDs were touching each other.

IMG_1185

I tried diffusing with the large cotton balls in class, but those didn’t allow enough light to pass through. I then tried different configurations of coffee filters, lightbulb housings, plastic storage containers, and more. The cone-type coffee filters worked best for me because they maintained structure around the LEDs, and having two stacked on top of each other allowed for more uniform diffusion.

IMG_1184

I then modified the code so that a user can press r, g, or b and the LEDs will change based on the following equation: (#key presses/10)*255. This gives the percentage of light, and then applies that to the full value of 255.

Components:

  • Arduino Uno
  • Breadboard
  • 3 LEDs (rgb)
  • 3 220Ω Resistors
  • 2 Coffee filters
  • jumper wires
  • USB cable
  • laptop

Code:


/* Modified on 9/7/16 by Molly Mahar
*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "&lt;colorCode&gt;&lt;colorVal&gt;", where "colorCode" is
* one of "r","g",or "b" and "colorVal" is a number 0 to 255.
* E.g. "r0" turns the red LED off.
* "g127" turns the green LED to half brightness
* "b64" turns the blue LED to 1/4 brightness
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt &lt;tod@todbot.com
* http://todbot.com/
*/

char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -&gt; you must state how long the array will be else it won't work properly
char colorCode;
int colorVal;

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() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println("enter color command (e.g. pressing 'r' 5 times will put red LED at 50% brightness) :");
}

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];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
//colorVal = atoi(serInString+1);
colorVal = 0;
for (int i=0; i&lt;strlen(serInString); i++) {
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' )
{
colorVal += 1;
}
}
if (strlen(serInString) &gt; 10){
colorVal = 0;
}

//colorVal = strlen(serInString);
colorVal = (colorVal/10.0)*255;
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}

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

Piano and Forte

I created a paper lantern that lights up and filters the diffused light. On occasion (as in, if positioned properly with a really low overhang), it can make a modest light pattern on my apartment ceiling.

To interact with the lantern, people can type in music dynamic such as ‘p’, ‘pp’, ‘mp’, ‘f’, ‘ff’. For example, ‘p’ stands for piano, and a small soft blue light will fade in and fade out slowly. ‘pp’ fades in and fades out with an even softer blue light twice. ‘F’ on the other hand is loud and lively, a color crescendo. My first version’s ‘F’ fluctuated from red-green to a vibrating blue to magenta. My second iteration is more smooth in transition.

They all fade at variable rates, representing the dynamics of their music symbolism.

Materials

  • 1/2 Blue LED
  • 1 Button
  • Jumper cables
  • Resistors (220Ω)
  • 1 Arduino Uno
  • 1 Breadboard
  • Ping Pong Ball or Translucent Cup
  • 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
*/
// Output
int greenPin = 3; // Red LED, connected to digital pin 9
int bluePin = 6; // Green LED, connected to digital pin 10
int redPin = 11; // Blue LED, connected to digital pin 11

// Program variables
String dynamic;
int blueVal = 0; // Variables to store the values to send to the pins
int greenVal = 0 ; // Initial values are Red full, Green and Blue off
int redVal = 0;
int repeat= 0;
int i = 0; // Loop counter
int wait = 25; // 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);
Serial.begin(9600);
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
Serial.println("enter a music dynamic command: pp, p, mp, f, ff");

}

// Main program
void loop()
{
i = 0;
dynamic = "";
while (Serial.available() == 0) {

}
dynamic = Serial.readString();
if (dynamic.substring(0,1) == "p") {
redVal = 0;
greenVal = 0;
blueVal = 0;
wait = 25;
for (repeat = 0; repeat &lt;dynamic.length(); repeat++) {
i = 0;
for (; i &lt;100; i++) {
greenVal += (1/dynamic.length()) ;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait);
}
for (; i &lt;200;i++) {
greenVal -= (1/dynamic.length());
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait);
}
}
}
if (dynamic == "mp") {
redVal = 0;
greenVal = 0;
blueVal = 0;
wait = 20;
for (repeat = 0; repeat &lt;4; repeat++) {
i = 0;
for (; i &lt;100; i++) {
redVal += 1;
greenVal += 1;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait);
}
for (; i &lt;200;i++) {
greenVal -= 1;
redVal -= 1;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait);
}
}
}
if (dynamic.substring(0,1) == "f") {
redVal = 255;
greenVal = 0;
blueVal = 0;
wait = 50;
for (repeat = 0; repeat &lt;dynamic.length(); repeat++) {
i = 0;
for (; i &lt;100; i++) {
redVal -= 1;
greenVal += 1;
blueVal =1;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait);
}
for (; i &lt;200;i++) {
redVal = 1;
greenVal -= 1;
blueVal += 1;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait);
}
for (; i &lt;300;i++) {
redVal += 1;
greenVal =1;
blueVal -= 1;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait);
}
redVal = 0;
greenVal = 0;
blueVal = 0;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
}
}

1st Iteration of ‘ff’:

2nd Iteration of ‘ff’:

Multi-color LED control

Description

I used an Arduino to control three LED’s: red, green, and blue. With pulse width modification, I control the brightness of each. They are all under a plastic container that has been covered with a couple layers of bubble wrap to act as a diffuser. The brightness of each can be controlled by using serial communication sending combinations of the letters “r”, “g”, and “b”. For example, “rrrrrbbbb” will result in a purple light.

Components

  • Arduino
  • 3 LED’s: red, green, blue
  • 3 Resistors (220Ω)
  • 5 Wires
  • 1 Breadboard
  • 1 Plastic storage container
  • Bubble wrap

Code

 



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 colorVal;
int redVal;
int greenVal;
int blueVal;
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() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin,   127);   // set them all to mid brightness
  analogWrite(greenPin, 127);   // set them all to mid brightness
  analogWrite(bluePin,  127);   // set them all to mid brightness
  Serial.println("enter color command using r's, b's, and g's (e.g. 'rrrrrrggggg') :");  
}
void loop () {
  // clear the string
  memset(serInString, 0, 100);
  //read the serial port and create a string out of what you read
  readSerialString(serInString);
  // initialize all back to zero for each loop
  redVal = 0;
  greenVal = 0;
  blueVal = 0;
  colorCode = serInString[0];
  if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
    // increment each color based on number of each letter
    for (int i = 0; i < strlen(serInString); i++) {
      switch(serInString[i]) {
        case 'r':
          redVal += 25;
          break;
        case 'g':
          greenVal += 25;
          break;
        case 'b':
          blueVal += 25;
          break;
      }
    }
    Serial.print("Setting color ");
    Serial.println();
    Serial.print("red to ");
    Serial.print(redVal);
    Serial.println();
    Serial.print("green to ");
    Serial.print(greenVal);
    Serial.println();
    Serial.print("blue to ");
    Serial.print(blueVal);
    Serial.println();
    serInString[0] = 0;                   // indicates we've used this string
    analogWrite(redPin, redVal);
    analogWrite(greenPin, greenVal);
    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++;
  }
}

LED color controller

Drinks in a Wine Glass

Description

3 LEDs diffused together with fluff in an upside down wine glass. The user can input in what type of drink they’d like. For example, they can have ‘wine’, a ‘smoothie’, or an ‘abc’ drink which is a mystery drink. The wine glass will fill with an appropriate color for each of the drinks. For the ‘abc’ mystery drink the glass fills with a fading progression through all of the colors.

Components

1 Arduino

3 LED

3 220 Ohm Resistor

1 Breadboard

4 wires

Code

/* 
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs 
 *
 * Command structure is "&lt;colorCode&gt;&lt;colorVal&gt;", where "colorCode" is
 * one of "r","g",or "b" and "colorVal" is a number 0 to 255.
 * E.g. "r0" turns the red LED off. 
 * "g127" turns the green LED to half brightness
 * "b64" turns the blue LED to 1/4 brightness
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt &lt;tod@todbot.com
 * http://todbot.com/
 */

char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
 // -&gt; you must state how long the array will be else it won't work properly
String colorCode;
int colorVal;

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() {
 pinMode(redPin, OUTPUT); // sets the pins as output
 pinMode(greenPin, OUTPUT); 
 pinMode(bluePin, OUTPUT);
 Serial.begin(9600);
 analogWrite(redPin, 0); // set them all to mid brightness
 analogWrite(greenPin, 0); // set them all to mid brightness
 analogWrite(bluePin, 0); // set them all to mid brightness
 Serial.println("What would you like to drink? wine, smoothie, or abc?"); 
}

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 = String(serInString);
 if( colorCode == "wine" || colorCode == "smoothie" || colorCode == "abc" ) {
 Serial.println("We will now poor your drink!");
 if(colorCode == "wine") {
 analogWrite(redPin, 255);
 analogWrite(greenPin, 10);
 analogWrite(bluePin, 10);
 } else if(colorCode == "smoothie") {
 analogWrite(redPin, 200);
 analogWrite(greenPin, 100);
 analogWrite(bluePin, 100);
 } else if(colorCode == "abc") {
 int i = 1;
 int rValue = 0;
 int gValue = 30;
 int bValue = 60;
 int change = 5;
 while (i &lt; 1000) {
 rValue = (rValue + change)%255;
 gValue = (gValue + change)%255;
 bValue = (bValue + change)%255;
 analogWrite(redPin, rValue);
 analogWrite(greenPin, gValue);
 analogWrite(bluePin, bValue);
 delay(50);
 if (i%50 == 0) {
 change = change*-1;
 }
 i++;
 } 
 }
 }
 
 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++;
 }
}
<img class="alignnone size-medium wp-image-579" src="https://blogs.ischool.berkeley.edu/i262/files/2016/09/Screen-Shot-2016-09-11-at-11.22.20-AM-300x262.png" alt="Screen Shot 2016-09-11 at 11.22.20 AM" width="300" height="262" />