The Spectrumizer !

Brief :

Welcome to the Spectrumizer. In this assignment i used three LEDs to generate 360 colors in the color spectrum wheel. To go towards the red side of the spectrum one can press ‘r’ through the serial input and press ‘b’ to go towards the blue side of the spectrum. I now use this setup to create mood lighting in my room !

Components:

  • Arduino Uno
  • Breadboard
  • 3 LEDs (rgb)
  • 3 220Ω Resistors
  • Jumper Wires
  • USB cable

Code :

/*
Spectrumizer

This program uses arrow keys to change colors on a lamp.
The colors are within the color spectrum

I have used the following code to map HSV values to RGB
The r and b change values from 0 to 360 and vice versa to show 360 different colors

*/

// The three primary colour LEDs, driven as analgue outputs (actually PWM, but
// close enough for our analogue eyes).

int ledPinRed = 9; // Red LED connected to analogue out pin
int ledPinGrn = 10; // Green LED connected to analogue out pin
int ledPinBlu = 11; // Blue LED connected to analogue out pin
// Constants to define the ranges.

const int hueRedLow = 0;
const int hueRedHigh = 255;
const int hueBlue = 170;

// The size of the angle of one sector (1/6 of a colour wheel), and of a complete
// cycle of the colour wheel.

const int angleMin = 0;
const int angleSector = 60;
const int angleMax = 360;
//range of brighness from 0 to 255

const int brightMin = 0;
const int brightMax = 255;

// The hue is the range 0 (red) to 255 (blue)
// The brightness ranges from 0 (dark) to 255 (full brightness)

int hue, brightness;

// The saturation is fixed at 255 (full) to remove blead-through of different
// colours.
// It could be linked separately if a demonstration of hue is desired.

const int saturation = 255;

// The brightess of each LED (0 to 255).
//defining the start of the color wheel
int color = 0;

unsigned int r, g, b;

void setup() {
// Setting a baud rate for communication via USB.
Serial.begin(9600);
Serial.println("Welcome to the Spectrumizer");
Serial.println("Use the 'r' key to go towards red in the color wheel");
Serial.println("Use the 'b' key to go towards blue in the color wheel");

// Set LED pins to output.
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinGrn, OUTPUT);
pinMode(ledPinBlu, OUTPUT);
}

void loop() {

int key = 0;
// The brightness is fixed at full for the colour wheel.
brightness = 255;
//using serial.read function to read keystrokes from the keyboard
if (Serial.available() > 0) {
// read the incoming byte:
key = Serial.read();
}

if(key == 114)
{
if(color != 0)
{
for( int i = 0 ;i <10;i++)
{
color = color - 1;
// Colour wheel (red to red, wrapped around in a cycle).
hue = map(color, angleMin, angleMax, hueRedLow, hueRedHigh);
// Do the conversion.
HSBToRGB(hue, saturation, brightness, &r, &g, &b);

analogWrite(ledPinRed, r);
analogWrite(ledPinGrn, g);
analogWrite(ledPinBlu, b);
delay(50);
}
}
}
else if(key == 98)
{
if(color != 360)
{
for( int i = 0 ;i <10;i++)
{
color = color + 1;
// Colour wheel (red to red, wrapped around in a cycle).
hue = map(color, angleMin, angleMax, hueRedLow, hueRedHigh);
// Do the conversion.
HSBToRGB(hue, saturation, brightness, &r, &g, &b);

analogWrite(ledPinRed, r);
analogWrite(ledPinGrn, g);
analogWrite(ledPinBlu, b);
delay(50);
}
}
}
}

// This function taken from here:
// http://eduardofv.com/read_post/179-Arduino-RGB-LED-HSV-Color-Wheel-

void HSBToRGB(
unsigned int inHue, unsigned int inSaturation, unsigned int inBrightness,
unsigned int *oR, unsigned int *oG, unsigned int *oB )
{
if (inSaturation == 0)
{
// achromatic (grey)
*oR = *oG = *oB = inBrightness;
}
else
{
unsigned int scaledHue = (inHue * 6);
unsigned int sector = scaledHue >> 8; // sector 0 to 5 around the color wheel
unsigned int offsetInSector = scaledHue - (sector << 8); // position within the sector
unsigned int p = (inBrightness * ( 255 - inSaturation )) >> 8;
unsigned int q = (inBrightness * ( 255 - ((inSaturation * offsetInSector) >> 8) )) >> 8;
unsigned int t = (inBrightness * ( 255 - ((inSaturation * ( 255 - offsetInSector )) >> 8) )) >> 8;

switch( sector ) {
case 0:
*oR = inBrightness;
*oG = t;
*oB = p;
break;
case 1:
*oR = q;
*oG = inBrightness;
*oB = p;
break;
case 2:
*oR = p;
*oG = inBrightness;
*oB = t;
break;
case 3:
*oR = p;
*oG = q;
*oB = inBrightness;
break;
case 4:
*oR = t;
*oG = p;
*oB = inBrightness;
break;
default: // case 5:
*oR = inBrightness;
*oG = p;
*oB = q;
break;
}
}
}

Check out the video here

Leave a Reply