Tree diffuser turning 3 different range of purple
Description
In this assignment I created a diffuser as a tree made by paper, cotton wool and stickys.
The 3 (R, G, B) LEDs atlernate between 3 different color shades - here 3 different shades of purple. This is defined in the loop by 'p' for purple. When 'p' is typed in the Serial Monitor the 3 different shades of purple will change continually.
*I had problems by defining an array that could change continually by only one touch of the keyboard button ’p’. Elliot helped me with the coding part.
Components
3 x LED Lights (R, G, B)
3 x resister
USB cable
Breadboard
Arduino
1 x elastic
4 x leads
Paper
Cotton wool
Stickys
Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorVal>", 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 <tod@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 = 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 redV[] = {0, 0, 0}; // defining redV array
int greenV[] = {0, 0, 0}; // defining greenV array
int blueV[] = {0, 0, 0}; // defining blueV array
int i = 0; //sets i to null
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 mid brightness
analogWrite(greenPin, 0); // set them all to mid brightness
analogWrite(bluePin, 0); // set them all to mid brightness
Serial.println("enter color command (e.g. 'r43') :");
}
void loop () {
// memset clear the string / 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 == 'p' || colorCode == 'y') { //which colorcodes that are excepted
/* colorVal = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; */ // indicates we've used this string
if(colorCode == 'r') //sets the value of red
{
/* analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite (bluePin, 0);
*/
redV[0] = 10;
redV[1] = 155;
redV[2] = 225;
greenV[0] = 0;
greenV[1] = 0;
greenV[2] = 0;
blueV[0] = 0;
blueV[1] = 0;
blueV[2] = 0;
}
else if (colorCode == 'g') //sets the value of green
{
/* analogWrite(greenPin, 255);
analogWrite(redPin, 0);
analogWrite (bluePin, 0); */
redV[0] = 0;
redV[1] = 0;
redV[2] = 0;
greenV[0] = 10;
greenV[1] = 155;
greenV[2] = 225;
blueV[0] = 0;
blueV[1] = 0;
blueV[2] = 0;
}
else if (colorCode == 'b') //sets the value of blue
{
/*analogWrite(bluePin, 255);
analogWrite(greenPin, 0);
analogWrite (redPin, 0);*/
redV[0] = 0;
redV[1] = 0;
redV[2] = 0;
greenV[0] = 0;
greenV[1] = 0;
greenV[2] = 0;
blueV[0] = 10;
blueV[1] = 155;
blueV[2] = 255;
}
else if (colorCode == 'p') //sets the value of 3 different shades of purple
{
redV[0] = 150;
redV[1] = 255;
redV[2] = 255; //light purple
greenV[0] = 0;
greenV[1] = 10;
greenV[2] = 0; //light purple
blueV[0] = 102;
blueV[1] = 200;
blueV[2] = 255; // light purple
}
}//if statement, INSIDE loop so don't have to continue with pressing the 'key'
analogWrite(redPin, redV[i]);
analogWrite(bluePin, blueV[i]);
analogWrite(greenPin, greenV[i]);
Serial.println(redV[i] + greenV[i] + blueV[i]);
if (i<2) { //runs the array from 0-2
i++;
}
else { // when array reaches = 2, start at 0 (again)
i = 0;
}
delay(700); // set the waiting time, 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 x LED Lights (R, G, B)
-
3 x resister
-
USB cable
-
Breadboard
-
Arduino
-
1 x elastic
-
4 x leads
-
Paper
-
Cotton wool
-
Stickys
Code
- Login to post comments