Whimsical light!


The brightness of the R, G, B LEDs is controlled via serial commands. Once we have compiled and uploaded the sketch in our Arduino, we open the Serial Monitor (tools  → serial monitor). When the Arduino is ready we will read the message “WELCOME to the WHIMSICAL light!” in the serial monitor window, and then we will be prompted to enter the color command.

The structure of the color command is “<colorCode><colorCode>…<colorCode>”, where “colorCode” is one of “r”,”g”,or “b”. The intensity of each light is determined by the number of times the color code is typed (i.e., once: LED off, twice: low, three times: medium low, four times: medium, five times: medium high, and six times: high).


“r”    turns the red LED off.
“gg”   turns the green LED to low brightness
“bbbb” turns the blue LED to medium brightness
If an unrecognized character is received, you will get a surprise!


  • 1 Arduino Uno
  • 3 resistor (220 ohms)
  • 1 red LED, 1 blue LED, 1 green LED
  • 1 USB cable
  • Solid core wire
  • Bubble wrap
  • White plastic piece (see photo)
  • Laptop


char serInString[100];  // array that will hold the different bytes of the string. 100=100characters; [char data type that takes up 1 byte of memory that stores a character value]

// -> you must state how long the array will be else it won’t work properly

char colorCode;         // “colorCode” is one of “r”,”g”,or “b”
int colorVal;           // “colorVal” is a number 0 to 255
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 flag = 1;
int cont = 0;


void setup() {            //********* Why don’t we declare all the variables here of before?
pinMode(redPin,   OUTPUT);   // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin,  OUTPUT);
Serial.begin(9600);           // Sets the data rate in bits per second (baud) for serial data transmission.
analogWrite(redPin,   127);   // set them all to mid brightness
analogWrite(greenPin, 127);   // set them all to mid brightness
analogWrite(bluePin,  127);   // set them all to mid brightness

Serial.println(“WELCOME to the WHIMSICAL ligth!”);

void loop () {
if (flag == 1){
Serial.println(“Please enter the 6 digit color command.”);
Serial.println(“(e.g. ‘r’ for turning off the red ligh, rr for a low red light, ggg for a medium green light, or ‘bbbb’ for a high blue light):”);
flag = 0;
memset(serInString, 0, 100);   //set all bytes in the buffer to 0. Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
readSerialString(serInString);  //read the serial port and create a string out of what you read
colorCode = serInString[0];

if( colorCode == ‘r’ || colorCode == ‘g’ || colorCode == ‘b’ ) {
Serial.print(“colorCode: “);
for (int x=0; x<7; x++){
if(serInString[x] == colorCode){
Serial.print(“Setting color “);
if (cont == 1){
Serial.println(” to 0″);
else if(cont == 2){
Serial.println(” to LOW”);
else if(cont == 3){
Serial.println(” to MEDIUM LOW”);
else if(cont == 4){
Serial.println(” to MEDIUM”);
else if(cont == 5){
Serial.println(” to MEDIUM HIGH”);
else if(cont == 6){
Serial.println(” to HIGH”);
serInString[0] = 0;                   // indicates we’ve used this string. Reseting the value to 0 (we have already backed up its value in colorCode and colorVal.? WHY DO YOU NEED TO INDICATE THIS? AT THE BEGINNING OF THE LOOP WE ARE SETTING ALL THE ARRAY TO 0
if(colorCode == ‘r’)
analogWrite(redPin, colorVal);
else if(colorCode == ‘g’)
analogWrite(greenPin, colorVal);
else if(colorCode == ‘b’)
analogWrite(bluePin, colorVal);
flag = 1;
if (colorCode != ‘r’ && colorCode != ‘b’ && colorCode != ‘g’ && colorCode != 0){
int aleatorio=random(15);
for(int y=0;y<255;y=y+random(10,30)){
analogWrite(redPin, y);
for(int y=0;y<256;y=y+random(4,25)){
analogWrite(bluePin, y);
for(int y=0;y<256;y=y+random(12,30)){
analogWrite(greenPin, y);
analogWrite(redPin, 0);
analogWrite(bluePin, 0);
analogWrite(greenPin, 0);
serInString[0] = 0;
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;
//available(): Get the number of bytes (characters) available for reading from the serial port.
//Syntax: Serial.available()
//Returns: the number of bytes available to read
if(!Serial.available()) {  //gets into the if when there’s no data available through the serial port. If there’s 0 bytes to be read, !Serial.available()=1 (the negation), and enters to the if and returns to the main loop.
//Serial.print(” Serial available value: “);
while (Serial.available()>0) { // while the value being returned by serial.available is different from 0 (meaning there are bytes to be read), reads the info coming through the port and stores it in the specified array
strArray[i] = Serial.read(); //Serial.read(): Returns the first byte of incoming serial data available (or -1 if no data is available). The first byte received is stored in posiiton [0]

Leave a Reply