Digital I/O with Arduino Boards + Fancy Diffuser

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

Description

In this lab, I used Serial Communications with the Arduino to read keyboard input to control the brightness of each of the three LEDs individually. Additionally, I created a diffuser for the LEDs.

Components Used

 

  • Red LED
  • Blue LED
  • Green LED
  • Resistors
  • Tissue paper
  • Glass
  • Paperclip

Arduino Code

 

/* 
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs 
 *
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 */


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   = 11;   // Red LED,   connected to digital pin 11
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 9;  // Blue LED,  connected to digital pin 9

int red = 127;
int blue = 127;
int green = 127;

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. 'r43') :");  
}

void loop () {
  //read the serial port and create a string out of what you read
  readSerialString(serInString);
  
  int i = 0;
  int redVal = 0;
  int blueVal = 0;
  int greenVal = 0;
  
  while(serInString[i] != '\0' && serInString[i] != 0) {
    colorCode = serInString[i];
    if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
      if(colorCode == 'r') {
        redVal = redVal + 1;
        red = (redVal * 25) % 255;
        analogWrite(redPin, red);
        Serial.println("incrementing color red");
      }
      else if(colorCode == 'g') {
        greenVal = greenVal + 1;
        green = (greenVal * 25) % 255;
        analogWrite(greenPin, green);
        Serial.println("incrementing color green");
      }
      else if(colorCode == 'b') {
        blueVal = blueVal + 1;
        blue = (blueVal * 25) % 255;
        analogWrite(redPin, blue);
        Serial.println("incrementing color blue");
      }
    }
    serInString[i] = 0;    // indicates we've used this string
    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() && i < sizeof(strArray)) {
    strArray[i] = Serial.read();
    i++;
  }
}
}

 

Diffusers

I created a diffuser using tissue paper, a small glass, and a paperclip.