Digital I/O with Arduino Boards + Diffuser

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

 

Description


The goal of this assignment was to create a slightly more elaborate circuit and explore Arduino's pulse width modulation capabilities while getting comfortable with serial communication and expanding our Arduino programming skill.

 

 

Components Used


The following components were used:
  • Arduino
  • Breadboard
  • Rubber band (2)
  • USB cable
  • Jumper Cables (3 small, 4 medium)
  • 220 Ohm resistor (3)
  • LED light (3: red, green, and blue)

 

Arduino Code


The following code was modified from the provided serial_led_rgb to provide an alternate means of input control to Arduino (e.g., "rrgbbgr" could be used)

/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs

*
* Command structure is "", 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
* http://todbot.com/
*
* Modified for i262 HW 2 on 16 September 2009
* Eric C. Mai
* http://www.ericmai.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 bluePin = 9; // Red LED, connected to digital pin 9
int redPin = 10; // Green LED, connected to digital pin 10
int greenPin = 11; // Blue LED, connected to digital pin 11

int blueValue = 127;
int redValue = 127;
int greenValue = 127;

void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redValue); // set them all to mid brightness
analogWrite(greenPin, greenValue); // set them all to mid brightness
analogWrite(bluePin, blueValue); // 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);
Serial.print("You typed ");
Serial.print(serInString);
Serial.print(". ");

processSerialCommands(serInString, 100);
}
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++;
}
}

void processSerialCommands(char *strArray, int maxLength) {
int i = 0;

while (i < maxLength && strArray[i] != '\0') {
//Read in the character at position i in the string
colorCode = serInString[i];

//If the character is r (red)...
if (colorCode == 'r') {
//Increase the current red value by 10, and if you reach 255 go back to 0
redValue = (redValue + 10) % 255;
analogWrite(redPin, redValue);
Serial.print("setting color r to ");
Serial.println(redValue);

//If the character is g (green)...
} else if (colorCode == 'g') {
greenValue = (greenValue + 10) % 255;
analogWrite(greenPin, greenValue);
Serial.print("setting color g to ");
Serial.println(greenValue);

//If the character is b (blue)...
} else if (colorCode == 'b') {
blueValue = (blueValue + 10) % 255;
analogWrite(bluePin, blueValue);
Serial.print("setting color b to ");
Serial.println(blueValue);
}

i++;
}
}

Pictures