3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

RGB LED game shield for arduino

Step 5The code

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);
     }
}
 

« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
4
Followers
6
Author:ikestarm17