Assignment 2: Digital I/O with Arduino Boards + Diffuser

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

DESCRIPTION

1) Design a good diffuser for your RGB LEDs (e.g. ping pong ball, Styrofoam, etc).

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

3) (Optional) Come up with other ways of controlling the colors of the LEDs using the keyboard.

COMPONENTS USED

  • 3 Light Emitting Diodes (LED)
  • 3 Resistors
  • Diffuser

DETAILS

1.  I used a shot glass covered with tissue paper to act as my diffuser.

2.  ARDUINO CODE:

#define ARR_SIZE 10

char serInString[ARR_SIZE];  // 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 bluePin  = 10;  // Blue LED,  connected to digital pin 10

int greenPin = 11;  // Green 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.");

Serial.println("Number of times entered determines percentage (i.e. rrrrr = 50% brightness) :");

}

 

void loop () {

int count = 0;

//read the serial port and create a string out of what you read

readSerialString(serInString);

 

colorCode = serInString[0];

count = 1;

 

if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {

//count number of times colorCode is entered

int i = 1;

while (serInString[i] == colorCode && i<ARR_SIZE)

{

count++;

i++;

}

 

//calculate value of color

colorVal = (count * 255) / 10;

 

Serial.print("setting color ");

Serial.print(colorCode);

Serial.print(" to ");

Serial.print(count * 10);

Serial.print("% brightness, or ");

Serial.print(colorVal);

Serial.println();

 

if(colorCode == 'r')

analogWrite(redPin, colorVal);

else if(colorCode == 'g')

analogWrite(greenPin, colorVal);

else if(colorCode == 'b')

analogWrite(bluePin, colorVal);

 

//re-initialize serInString

for (int x=0; x < ARR_SIZE; x++)

serInString[x] = 'X';

}

 

delay(200);  // 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++;

}

}

 

3.  The following code gives the user 7 different pre-programmed colors to choose from.  The user is asked to enter 3 color codes in succession, and the program cycles through those 3 colors every 3 seconds.

#define ARR_SIZE 3

char serInString[ARR_SIZE];  // 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 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

 

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

 

for(int i=0; i < ARR_SIZE; i++)

serInString[i] = ' ';

 

Serial.println("Pick three colors from the list below");

Serial.println("1. Red");

Serial.println("2. Blue");

Serial.println("3. Green");

Serial.println("4. Yellow");

Serial.println("5. Orange");

Serial.println("6. Pink");

Serial.println("7. Purple");

Serial.println("Enter number of the 3 colors you want (123 = Red, Blue, Green): ");

}

 

void loop () {

int count = 0;

//read the serial port and create a string out of what you read

readSerialString(serInString);

 

//Serial.println("You entered: " + serInString[0] + ", " + serInString[1] + ", " + serInString[2]);

 

if( serInString[0] != ' ' ) {

Serial.print("You entered: ");

Serial.print(serInString[0]);

Serial.print(serInString[1]);

Serial.println(serInString[2]);

 

for (int i = 0; i < ARR_SIZE; i++)

{

//choose color, and then adjust LEDs to the proper brightness

switch (serInString[i]) {

case '1': analogWrite(redPin, 255);

analogWrite(greenPin, 0);

analogWrite(bluePin,0);

break;

case '2': analogWrite(redPin, 0);

analogWrite(greenPin, 0);

analogWrite(bluePin,255);

break;

case '3': analogWrite(redPin, 0);

analogWrite(greenPin, 255);

analogWrite(bluePin,0);

break;

case '4': analogWrite(redPin, 255);

analogWrite(greenPin, 255);

analogWrite(bluePin,0);

break;

case '5': analogWrite(redPin, 255);

analogWrite(greenPin, 127);

analogWrite(bluePin,0);

break;

case '6': analogWrite(redPin, 255);

analogWrite(greenPin, 100);

analogWrite(bluePin, 150);

break;

case '7': analogWrite(redPin, 127);

analogWrite(greenPin, 0);

analogWrite(bluePin, 127);

break;

}

delay(3000);

}

}

}

 

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

}

}