Introduction: RGB LED Game Shield for Arduino

I was wondering why there aren't a lot of games made by the arduino, I mean I found a page of them, but it wasn't tht many. So I decided I should try to make one of my own!

To play this game, you have to match the random color displayed by one of the rgb leds by using 3 sliding potentiometers. Then, you press a button and you are told whether you were right or not. There are 2 levels, easy and  hard, and a reset button so you can go to a new color after you win.

That's the main basis of my project, and I hope you enjoy seeing how I made it.



Step 1: Materials

This is what you need for the project:

Materials:
Arduino
2 rgb leds
3 red leds*
3 green leds*
2 pusbutton switches
1 sliding switch (I used a right-angle DPDT)
3 10k Slidding Potentiometers
1 piece of pc board*
4 10-pin male headers
1 10k resistor
8 150 ohm resistors*
6 100 ohm resistors*
wire

Tools:
Solder
Soldering iron
Basic electronic tools(pliers, wire cutters, wire strippers)

*The stars will be explained on the next page

Step 2: The Parts

The red and green leds

For this you can either use 3 green leds and 3 red leds which are used to display if you won the game or lost it, but you can also do what I did and use 3 red/green leds.

The pc board

Make sure the pc board is large enough to cover the entire arduino, as some pieces are too small and will not fit all the parts or pins on the arduino.

Resistors

I used 100 ohms on the blue and green of the rgb leds and 150 ohm on the red but that can change depending on your leds. I used 150 ohm on my red/green leds but thaat can change for yours too. The reset button has one 100 ohm resistor and the "checking button" has a 10k and a 100 ohm resistor.

Step 3: Bend the Headers

the make the headers work for this shield, use pliers and push the smaller part down so the longer part gets longer. Now cut two of the headers into blocks of 6 and the other 2 headers into blocks of 8.

The arduino does not have all .1" spacing, so we have to accomodate for that. Take one of the 8-blocks and bend all the pinsto one way about 30 degrees. Now,grab the pins a little higher with the pliers and bend them back to normal. This should offset them just enough to fit the arduino.

Step 4: Soldering

I can not really explain how to solder this so please use the picture I made on fritzing for reference and the other pictures I have provided on where to place everything.

If you have trouble reading it, I will do a write-up of the circuit too.

Step 5: The Code

The code for this project is pretty simple. The first part is for defing everything, the second for creating a random color, and the third for using the potentiometers and giving the pushbutton and leds commands.
Here it is:

/*Code for rgb led game shield. By Ikestarm17. Special thanks to Ladyada for
all the great products, mbeckler for his informational site, marklar, grumpy_mike,
and AWOL on the Arduino forums.*/

int greenled1 = 13;
int greenled2 = 8;
int greenled3 = 4;

int redled1 = 19;
int redled2 = 7;
int redled3 = 12;     // leds that show if you were right or not

int buttonstate;
int val;
int val2;
int val3;
int switchpin = 2;
int switchpin2 = 0;   // variables for the button stuff

int redactual;
int greenactual;
int blueactual;       // variables for storing the colors you need to match

int redPin = 3;
int bluePin = 5;
int greenPin = 6;     // pins for the rgb led you control

int redIn = 2;
int greenIn = 3;
int blueIn = 4;       // pins for the potentiometers for the rgb led

int redguess;
int greenguess;
int blueguess;        // variables to store the values of your led to compare to the random ones

  /* This following part before "void setup()"
  *is all about making the rgb led
  *a "good" random color, not just a whited out one.
  *I don't know much about this part,
  *because I only changed one little part,
  *Marklar from the arduino forums created it */

#ifndef RGBCommonLED_H
#define RGBCommonLED_H

#include "WProgram.h"


class RGBCommonLED {
public:
  RGBCommonLED(int rpin, int gpin, int bpin);
  ~RGBCommonLED();

  void setRGB(byte r, byte g, byte b);
  void setHSV(byte h, byte s, byte v);
  void setColor(unsigned long v);
  void setHue(byte level);
  void setSat(byte level);
  void setVal(byte level);

  void adjHue(int level);
  void adjSat(int level);
  void adjVal(int level);

  void setRandomColor();
  void setRandomHue();
  void setRandomSat();
  void setRandomVal();

  void setHSVLevel(int section, byte level);
  void adjHSVLevel(int section, int level);
  void setRandomHSVLevel(int section);

  void updateLED();

  void HSVToRGB( unsigned int inHue, unsigned int inSaturation, unsigned int inValue,
    unsigned int *oR, unsigned int *oG, unsigned int *oB );

  void RGBToHSV( unsigned int inRed, unsigned int inGreen, unsigned int inBlue,
    unsigned int *oH, unsigned int *oS, unsigned int *oV );


  void updateHSVFromRGB();


  int pins[3];

  int rgb[3];

  int hsv[3];

private:

  boolean hsvSet;
  void setRGBFromHSV();
  void setHSVFromRGB();
};

#endif

RGBCommonLED::RGBCommonLED(int rpin, int gpin, int bpin){
  pins[0] = rpin;
  pins[1] = gpin;
  pins[2] = bpin;
  hsvSet = false;
}


RGBCommonLED::~RGBCommonLED(){
}

void RGBCommonLED::setColor(unsigned long v)
{
  this->setRGB((v & 255),((v >> 8) & 255),(v >> 16) & 255);
}


void RGBCommonLED::setRGB(byte r, byte g, byte b){
  rgb[0] = r;
  rgb[1] = g;
  rgb[2] = b;
  hsvSet = false;
}

void RGBCommonLED::setHSV(byte h, byte s, byte v){
  hsv[0] = h;
  hsv[1] = s;
  hsv[2] = v;
  this->setRGBFromHSV();
  hsvSet = true;
}


void RGBCommonLED::setHSVLevel(int section, byte level){
  updateHSVFromRGB();
  hsv[section] = level;
  this->setRGBFromHSV();
}

void RGBCommonLED::setHue(byte level){
  this->setHSVLevel(0,level);
}

void RGBCommonLED::setSat(byte level){
  this->setHSVLevel(1,level);
}

void RGBCommonLED::setVal(byte level){
  this->setHSVLevel(2,level);
}

void RGBCommonLED::adjHSVLevel(int section, int level){
  updateHSVFromRGB();
  int th = hsv[section] + level;

  if( th < 0 ){
    th = 255 + th;
  }
  else if( th > 255 ) {
    th = 255 - th;
  }

  th = constrain(th,0,255);
  hsv[section] = th;
  this->setRGBFromHSV();
}

void RGBCommonLED::adjHue(int level){
  this->adjHSVLevel(0,level);
}

void RGBCommonLED::adjSat(int level){
  this->adjHSVLevel(1,level);
}

void RGBCommonLED::adjVal(int level){
  this->adjHSVLevel(2,level);
}

void RGBCommonLED::RGBToHSV( unsigned int inRed, unsigned int inGreen, unsigned int inBlue,
unsigned int *oH, unsigned int *oS, unsigned int *oV )
{
  double vals[3];
  unsigned char maxc=0, minc=0;
  double hue, sat, val;

  vals[0]=inRed;
  vals[1]=inGreen;
  vals[2]=inBlue;
  //red is set as maximum and minimum
  if(vals[1]>vals[maxc]) maxc=1;
  if(vals[2]>vals[maxc]) maxc=2;
  if(vals[1]<vals[minc]) minc=1;
  if(vals[2]<vals[minc]) minc=2;
  val = vals[maxc];
  if(vals[maxc]==0)
    sat = hue = 0;
  else
  {
    sat=255*(1-(vals[minc]/vals[maxc]));
    hue = 60 * ((maxc*2) + (vals[(maxc+1)%3] - vals[(maxc+2)%3])/(vals[maxc] - vals[minc]));
  }
  if(hue < 0) hue += 360; //corrector for hues in -60 to 0 range
  *oH = hue; //map(hue,0,360,0,255);
  *oS = sat;
  *oV = val;
}

void RGBCommonLED::HSVToRGB( unsigned int inHue, unsigned int inSaturation, unsigned int inValue,
unsigned int *oR, unsigned int *oG, unsigned int *oB )
{
  if( inSaturation == 0 )
  {
    // achromatic (grey)
    *oR = *oG = *oB = inValue;
  }
  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 = (inValue * ( 255 - inSaturation )) >> 8;
    unsigned int q = (inValue * ( 255 - ((inSaturation * offsetInSector) >> 8) )) >> 8;
    unsigned int t = (inValue * ( 255 - ((inSaturation * ( 255 - offsetInSector )) >> 8) )) >> 8;

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

void RGBCommonLED::setRandomColor(){
  this->setColor((unsigned long)random(0x01000000));
}

void RGBCommonLED::setRandomHue(){
  this->setRandomHSVLevel(0);
}
void RGBCommonLED::setRandomSat(){
  this->setRandomHSVLevel(1);
}
void RGBCommonLED::setRandomVal(){
  this->setRandomHSVLevel(2);
}

void RGBCommonLED::setRandomHSVLevel(int section){
  this->setHSVLevel(section, (unsigned int)random(0x0100));
}


void RGBCommonLED::updateHSVFromRGB(){
  this->setHSVFromRGB();
  hsvSet = true;
}
void RGBCommonLED::updateLED(){
  analogWrite(this->pins[0], rgb[0]);
  analogWrite(this->pins[1], rgb[1]);
  analogWrite(this->pins[2], rgb[2]);

  redactual = rgb[0];
  greenactual = rgb[1];
  blueactual = rgb[2];

}

void RGBCommonLED::setRGBFromHSV(){
  unsigned int h = hsv[0];
  unsigned int s = hsv[1];
  unsigned int v = hsv[2];
  unsigned int r = 0;
  unsigned int g = 0;
  unsigned int b = 0;
  HSVToRGB(h,s,v,&r,&g,&b);
  this->setRGB(r,g,b);
}

void RGBCommonLED::setHSVFromRGB(){
  unsigned int r = rgb[0];
  unsigned int g = rgb[1];
  unsigned int b = rgb[2];
  unsigned int h;
  unsigned int s;
  unsigned int v;

  this->RGBToHSV(r,g,b,&h,&s,&v);
  hsv[0] = map(h,0,360,0,255);
  hsv[1] = s;
  hsv[2] = v;
  hsvSet = true;  
}

RGBCommonLED led1(10,11,9);

void setup()
{
  pinMode(redled1, OUTPUT);
  pinMode(redled2, OUTPUT);
  pinMode(redled3, OUTPUT);
  pinMode(greenled1, OUTPUT);
  pinMode(greenled2, OUTPUT);
  pinMode(greenled3, OUTPUT); 
  pinMode(switchpin, INPUT);  
  pinMode(switchpin2, INPUT);// setting the leds as output and the switch as input
  Serial.begin(9600);          // start serial
  randomSeed(analogRead(0));   // read analog pin 0 to get random value

  led1.setRandomHue();
  led1.setSat(255);
  led1.setVal(255);
  led1.setRandomColor();
  led1.updateLED();             // set a random color on the first rgb led

 buttonstate = digitalRead(switchpin);  // read the switch as off

}

void loop(){

   redguess = analogRead(redIn);
  greenguess = analogRead(greenIn);
  blueguess = analogRead(blueIn);               // read the potentiometers

  redguess = map(redguess, 0, 1023, 0, 255);
  greenguess = map(greenguess, 0, 1023, 0, 255);
  blueguess = map(blueguess, 0, 1023, 0, 255);    // scale down the values

  analogWrite(redPin, redguess);
  analogWrite(greenPin, greenguess);
  analogWrite(bluePin, blueguess);              // set the color values

  redguess = 255 - redguess;
  blueguess = 255 - blueguess;
  greenguess = 255 - greenguess;                  // fix the value's numbers so 0 is 255 and 255 is 0

 val = digitalRead(switchpin);
  delay(10);
  val2 = digitalRead(switchpin);
     val3 = digitalRead(switchpin2);               // read switches and fix the bouncing



     if(val3 == LOW && val == val2 && val != buttonstate && val == LOW && redactual + 55 < redguess ||
              val3 == LOW && val == val2 && val != buttonstate && val == LOW && redactual - 55> redguess ||
              val3 == LOW && val == val2 && val != buttonstate && val == LOW && blueactual + 55 < blueguess ||
              val3 == LOW && val == val2 && val != buttonstate && val == LOW && blueactual - 55 > blueguess ||
              val3 == LOW && val == val2 && val != buttonstate && val == LOW && greenactual + 55 < greenguess ||
              val3 == LOW && val == val2 && val != buttonstate && val == LOW && greenactual - 55 > greenguess ||
              val3 == HIGH && val == val2 && val != buttonstate && val == LOW && redactual + 85 < redguess ||
              val3 == HIGH && val == val2 && val != buttonstate && val == LOW && redactual - 85 > redguess ||
              val3 == HIGH && val == val2 && val != buttonstate && val == LOW && blueactual + 85 < blueguess ||
              val3 == HIGH && val == val2 && val != buttonstate && val == LOW && blueactual - 85 > blueguess ||
              val3 == HIGH && val == val2 && val != buttonstate && val == LOW && greenactual + 85 < greenguess ||
              val3 == HIGH && val == val2 && val != buttonstate && val == LOW && greenactual - 85 > greenguess){   // make sure your led's colors do not match the other one's for the easier level

              digitalWrite(greenled1, LOW),         // turn on red leds
              digitalWrite(redled1, HIGH),
              delay(500),
              digitalWrite(redled1, LOW),
              digitalWrite(redled2, HIGH),
              delay(500),
              digitalWrite(redled2, LOW),
              digitalWrite(redled3, HIGH),
              delay(500),
              digitalWrite(redled3,LOW);
              digitalWrite(redled1, HIGH),
              delay(500),
              digitalWrite(redled1, LOW),
              digitalWrite(redled2, HIGH),
              delay(500),
              digitalWrite(redled2, LOW),
              digitalWrite(redled3, HIGH),
              delay(500),
              digitalWrite(redled3,LOW),
              digitalWrite(redled1, HIGH),
              digitalWrite(redled2, HIGH),
              digitalWrite(redled3, HIGH),
              delay(250),
              digitalWrite(redled1, LOW),
              digitalWrite(redled2, LOW),
              digitalWrite(redled3, LOW),
              delay(250),
              digitalWrite(redled1, HIGH),
              digitalWrite(redled2, HIGH),
              digitalWrite(redled3, HIGH),
              delay(250),
              digitalWrite(redled1, LOW),
              digitalWrite(redled2, LOW),
              digitalWrite(redled3, LOW),
              delay(250),
              digitalWrite(redled1, HIGH),
              digitalWrite(redled2, HIGH),
              digitalWrite(redled3, HIGH),
              delay(250),
              digitalWrite(redled1, LOW),
              digitalWrite(redled2, LOW),
              digitalWrite(redled3, LOW),
              delay(250),
              digitalWrite(redled1, HIGH), 
              digitalWrite(redled2, HIGH),
              digitalWrite(redled3, HIGH),
              delay(250),
              digitalWrite(redled1, LOW),
              digitalWrite(redled2, LOW),
              digitalWrite(redled3, LOW),
              delay(250);
              }

   while(val3 == LOW && val == val2 && val != buttonstate && val == LOW &&              // make sure button is pressed
     redactual + 55 >= redguess && redactual - 55 <= redguess &&
     blueactual + 55 >= blueguess && blueactual - 55 <= blueguess &&
     greenactual + 55 >= greenguess && greenactual - 55 <= greenguess ||
     val3 == HIGH && val == val2 && val != buttonstate && val == LOW &&             
     redactual + 85 >= redguess && redactual - 85 <= redguess &&
     blueactual + 85 >= blueguess && blueactual - 85 <= blueguess &&
     greenactual + 85 >= greenguess && greenactual - 85 <= greenguess){   // make sure your led's colors are close enough to the other one's for the easy level            

      digitalWrite(redled1,LOW),          // turn on green leds                   
      digitalWrite(greenled1, HIGH),
      delay(500),
      digitalWrite(greenled1, LOW),
      digitalWrite(greenled2, HIGH),
      delay(500),
      digitalWrite(greenled2, LOW),
      digitalWrite(greenled3, HIGH),
      delay(500),
      digitalWrite(greenled3, LOW),
      digitalWrite(redled1,LOW),               // turn on green leds                   
      digitalWrite(greenled1, HIGH),
      delay(500),
      digitalWrite(greenled1, LOW),
      digitalWrite(greenled2, HIGH),
      delay(500),
      digitalWrite(greenled2, LOW),
      digitalWrite(greenled3, HIGH),
      delay(500),
      digitalWrite(greenled3, LOW),
      digitalWrite(greenled1, HIGH), 
      digitalWrite(greenled2, HIGH),
      digitalWrite(greenled3, HIGH),
      delay(250),
      digitalWrite(greenled1, LOW),
      digitalWrite(greenled2, LOW),
      digitalWrite(greenled3, LOW),
      delay(250),
      digitalWrite(greenled1, HIGH),
      digitalWrite(greenled2, HIGH),
      digitalWrite(greenled3, HIGH),
      delay(250),
      digitalWrite(greenled1, LOW),
      digitalWrite(greenled2, LOW),
      digitalWrite(greenled3, LOW),
      delay(250),
      digitalWrite(greenled1, HIGH), 
      digitalWrite(greenled2, HIGH),
      digitalWrite(greenled3, HIGH),
      delay(250),
      digitalWrite(greenled1, LOW),
      digitalWrite(greenled2, LOW),
      digitalWrite(greenled3, LOW),
      delay(250),
      digitalWrite(greenled1, HIGH),
      digitalWrite(greenled2, HIGH),
      digitalWrite(greenled3, HIGH),
      delay(250),
      digitalWrite(greenled1, LOW),
      digitalWrite(greenled2, LOW),
      digitalWrite(greenled3, LOW),
      delay(250);
     }
}

Step 6: Final Thoughts

I think this project works pretty well and is entertaining for long car rides and other things. I know my soldering on the back was not very good but my soldering iron was not working right and I am still a beginner at electronics. This was my first arduino project and I hope you enjoyed it. Thanks for reading!

Light Up the Night! Contest

Participated in the
Light Up the Night! Contest

Arduino Contest

Participated in the
Arduino Contest