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

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

 

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

Description:
This assignment had two parts: designing a diffuser and controling RGB values with

multiple key presses (ulitizing serial communication).

Diffuser
I made my diffuser from the bottom of a opaque plastic cup.
I tried a bunch of items before resigning to this combination... hollowed candles, etc...but this seemed to work the best.

Also, although the purple does not seem as purple as the purple cap next to it in the photo, it was close.

Code to demonstrate multiple key presses
My code allows one to imput what color (r,g,b) on wants to manipulate and how one wants to change it (increase or decrease).

Pressing "+r" "+g" or "+b" increases the respective color (red, green or blue) by 5%.
Pressing "-r" "-g" or "-b" decreases the respective color (red, green or blue) by 5%.

Components Used:
A blue, green and red LED
3 orange wire
4 black ground wires
3 resistors


Code

/*
* A2 TUI 2009
* Based off of "Serial RGB LED"
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Pressing "+r" "+g" or "+b" increases the respective color (red, green or blue) by 5%.
* Pressing "-r" "-g" or "-b" decreases the respective color (red, green or blue) by 5%.
* Type potofgold for a color suprise!
*
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Adapted 12 September 2009
* by Hazel Onsrud
*/

#include <stdio.h>
#include <string.h>

char serInString[100];  // array that will hold the different bytes of the string. 100=100characters;

char colorCode;
char signCode;
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 rValue = 130;
int gValue = 130;
int bValue = 130;

void setup() {
pinMode(redPin,   OUTPUT);   // sets the pins as output
pinMode(greenPin, OUTPUT);  
pinMode(bluePin,  OUTPUT);
Serial.begin(9600);
analogWrite(redPin,   rValue);   // set them all to mid level brightness
analogWrite(greenPin, gValue);   // set them all to mid level brightness
analogWrite(bluePin,  bValue);   // set them all to mid level brightness
Serial.println("This program allows you to manipulate the colors of the light."); 
Serial.println("Enter +r, +b or +g to increase the respective color or -r, -b or -g to decrease it.");

Serial.println("Or type in "potofgold" for something more exicting!");

}

void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString); 
//if (Serial.available()) {
int i = 0;
int j = 1;
int k = 2;
while ((k < 100) ||(serInString[k] != '\0' )) {
signCode = serInString[i];
colorCode = serInString[j];
i = i + 2;
j = j + 2;
k = k + 2;
if (signCode == '+'){
if( colorCode == 'r'){
if ( rValue < 255 ) {
rValue= rValue + 15;
analogWrite(redPin, rValue);
Serial.println("More red.");
}
else {
Serial.println("You can't increase red any more. Try something else.");
}
}
if( colorCode == 'g'){
if ( gValue < 255 ) {
gValue= gValue + 15;
analogWrite(greenPin, gValue);
Serial.println("You added green.");
}
else
Serial.println("You can't increase green any more. Try something else.");
}
if( colorCode == 'b'){
if ( bValue < 255 ) {
bValue= bValue + 15;
analogWrite(bluePin, bValue);
Serial.println("You have increased blue.");
}
else
Serial.println("You can't increase blue any more. Try something else.");
}
}

if (signCode == '-'){
if( colorCode == 'r'){
if ( rValue > 0 ) {
rValue= rValue - 15;
analogWrite(redPin, rValue);
Serial.println("Got rid of some red");
}
else
Serial.println("You can't decrease red any more. Try something else.");
}
if( colorCode == 'g'){
if ( gValue > 0 ) {
gValue= gValue - 15;
analogWrite(greenPin, gValue);
Serial.println("Byebye green.");
}
else
Serial.println("You can't decrease green any more. Try something else.");
}
if( colorCode == 'b'){
if ( bValue > 0 ) {
bValue= bValue - 15;
analogWrite(bluePin, bValue);
Serial.println("Ciao blue.");
}
else
Serial.println("You can't decrease blue any more. Try something else.");
}
}

if (strcmp(serInString, "potofgold")  == 0) { // test to see if the two strings are equal
//red, yellow, green, cyan, blue, violet
//red
analogWrite(redPin,   255);   // set them all to mid level brightness
analogWrite(greenPin, 0);   // set them all to mid level brightness
analogWrite(bluePin,  0);   // set them all to mid level brightness
delay(300);
//yellow
analogWrite(redPin,   130);   // set them all to mid level brightness
analogWrite(greenPin, 130);   // set them all to mid level brightness
analogWrite(bluePin,  0);   // set them all to mid level brightness
delay(300);
//green
analogWrite(redPin,   0);   // set them all to mid level brightness
analogWrite(greenPin, 255);   // set them all to mid level brightness
analogWrite(bluePin,  0);   // set them all to mid level brightness
delay(300);
//cyan
analogWrite(redPin,   0);   // set them all to mid level brightness
analogWrite(greenPin, 130);   // set them all to mid level brightness
analogWrite(bluePin,  130);   // set them all to mid level brightness
delay(300);
//blue
analogWrite(redPin,   0);   // set them all to mid level brightness
analogWrite(greenPin, 0);   // set them all to mid level brightness
analogWrite(bluePin,  255);   // set them all to mid level brightness
delay(300);
//violet
analogWrite(redPin,   130);   // set them all to mid level brightness
analogWrite(greenPin, 0);   // set them all to mid level brightness
analogWrite(bluePin,  130);   // set them all to mid level brightness
delay(300);

}

}
delay(100);  // wait a bit, for serial data

//reset the string
for (int i = 0; i < 100; i++) {
serInString[i] = '\0';
}

}

//read a string from the serial and store it in an array
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}

}