Jam Jar color mixer

Description

I used an Arduino, LEDs, and a jam jar “diffuser.” My program prompts users to enter a flavor of jam into the Arduino Serial monitor (“Welcome to the Jam Color Mixer! What type of jam would you like?”), then mixes red, green, and blue light to create a color approximating that jam type. Requesting strawberry jam gives a purpley-red color, blueberry gives a dark blue, and lime gives a bright green. The serial monitor repeats the request to the user and displays the RBG color combination (“You asked for cherry jam. cherry is composed of R255, G31, B31”).

To make the diffusor, a glass jam jar was lined with white tissue paper on the sides and a wad of cotton batting on the bottom. The jar screw cap (but not the rubberized seal) was screwed on to keep the tissue paper in place.

Components

  • Arduino UNO board
  • 1 red, 1 green, and 1 blue LED
  • connector wires
  • a “quilted” pattern, 6 oz glass jam jar
  • white tissue paper
  • cotton batting

Code

/* Jam RGB LED
* Serial commands control the type of jam (color of light)
* by controlling the brightness of R,G,B LEDs
* Command structure is “<fruitName>”, where “fruitName” is
* one of ten possible fruits. the colors of these fruits are translated into RGB colors.
* E.g. “apple” gives R163, G82, B0
* “blueberry” gives R51, G0, B102
*/
String fruitName; //a String class variable that will contain the entire serial input
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 debug = 1;
//arrays of RGB values
int redArray[10] = {163,51,46,255,245,255,112,138,184,255};
int greenArray[10] = {82,0,0,31,184,255,224,0,0,51};
int blueArray[10] = {0,102,184,31,0,31,0,184,46,102};

int colorIndex; //an index variable

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 off
analogWrite(greenPin, 0); // set them all to off
analogWrite(bluePin, 0); // set them all to off
Serial.println(“Welcome to the Jam Color Mixer! What type of jam would you like?”);
}

void loop() {
//clear the fruitName variable;
fruitName.remove(0);
//read the serial port and create a string out of it
readSerialString();

if (fruitName.length()>0) {
//confirm the request
Serial.print(“You asked for “);
Serial.print(fruitName);
Serial.println(” jam.”);

if(fruitName == “apple”)
colorIndex=0;
else if(fruitName == “blackberry”)
colorIndex=1;
else if(fruitName == “blueberry”)
colorIndex=2;
else if(fruitName == “cherry”)
colorIndex=3;
else if(fruitName == “honey”)
colorIndex=4;
else if(fruitName == “lemon”)
colorIndex=5;
else if(fruitName == “lime”)
colorIndex=6;
else if(fruitName == “plum”)
colorIndex=7;
else if(fruitName == “raspberry”)
colorIndex=8;
else if(fruitName == “strawberry”)
colorIndex=9;

analogWrite(redPin, redArray[colorIndex]);
analogWrite(greenPin, greenArray[colorIndex]);
analogWrite(bluePin, blueArray[colorIndex]);

if (debug == 1) {
Serial.print(fruitName);
Serial.print(” is composed of R”);
Serial.print(redArray[colorIndex]);
Serial.print(“, G”);
Serial.print(greenArray[colorIndex]);
Serial.print(“, B”);
Serial.println(blueArray[colorIndex]);
}

fruitName.remove(0); //removes (deletes) all of the inString so that it’s not used anymore
}

delay(100); // wait a bit, for serial data
}
//read a string from the serial and store it in a String
void readSerialString () {
//available() returns the number of bytes avaiable for reading from the serial port
if(!Serial.available()) { //if there’s no serial string received, do nothing
return;
}
else if (Serial.available()) {
fruitName=Serial.readString(); //read the entire serial input into a String variable
/*code for debugging fruitName
Serial.print(“Read in string: “);
Serial.print(fruitName);
Serial.println();*/
}
}

breadboard setup diffusor exteriorplum

Rainbow House

Description:

I used an Arduino-Uno as a microprocessor to light three LED lights dependent on input from a keyboard. There are three different modes: ROYGBIV explicit color input, RGB explicit color input, or step-dimness color input. As a diffuser, I used a house that I 3D designed and printed from an Ultimaker 2+ out of PLA and ABS material, with some cotton distributed by the windows. The LEDs are capable of entering the house via the chimney.

Components Used:

  • 1 Arduino
  • 3 LEDs (red, green, and blue)
  • 3 Resistor (220Ω)
  • 1 Breadboard
  • A keyboard
  • A 3D printed house with holes in it.
  • Cotton.

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
char colorCode;
String colorCodeString; // necessary for first conditional check for ROYGBIV
float colorVal; // necessary for step-change conditional
int colorValInt; // necessary for clean println statements
float colorDuplicateInput; // necessary for computation of LED power

struct RGB { //instantiate a datastructure for RGB values
 char colorName[50];
 int r;
 int g;
 int b;
};

struct RGB variable[9]; // declare a variable array that contains data types of data structure RGB

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() {
// ROYGBIV defined
strcpy(variable[0].colorName, "white");
variable[0].r = 255; 
variable[0].g = 255;
variable[0].b = 255;

strcpy(variable[1].colorName, "red");
variable[1].r = 255; 
variable[1].g = 0;
variable[1].b = 0;

strcpy(variable[2].colorName, "orange");
variable[2].r = 255; 
variable[2].g = 165;
variable[2].b = 0;

strcpy(variable[3].colorName, "yellow");
variable[3].r = 255; 
variable[3].g = 255;
variable[3].b = 0;

strcpy(variable[4].colorName, "green");
variable[4].r = 0; 
variable[4].g = 255;
variable[4].b = 0;

strcpy(variable[5].colorName, "blue");
variable[5].r = 0; 
variable[5].g = 0;
variable[5].b = 255;

strcpy(variable[6].colorName, "indigo");
variable[6].r = 75; 
variable[6].g = 0;
variable[6].b = 130;

strcpy(variable[7].colorName, "violet");
variable[7].r = 238; 
variable[7].g = 130;
variable[7].b = 238;

strcpy(variable[8].colorName, "black");
variable[8].r = 0; 
variable[8].g = 0;
variable[8].b = 0;
 
 pinMode(redPin, OUTPUT); // sets the pins as output
 pinMode(greenPin, OUTPUT); 
 pinMode(bluePin, OUTPUT);
 Serial.begin(9600); // instantiate communication between the computer and the microprocessor
 analogWrite(redPin, variable[0].r); //default is R value for white
 analogWrite(greenPin, variable[0].g); //default is G value for white
 analogWrite(bluePin, variable[0].b); //default is B value for white
 Serial.println("Please enter color command (e.g. r43, red, rrr) :"); 
}

void loop () {
 // clear the string
 memset(serInString, 0, 100);
 //read the serial port and create a string out of what you read
 readSerialString(serInString);
 //this is basically catching everything... 
 if (atoi(serInString + 1) &gt; 0 || atoi(serInString + 1) == 0) {
 colorCodeString = String(serInString);
 colorCode = serInString[0];
 // check if we have the word in our dictionary 
 if( colorCodeString == "red" || colorCodeString == "green" || colorCodeString == "blue" || colorCodeString == "white" || colorCodeString == "black" || colorCodeString == "yellow" || colorCodeString == "orange" || colorCodeString == "violet" || colorCodeString == "indigo") {
 for (int count = 0; count &lt;= 8; count++) {
 if(colorCodeString == variable[count].colorName) {
 analogWrite(redPin, variable[count].r);
 analogWrite(greenPin, variable[count].g);
 analogWrite(bluePin, variable[count].b);
 Serial.print("Setting RGB values for default ");
 Serial.print(colorCodeString);
 Serial.print(" hue: ");
 String colorSentence_R = "R: " + String(variable[count].r); 
 String colorSentence_G = ", G: " + String(variable[count].g);
 String colorSentence_B = ", B: " + String(variable[count].b);
 Serial.println(colorSentence_R + colorSentence_G + colorSentence_B);
 return;
 }
 }
 }
 // if not a full word in our dictionary... 
 else if (colorCode == 'r' || colorCode == 'g' || colorCode == 'b') {
 // if not a step conditional but an explicit value
 if (serInString[0] != serInString[1]) {
 colorVal = atoi(serInString+1);
 }
 // the step conditional
 else {
 colorDuplicateInput = strlen(serInString);
 colorVal = (float)(colorDuplicateInput/10 * 255.0); 
 }
 colorValInt = (int)colorVal;
 Serial.print("setting color ");
 Serial.print(colorCode);
 Serial.print(" to ");
 Serial.print(colorValInt);
 Serial.println();
 serInString[0] = 0; // indicates we've used this string
 if(colorCode == 'r') 
 analogWrite(redPin, colorValInt);
 else if(colorCode == 'g')
 analogWrite(greenPin, colorValInt);
 else if(colorCode == 'b')
 analogWrite(bluePin, colorValInt);
 }
 }
 
 
 
 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++;
 }
}


Rainbow House Clip

A new look on milk and coffee

Description:

I used the example code (_3_LED_Fading.txt and Serial_LED.txt) for this lab. The fading code faded one LED color in and out and into the next, forming the effect of new colors like purple and green.

Once that was tested and completed, I adjusted the serial LED code to accept user inputs via the Serial Monitor to adjust the brightness of each LED. Each r, g or b in the Serial Monitor now represents 10% of the possible brightness (255) of each LED. For example rrr = (3/10*255). As a diffuser, I used an old small milk bottle and stuffed it with 2 coffee filters to diffuse the light from the LEDs. The effect was pretty rad.


Components:

  • 1 Arduino
  • 1 Breadboard
  • 3 LEDs (b,r,g)
  • 3 Resistors (220 ohms)
  • 4 connecting wires
  • 1 USB cable
  • 1 Laptop
  • 1 Milk bottle
  • 2 Coffee filters

Code:

_3_LED_Fading

// 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 = 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 = 18; // 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
}
}

// Main program
void loop()
{
i += 1; // Increment counter
if (i < 255) // First phase of fades
{
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
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
}
else // Re-set the counter, and start the fades again
{
i = 1;
}

analogWrite(redPin, redVal); // 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
}

Serial_LED

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;
float colorVal;

int redPin = 11; // Red LED, connected to digital pin 9
int greenPin = 9; // Green LED, connected to digital pin 10
int bluePin = 10; // 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, 80); // set them all to mid brightness
analogWrite(greenPin, 80); // set them all to mid brightness
analogWrite(bluePin, 80); // set them all to mid 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 = strlen(serInString);
colorVal = (colorVal/10*255); // the number of times you type r, g or b defines the brightness (% of)
if (colorVal>255)
{ // need a way to turn off our colors
colorVal = 0;}
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++;
}
}

Dimming and Keyboard Inputs

Description:

I connected 3 LEDs in parallel and covered them with a diffuser made of styrofoam and bubblewrap. The initial code changes the brightness of the LEDs one at a time through a keyboard input and pulse width modulation (eg: r127). I modified the code so that a user could enter the letters ‘r’, ‘g’ or ‘b’ a number of times to indicate the percentage of brightness that they want that LED to be at. The minimum is 0% and the maximum is 100%, so the users can enter a letter 1 to 10 times to change the value (‘ggg’ corresponds to 30%). I adjusted the code so that entering a letter once turns the LED off. In addition I added the functionality of changing the intensity of multiple LEDs in one line input. The user can specify colors by entering different letters at once (eg: rrgggbbb). If the user doesn’t enter a specific color at all, then that color doesn’t change with that input.

diffuserLED

Components:

  • 1 Arduino
  • 1 Breadboard3 LEDs (blue, red, green)
  • 3 Resistors (220 ohms each)
  • 4 Connecting wires
  • 1 Diffuser (3 styrofoam peanuts + 1 bubble wrap sheet)
  • 1 USB Cable
  • 1 Laptop

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;

float colorVal;

float redVal = 0, blueVal = 0, greenVal = 0;

int redCode, greenCode, blueCode;

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. ‘rrrrgggbb’) to indicate percentage of brightness(1-10). Single letter for turning off:”);

}

void loop () {

  // clear the string

  memset(serInString, 0, 100);

  redVal = 0, blueVal = 0, greenVal = 0;

  redCode = 0, blueCode = 0, greenCode = 0;

  //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’ ) {

  for (int i = 0; i < strlen(serInString); i++) {

    if (serInString[i] == ‘r’) {

      redVal++;

      redCode = 1;

    }

    else if (serInString[i] == ‘g’) {

      greenVal++;

      greenCode = 1;

    }

    else if (serInString[i] == ‘b’) {

      blueVal++;

      blueCode = 1;

    }

  }

  Serial.print(redVal);

  Serial.print(” “);

  Serial.print(greenVal);

  Serial.print(” “);

  Serial.println(blueVal);

  redVal = (redVal/10)*255;

    if (redVal < 26) {

      redVal = 0;

  }

  greenVal = (greenVal/10)*255;

    if (greenVal < 26) {

      greenVal = 0;

  }

  blueVal = (blueVal/10)*255;

    if (blueVal < 26) {

      blueVal = 0;

  }

  Serial.print(“setting colors to “);

  Serial.print(redVal);

  Serial.print(” “);

  Serial.print(greenVal);

  Serial.print(” “);

  Serial.print(blueVal);

  Serial.println();

  serInString[0] = 0;

  if (redCode > 0)

    analogWrite(redPin, redVal);

  if (greenCode > 0)

    analogWrite(greenPin, greenVal);

  if (blueCode > 0)

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

  }

}