Lab 2

Part 1:  LIGHT DIFFUSER

I made an origami flower out of waxed paper. I was curious how the wax coating on the paper would affect the light quality. The origami flower is folded in a way that the center is a hollow sphere, with the petals attached to this sphere. I like how the light reaches beyond the sphere and is reflected on the petals.

Part 2: MANUAL LED OPERATION

I wanted to set the LED lights so they could be each be individually turned on by increments of 50 with each letter of their color typed in. At the beginning of a load-in, the lights are all off, but when the first letter of a color (i.e. “r” for red) is typed in the serial monitor, that color is lit by increments of 50. So the first “r” will bring the light to 50, second “r” brings the light to 100, third to 150 and so on. When the brightness reaches maximum (250), the next “r” brings it back to 0, or off.

This control is repeated for the other two lights: “g” for green and “b” for blue.

materials:
—1 Arduino

—3 LEDs

—3 220 ohm resistors

—1 breadboard

—7 wires

 

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 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 redVal = 0; // brightness value for red LED, must be between 0 and 255
int greenVal = 0; // brightness value for green LED, must be between 0 and 255
int blueVal = 0; // brightness value for blue LED, must be between 0 and 255

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(“enter color command (e.g. ‘r43’) :”);
}

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

serInString[0] = 0; // indicates we’ve used this string
if(colorCode == ‘r’){
redVal = redVal + 50;
if (redVal > 255){
redVal = 0;
}
Serial.print(“setting color “);
Serial.print(colorCode);
Serial.print(” to “);
Serial.print(redVal);
Serial.println();

analogWrite(redPin, redVal);
}
else if(colorCode == ‘g’){
greenVal = greenVal + 50;
if (greenVal > 255){
greenVal = 0;
}
Serial.print(“setting color “);
Serial.print(colorCode);
Serial.print(” to “);
Serial.print(greenVal);
Serial.println();

analogWrite(greenPin, greenVal);
}
else if(colorCode == ‘b’){
blueVal = blueVal + 50;
if (blueVal > 255){
blueVal = 0;
}
Serial.print(“setting color “);
Serial.print(colorCode);
Serial.print(” to “);
Serial.print(blueVal);
Serial.println();
}
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++;
}

 

 

 

Leave a Reply