Skip to content

SALES INQUIRIES: 1 (888) 767-9864

Arduino RGB Guessing Game

What is a cat’s favorite color? Purr-ple!

While that pun may not be amazing, Tony’s Verduga’s Arduino RGB guessing game is! We love to see Arduino used in fun ways, and Tony (a member of Programming Electronics Academy) shows us how to have fun with his game made with Arduino.

When we saw this Arduino RGB game, it blue our minds.  Keep reading to see this game in action!

Can you tell us about your project?

I made an RGB guessing game! The object of the game is to guess the RGB value of the color that is generated by the game.

What inspired you to make this game?

I made the game as part of a learning experience, more than anything else really.

Ironically enough, I told someone that Arduino isn’t a toy, and yet I built a game using Arduino!

When I first came up with the idea for the game, I was learning how to use the serial monitor. I wanted to add an external display at the time, but I couldn’t figure it out.

So, after I learned more and improved my skills, I took another swing at it and made this game!

How does a user play your game?

The object of the game is to guess the RGB value for the color that the Arduino generates.

The user can press the start button to begin and will be shown a random color on the first LED. On the screen, the user will have the option to enter what they think that the red, green, and blue value of the color is.

For each red, green, and blue value, the user enters their guess between 0 and 255.

Once you guess your values for red, green, and blue, you can see what color your RGB value created and how close it is to the color that the Arduino generated!

Can you tell us more about how the game works?

In the game, the Arduino generates a random color.

On the board, there are two LEDs. The first LED shows the color that the Arduino randomly generates. There’s a potentiometer that the user can use to dial in a value for each color.

The button press code takes you through the game, and at the end of the game, the second RGB LED light will show the user what color is created using the RGB values they entered.

On the display, the user can view how well they did and look at their accuracy.

I was even so inspired by my game that I made it a permanent project using a MEGA board!

Arduino RGB Circuit

What components did your project take?

I used an OLED display, a button, a resistor, RGB LED, and potentiometer

What kind of Arduino board/clone did you use?

I used an Arduino Mega clone/ATMega328p

How is the game powered?

It can be powered through multiple methods, using wall power, USB to computer connection, and also through batteries.

What were some of the things you struggled with while working on it?

The biggest challenge I faced was dealing with shoddy breadboard connections. Sometimes there would be issues with the connection between the analog potentiometer and the Arduino.

Did the project turn out as you expected?

It turned out better than I expected! I was so happy with the stability of the reading from the analog potentiometer once I was complete.

The finished unit performs even better than I had hoped.

Looking back, is there anything you learned about programming/electronics throughout the process?

The button press counter code was very helpful. And although I may not be using it in the most sophisticated way, I also learned a lot about OLED displays. I have had a lot of free time to see a lot of free online tutorials on Arduino.

My subscription to PEA has been the single most important investment along the way. Editor’s Note: Thanks Tony! So pleased it has been helpful!

About Tony:

Tony has spent 25+ years in business technology, working for copier dealerships and specializing in IT and Solutions Software.

Most of his work was done in and around downtown Boston, where he found some of the worst traffic situations he’d ever seen.

After taking a year off from work, Tony moved out to rural central Massachusetts.

There, he now works part-time at a local musical instrument store, managing online sales of guitar effects pedals, and teaching guitar at night.

Oh, did we forget to mention that Tony is a lifelong musician!?

While Tony just started programming in February 2018, starting with Arduino, he has been working with electronics for a couple of decades and has plenty of troubleshooting experience.

Thanks for sharing your game with us Tony!

Check out the Arduino code for Tony’s RGB Game:

#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const byte knob = A0;
const byte button = 12;

int red1 = 11;
int green1 = 10;
int blue1 = 9;

int red2 = 6;
int green2 = 5;
int blue2 = 3;

int red1val;
int green1val;
int blue1val;

int red2val;
int green2val;
int blue2val;

long randomRed;
long randomGreen;
long randomBlue;

int buttonPushCounter;
int buttonState = LOW;
int lastButtonState;

int rawReading;
int redReading;
int greenReading;
int blueReading;

void setup() {
  Serial.begin(9600);

  pinMode(red1, OUTPUT);
  pinMode(green1, OUTPUT);
  pinMode(blue1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(green2, OUTPUT);
  pinMode(blue2, OUTPUT);

  digitalWrite(red1, LOW);
  digitalWrite(green1, LOW);
  digitalWrite(blue1, LOW);
  digitalWrite(red2, LOW);
  digitalWrite(green2, LOW);
  digitalWrite(blue2, LOW);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("The RGB Guessing Game");
  display.setCursor(0, 8);
  display.println(" By Tony Verduga");
  display.setTextSize(2);
  display.setCursor(0, 16);
  display.println("Press the");
  display.setCursor(0, 31);
  display.println("button to");
  display.setCursor(0, 48);
  display.println("begin.");
  display.display();

  randomSeed(analogRead(A1));
  randomRed = random(0, 255);
  analogWrite(red1, randomRed);
  randomGreen = random(0, 255);
  analogWrite(green1, randomGreen);
  randomBlue = random(0, 255);
  analogWrite(blue1, randomBlue);
}

void loop() {

  int buttonState = digitalRead(button);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      delay(250);
      buttonPushCounter++;
      Serial.println(buttonPushCounter);
    }
    lastButtonState = buttonState;
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);

  if (buttonPushCounter == 1) {
    rawReading = analogRead(knob);
    display.setCursor(0, 0);
    display.println("Select RED value,");
    display.setCursor(0, 8);
    display.println("press the button.");
    display.setCursor(0, 16);
    display.setTextSize(2);
    display.println("RED");
    display.setCursor(80, 16);
    display.println(rawReading / 4);
    display.setCursor(0, 32);
    display.println("GREEN");
    display.setCursor(0, 48);
    display.println("BLUE");
    display.display();
    redReading = analogRead(knob);
  } else if (buttonPushCounter == 2) {
    display.setCursor(0, 0);
    display.println("Select GREEN value,");
    display.setCursor(0, 8);
    display.println("press the button.");
    display.setCursor(80, 32);
    display.setTextSize(2);
    display.println(analogRead(knob) / 4);
    display.setCursor(0, 16);
    display.println("RED");
    display.setCursor(80, 16);
    display.println(redReading / 4);
    display.setCursor(0, 32);
    display.println("GREEN");
    display.setCursor(0, 48);
    display.println("BLUE");
    display.display();
    greenReading = analogRead(knob);
  } else if (buttonPushCounter == 3) {
    display.setTextSize(1);
    display.setCursor(0, 0);
    display.println("Select BLUE value,");
    display.setCursor(0, 8);
    display.println("press the button.");
    display.setCursor(80, 48);
    display.setTextSize(2);
    display.println(analogRead(knob) / 4);
    display.setCursor(0, 16);
    display.println("RED");
    display.setCursor(80, 16);
    display.println(redReading / 4);
    display.setCursor(0, 32);
    display.println("GREEN");
    display.setCursor(0, 48);
    display.println("BLUE");
    display.setCursor(80, 32);
    display.println(greenReading / 4);
    display.display();
    blueReading = analogRead(knob);
  } else if (buttonPushCounter == 4) {
    display.setCursor(0, 0);
    display.println("Press the button to compare your results.");
    display.setTextSize(2);
    display.setCursor(0, 16);
    display.println("RED");
    display.setCursor(80, 16);
    display.println(redReading / 4);
    display.setCursor(0, 32);
    display.println("GREEN");
    display.setCursor(80, 32);
    display.println(greenReading / 4);
    display.setCursor(0, 48);
    display.println("BLUE");
    display.setCursor(80, 48);
    display.println(blueReading / 4);
    display.display();
  } else if (buttonPushCounter == 5) {
    analogWrite(red2, redReading / 4); //Shows the colors you picked on the second LED while the first LED is sill lit
    analogWrite(green2, greenReading / 4);
    analogWrite(blue2, blueReading / 4);

    display.setTextSize(1);
    display.setCursor(0, 16);
    display.println("Your RED");
    display.setCursor(80, 16);
    display.println(redReading / 4);
    display.setCursor(0, 24);
    display.println("Actual RED");
    display.setCursor(80, 24);
    display.println(randomRed);
    display.setCursor(0, 32);
    display.println("Your GREEN");
    display.setCursor(80, 32);
    display.println(greenReading / 4);
    display.setCursor(0, 40);
    display.println("Actual GREEN");
    display.setCursor(80, 40);
    display.println(randomGreen);
    display.setCursor(0, 48);
    display.println("Your BLUE");
    display.setCursor(80, 48);
    display.println(blueReading / 4);
    display.setCursor(0, 56);
    display.println("Actual BLUE");
    display.setCursor(80, 56);
    display.println(randomBlue);

    int redDiff = abs((redReading / 4) - randomRed);
    int greenDiff = abs((greenReading / 4) - randomGreen);
    int blueDiff = abs((blueReading / 4) - randomBlue);
    int totalDiff = redDiff + greenDiff + blueDiff;
    display.setTextSize(2);
    int diffPercentage = map(totalDiff, 765, 0, 0, 100);
    display.setCursor(0, 0);
    display.println(diffPercentage);
    display.setCursor(30, 0);
    display.println("%");
    display.display();
  } else if (buttonPushCounter == 6) {
    digitalWrite(red1, LOW); //turns both LEDs off
    digitalWrite(green1, LOW);
    digitalWrite(blue1, LOW);
    digitalWrite(red2, LOW);
    digitalWrite(green2, LOW);
    digitalWrite(blue2, LOW);
    randomSeed(analogRead(0));
    randomRed = random(0, 255);
    analogWrite(red1, randomRed);
    randomGreen = random(0, 255);
    analogWrite(green1, randomGreen);
    randomBlue = random(0, 255);
    analogWrite(blue1, randomBlue);
    buttonPushCounter = 1;
  }
}

5 Comments

  1. Avatar Tony Verduga on July 19, 2019 at 11:12 am

    Thank you PEA! I am honored to be part of all of this.

    • Avatar Michael James on July 19, 2019 at 11:43 am

      Thanks for submitting a project Tony – I love the video and I enjoyed reading your your code!

  2. Avatar Paul Boston on July 21, 2019 at 4:35 pm

    Fantastic code organization! It reads without comments.
    I purchased the parts and assembled the gadget with only one small error. The Fritzing diagram has the clock and the data wires reversed. Once that was done it worked like a charm!

    Thanks for such a great project!

    • Avatar Tony Verduga on July 22, 2019 at 10:24 am

      Hey Paul,
      Thanks for the kind response, and the heads-up on the wiring diagram. I’m also from the Boston area! Reply back if you want to get in touch…or look me up on FB.

    • Avatar Tony Verduga on August 9, 2019 at 10:37 pm

      I corrected and submitted the wiring diagram. PEA was kind enough to update this article.

Leave a Comment