EGGuino
Description
In this assignment I attempted to write a script that would fade each pin down to 0 after the user set the pin to a particular value, and 400 cycles have passed. The LEDs fade down to 0 using the PWM pins and can be activated independently of each other. I'm currently working out a way to make the serial entry a little more 'user friendly' by associating a mood (happy, sad, angry) with a color and to ensure pins also fade independently.
I ultimately employed an egg shell as a diffuser but researched other naturally translucent material like crab claws.
* Elliot acted as an invaluable resource for this project by answering my many questions about the code's architecture.
Components Used
- 3 LED Lights
- Breadboard
- 3 lead wired coming from Arduino board
- 3 220 Ω resisters
- USB Cable
- Packing Peanut
- Egg shell
- Arduino UNO
- 4 grounding wires (3 short 1 long)
- rubber band
- Salmonella (?)
Code 1
/*
* 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
* http://todbot.com/
* Further adapted for the EGGuino Feb 12, 2013
* copy left 2013 Deb Linton <deb@ischool.berkeley.edu>
*/
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;
char fade;
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
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 170); // set red to a higher brightness - hard to see
analogWrite(greenPin, 127); // set them to mid brightness
analogWrite(bluePin, 127); // set them to mid brightness
Serial.println("How happy or sad is your Mood Egg?");
}
void loop () {
//Serial.println("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("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
fade = colorCode;
} //close BIG if colorcode
//FADE
if(fade == 'r') {
analogWrite(redPin, colorVal);
delay(400);
}
else if(fade == 'g'){
analogWrite(greenPin, colorVal);
delay(400);
}
else if(fade == 'b'){
analogWrite(bluePin, colorVal);
delay(400);
}
// fade each pin seperatly and only if it was turned on
if (colorVal <= 0) {
colorVal = 0;
}
else {
if(fade == 'r') {
colorVal -= 5;
}
else if(fade == 'g'){
colorVal -= 5;
}
else if(fade == 'b'){
colorVal -= 5;
}
}
Serial.println(colorVal);
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; // esc out of the function if nothing is typed
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
Code 2
*
* 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
* http://todbot.com/
* Further adapted for the EGGuino Feb 12, 2013
* copy left 2013 Deb Linton <deb@ischool.berkeley.edu>
*/
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 fadeHappy;
int fadeSad;
int fadeAngry;
char pinEmote;
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
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 170); // set red to a higher brightness - hard to see
analogWrite(greenPin, 127); // set them to mid brightness
analogWrite(bluePin, 127); // set them to mid brightness
Serial.println("How happy or sad or angry is your moody EGGuino? Format: <emotion>amount 20-255");
}
void loop () {
//Serial.println("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[5];
if( colorCode == 'angry' || colorCode == 'happy' || colorCode == 'sad' ) {
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
pinEmote = colorCode;
} //close BIG if colorcode
//Set pins to an emotionally equivalent color waiting 400 cycles
if(pinEmote == 'angry') {
fadeAngry = colorVal; // this should reset the global variable of fadeAngry to colorVal
analogWrite(redPin, fadeAngry);
delay(400);
}
else if(pinEmote == 'happy'){
fadeHappy = colorVal; // this should reset the global variable of fadeAngry to colorVal
// upon second thought, giving each pin a unique value may have to happen earlier in the code
analogWrite(greenPin, fadeHappy);
delay(400);
}
else if(pinEmote == 'sad'){
fadeSad = colorVal; // this should reset the global variable of fadeAngry to colorVal
analogWrite(bluePin, fadeSad);
delay(400);
}
// if it color value is zero, or has been reduced to zero, just let it be dark unless prompted otherwise - emotions are ephemeral
if (colorVal <= 0) {
colorVal = 0;
}
if (fadeHappy <= 0) {
fadeHappy = 0;
}
if (fadeSad <= 0) {
fadeSad = 0;
}
if (fadeAngry <= 0) {
fadeAngry = 0;
}
// fade each pin separately ONLY if it was turned on
// this section SHOULD allow the pins to fade independently but it's not working yet
else {
if(fadeHappy > 0) {
fadeHappy -= 5;
}
else if(fadeSad > 0){
fadeSad -= 5;
}
else if(fadeAngry > 0){
fadeAngry -= 5;
}
}
//Serial.println(colorVal);
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; // esc out of the function if nothing is typed
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
Image
- Login to post comments