Home‎ > ‎Unit 08‎ > ‎Analog Write‎ > ‎

Color Match Game

Your job is to improve a game I've written. Make the diffuser as shown and then try out the code below. You'll need your RGB LED and all three slide potentiometers. You may need to change the numbers in the program to match your wiring. The program will randomly generate a color and write it to the LED. The sliders control the color of the background. When you think you've got a match just press any key on the keyboard to check. If you get it wrong nothing happens. If you're correct you get a new color to try and match.

All in all it's not a very good game. Make it better, or use it as the inspiration for a new and completely different game. Your game must use both analogWrite and analogRead.

In order to get best results you should make a diffuser.  One way to make the color mixing work better is to diffuse the light, in a light box. You can make a light box out of plain paper, scissors and some tape. Just make a paper box and cut a hole in it. Fill the box with tissue paper. The tissue acts as a diffuser, helping the light mix nicely. You may find ou don't need a full sheet of kleenex.

import processing.serial.*; //Import the serial library into your sketch

import cc.arduino.*;        //Import the Arduino-Firmata library into your sketch

Arduino arduino;      //Create an instance of Arduino named arduino (can be any name) 

int redPin = 9;      //Change these pin numbers as needed to match your wiring
int greenPin = 10;  
int bluePin = 11;  

int rR=255;    // These will be for the random colors
int rG=255;
int rB=255;

int guessR;    // These are the values of the analogRead
int guessG;
int guessB;

void setup() {
  size(200, 200);    
  arduino = new Arduino(this, Arduino.list()[0], 57600); 

void draw()
  arduino.analogWrite(redPin, rR);
  arduino.analogWrite(greenPin, rG);
  arduino.analogWrite(bluePin, rB);
  guessR = arduino.analogRead(2);
  guessG = arduino.analogRead(3);
  guessB = arduino.analogRead(4);
  background(guessR, guessG, guessB);

void keyPressed() // Press any Key to Check Guess
  if (guessR > rR-5 && guessR < rR+5 && guessR > rG-5 && guessG < rG+5 && guessB > rB-5 && guessB < rB+5)

void newColor()  // Generate a new color
  rR=int(random(0, 255));
  rG=int(random(0, 255));
  rB=int(random(0, 255));