Lab2_LEDDiffuser
Diffuser: Tracing paper on inside of a jar.
keyboard command codes color mapping: r=red, b=blue, g=green, y=r+g, t=b+g, p=b+r, a=r+b+g
color codes {r,g,b,y,t,p,a} fade from full power color to 0.
color codes {R,G,Y,T,P,A} fade from 0 to full power.
code:
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 = 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 i = 0; // Loop counter
int wait = 15; // (.005 second) delay (which is pretty short for a quick fade)
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 0); // set them all to off
analogWrite(greenPin, 0); // set them all to off
analogWrite(bluePin, 0); // set them all to off
Serial.println("Type in the color code you want, then do it again. But be quick the color fades away fast.(e.g. 'r') :");
}
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' || colorCode == 'y' || colorCode == 't' || colorCode == 'p' || colorCode == 'a' ) {
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal = 255;
Serial.print("Color ");
Serial.print(colorCode);
Serial.print(" is fading away!! Hurry and type in another one!");
Serial.println();
Serial.println();
serInString[0] = 0; // indicates we've used this string
}
else if( colorCode == 'R' || colorCode == 'G' || colorCode == 'B' || colorCode == 'Y' || colorCode == 'T' || colorCode == 'P' || colorCode == 'A' ){
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
Serial.print("Color ");
Serial.print(colorCode);
Serial.print(" is powern' up!! Try another one!");
Serial.println();
serInString[0] = 0; // indicates we've used this string
}
if (colorCode == 'r')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal = 255;
while (colorVal >= 0)
{
analogWrite(redPin, colorVal);
colorVal -=1;
delay(wait);
}
}
else if(colorCode == 'g')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal = 255;
while (colorVal>=0)
{
analogWrite(greenPin, colorVal);
colorVal -=1;
delay(wait);
}
}
else if(colorCode == 'b')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal = 255;
while (colorVal >= 0)
{
analogWrite(bluePin, colorVal);
colorVal -=1;
delay(wait);
}
}
if (colorCode == 'y')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal = 255;
while (colorVal >= 0)
{
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal -=1;
delay(wait);
}
}
if (colorCode == 't')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal = 255;
while (colorVal >= 0)
{
analogWrite(bluePin, colorVal);
analogWrite(greenPin, colorVal);
colorVal -=1;
delay(wait);
}
}
if (colorCode == 'p')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal = 255;
while (colorVal >= 0)
{
analogWrite(redPin, colorVal);
analogWrite(bluePin, colorVal);
colorVal -=1;
delay(wait);
}
}
if (colorCode == 'a')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal = 255;
while (colorVal >= 0)
{
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
analogWrite(bluePin, colorVal);
colorVal -=1;
delay(wait);
}
}
if (colorCode == 'A')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
while (colorVal <= 255)
{
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
analogWrite(bluePin, colorVal);
colorVal +=1;
delay(wait);
}
}
if (colorCode == 'P')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
while (colorVal <= 255)
{
analogWrite(redPin, colorVal);
analogWrite(bluePin, colorVal);
colorVal +=1;
delay(wait);
}
}
if (colorCode == 'T')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
while (colorVal <= 255)
{
analogWrite(greenPin, colorVal);
analogWrite(bluePin, colorVal);
colorVal +=1;
delay(wait);
}
}
if (colorCode == 'Y')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
while (colorVal <= 255)
{
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
colorVal +=1;
delay(wait);
}
}
if (colorCode == 'R')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
while (colorVal <= 255)
{
analogWrite(redPin, colorVal);
colorVal +=1;
delay(wait);
}
}
if (colorCode == 'G')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
while (colorVal <= 255)
{
analogWrite(greenPin, colorVal);
colorVal +=1;
delay(wait);
}
}
if (colorCode == 'B')
{
colorVal = 0;
analogWrite(bluePin, colorVal);
analogWrite(redPin, colorVal);
analogWrite(greenPin, colorVal);
while (colorVal <= 255)
{
analogWrite(bluePin, colorVal);
colorVal +=1;
delay(wait);
}
}
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++;
}
}