Assignment 2 - LED diffuser

Submitted by jooddang on Sun, 02/10/2013 - 23:59

Eunkwang Joo

 

Description

 
I have implemented two applications and each has its unique diffuser.
 
First, iPhone diffuser is surrounding LED in four direction. People can see LED light through iPhones. If I had one more iPhone, I would have it on the top of LED. I would like to say that smartphone-generations see everything in the world through their smartphones.
In the code, I followed the direction 2 of the assignment. 
   - 2. Change the code so that you can control the RGB values with multiple key presses. For example,pressing ‘r’ 5 times will set the brightness to 50% (or brightness = 127) and pressing ‘r’ 10 times will set it to 100% (or brightness = 255)
It reads 'r', or 'g', or 'b' characters only from inputs. If 'r' was hit 11 times, it will set red LED to 10%. If 'g' was hit 13 times, green LED will be set to 30%.
 
 
 
The second diffuser is a simple origami boat. Since the paper is semitransparent, the light is blurred. The direction 3 of the assignment is as below.
   - 3. (Optional) Come up with other ways of controlling the colors of the LEDs using the keyboard
I imagined that the LED was the color of the sea. At sunrise and sunset the sea reflects red color for the sunlight. During the day it is blue color. According to user's input, the LED color will change.
 
 
 
 

Components Used

 
- Arduino Uno board
- 220ohm resistor (x3)
- red LED
- green LED
- blue LED
 
diffuser:
- iPhone (x4)
- semitransparent paper
- styrofoam
 
 
 

Code

iPhone diffuser
 
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
 
int redCount = 0;
int greenCount = 0;
int blueCount = 0;
  
char serialString[100];
 
 
void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin, 127);
  analogWrite(greenPin, 127);
  analogWrite(bluePin, 127);
  Serial.println("enter color command (e.g.: 'rrrggbbb') I MUST NOT exceed 100 characters. :");
}
 
void loop() {
  
  readSerialString(serialString);
  
  char colorCharacter;
  
  if (serialString[0] != 0) {
    redCount = 0;
    greenCount = 0;
    blueCount = 0;
  }
  
  for (int i = 0; i < 100; i++) {
    colorCharacter = serialString[i];
    
    if (colorCharacter == 'r') {
      redCount++;
    }
    else if (colorCharacter == 'g') {
      greenCount++;
    }
    else if (colorCharacter == 'b') {
      blueCount++;
    }
    else if (colorCharacter == 0) {
      break;
    }
  }
  
  redCount = redCount % 11;
  greenCount = greenCount % 11;
  blueCount = blueCount % 11;
  
  Serial.print("red: ");
  Serial.print(redCount * 10);
  Serial.print("%, green: ");
  Serial.print(greenCount * 10);
  Serial.print("%, blue: ");
  Serial.print(blueCount * 10);
  Serial.println("%");
  Serial.println("enter color command (e.g.: 'rrrggbbb') I MUST NOT exceed 100 characters. :");
  
  analogWrite(redPin, redCount * 0.1 * 255);
  analogWrite(greenPin, greenCount * 0.1 * 255);
  analogWrite(bluePin, blueCount * 0.1 * 255);
  
  for (int i = 0; i < 100; i++) {
    serialString[i] = 0;
  }
  
  delay(1000);
 
}
 
void readSerialString (char *strArray) {
  int i = 0;
  if(!Serial.available()) {
    return;
  }
  while (Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}
 
 
 
Sailing Boat (Optional)
 
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
 
char serialString[100];
 
void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin, 1);
  analogWrite(greenPin, 1);
  analogWrite(bluePin, 1);
  Serial.println("What time do you want to sail?(e.g. 10:00, 13:00, 18:00, ...) :");
}
 
int compareWords(char* w1, char *w2) {
 
  if (strcmp(w1, w2) == 0) {
    return 1;
  }
  
  return 0;
}
 
void turnOnLED(int redCount, int greenCount, int blueCount) {
  
  analogWrite(redPin, redCount * 0.1 * 255);
  analogWrite(greenPin, greenCount * 0.1 * 255);
  analogWrite(bluePin, blueCount * 0.1 * 255);
  
}
 
void loop() {
  
  readSerialString(serialString);
  
  if (serialString[0] != 0) {
    Serial.println(serialString);
  }
 
  for (int i = 0; i < 100; i++) {
    
    if (serialString[i] == ':') {// || serialString[i] == 0) {
 
      char buf[10];
      memset (buf, 0, sizeof(buf));
 
      for (int k = 0; k < i; k++) {
        buf[k] = serialString[k];
      }
 
      int time = atoi(buf);
      Serial.println(time);
      
      if (time < 7)
        turnOnLED(0,0,1);
      if (time == 7)
        turnOnLED(9,0,4);
      if (time == 8)
        turnOnLED(6,0,2);
      if (time == 9)
        turnOnLED(5,0,4);
      if (time == 10)
        turnOnLED(1,2,8);
      if (time == 11)
        turnOnLED(0,3,9);
      if (time == 12)
        turnOnLED(0,2,10);
      if (time > 12 && time < 17)
        turnOnLED(0,3,10);
      if (time == 17)
        turnOnLED(4,0,6);
      if (time == 18)
        turnOnLED(7,0,4);
      if (time == 19)
        turnOnLED(7,0,3);
      if (time == 20)
        turnOnLED(9,0,5);
      if (time == 21)
        turnOnLED(9,0,6);
      if (time > 21)
        turnOnLED(0,0,1);
    }
  }
  
  Serial.println("What time do you want to sail?(e.g. 10:00, 13:00, 18:00, ...) :");
  
  for (int i = 0; i < 100; i++) {
    serialString[i] = 0;
  }
  
  delay(2000);
}
 
void readSerialString (char *strArray) {
  int i = 0;
  if(!Serial.available()) {
    return;
  }
  while (Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}
 
0
Your rating: None
Drupal theme by Kiwi Themes.