Lab 2 - hexadecimal LED diffuser

Submitted by syh on Tue, 02/12/2013 - 09:57

Lab 2 - LED Diffuser

This LED diffuser takes a 6-digit hexadecimal input and translates it as an RGB value. The diffuser itself is made of vellum paper, aluminum foil, and polyester batting. Because of its various sides, the crushed foil creates neat patterns on the paper tower. The batting pushes the foil against the paper, preventing it from falling.

The program is based off of serial RGB code provided in lab, except instead of reading a letter and two numbers, it reads an entire string (e.g., c0ffee) and parses the array as {r,r,g,g,b,b}. Difficulties I encountered were 1. subverting the existing code so that it wouldn’t constantly be trying to set the LED values (initially there was no if statement, so it was always setting it to zero; this is partially why it tests if the first character is the non-hex value ‘g’), and 2. getting the code to interpret properly as a hex value.

Issues encountered:
I was hoping for some hex equivalent of atoi(), but there is no atoh() or htoi(). Initially I used strtol(long value, 0, 16), where 16 is the base, but although it seemed to work, I was getting extremely high values (>50 000). Ultimately I decided to write my own function, hexToInt(), which took a single character input and returned 0-15 as appropriate.

Additionally, my green and blue LEDs are much brighter than my red LED, so it is basically impossible to get orange and red tones (the red values have to be very high), while very easy to get blues and purples. “White” has a lot of blue value in it, also.

1 Arduino Uno
1 USB cable
1 breadboard
3 LEDs (red, green, blue)
3 220-ohm resistors

6”x6” piece of aluminum foil
polyester batting
vellum paper folded into a 4-sided tower

* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
* RGB Hex codes are parsed and the values are set based on the input.
* Based on:
* copyleft 2006 Tod E. Kurt <

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   = 10;   // Red LED,   connected to digital pin 10
int greenPin = 11;  // Green LED, connected to digital pin 11
int bluePin  = 9;  // Blue LED,  connected to digital pin 9

void setup() {
 pinMode(redPin,   OUTPUT);   // sets the pins as output
 pinMode(greenPin, OUTPUT);   
 pinMode(bluePin,  OUTPUT);
 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("enter a 6-digit lower case hex code (e.g. 'cdcdcd') :");  

void loop () {
 // clear the string
 memset(serInString, 0, 100);
 serInString[0] = 'g'; // set the first cell to non-hex value 'g'
 //read the serial port and create a string out of what you read
 //if the array has changed, the first character should not be 'g'
 //OK so it is kind of a kludge but it totally works, and non-values should be interpreted as 0
 if(serInString[0] != 'g' ){
     //calculate the dec value from hex
     //for each, min is 00 and max is ff (255) so we should be good to call my hacky functio
     long valred = hexToInt(serInString[0])*16 + hexToInt(serInString[1]);
     long valgreen = hexToInt(serInString[2])*16 + hexToInt(serInString[3]);
     long valblue = hexToInt(serInString[4])*16 + hexToInt(serInString[5]);
     //setting LED values
     Serial.println("Setting rgb to " + String(valred) +","+ String(valgreen) +","+ String(valblue));
     analogWrite(bluePin, valblue);
     analogWrite(greenPin, valgreen);
     analogWrite(redPin, valred);
     serInString[0] = 'g';                   // indicates we've used this string
 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()) {
 while (Serial.available()) {
   strArray[i] =;

// quick and dirty function to turn hex letters into the corresponding integer
int hexToInt (char c){
 if (c <= '9' && c >= '0')
   return c;
 else if (c == 'a')
   return 10;
 else if (c == 'b')
   return 11;
 else if (c == 'c')
   return 12;
 else if (c == 'd')
   return 13;
 else if (c == 'e')
   return 14;
 else if (c == 'f')
   return 15;

 //if it's not 0-9 or a-f, just return 0
 return 0;

Your rating: None
Drupal theme by Kiwi Themes.