Mastermind Game /w Arduino Uno

431

3

1

Posted in PlayPuzzles

Introduction: Mastermind Game /w Arduino Uno

This project was for the most part a programming challenge for myself, since this was the first time I've written anything in C/C++ and the first time I've worked with Arduino.

Step 1: The Used Supplies

1x Arduino Uno

Set of Breadboard Jumper Wires

10x PCB Pushbuttons

Adafruit NeoPixel Digital RGB LED Weatherproof Strip (3x 4 LEDs)

10x 330 Ohm Resistors

1x Perf Board

Cardboard

Black Spraycan

Step 2: Assembling the Parts

The 10 buttons and the 3 LED strips are all connected to the 5V pin.

Every button is connected to 1 330 ohm resistor.
Every resistor as well as the 3 LED strips are connected to the GND.

LED strip for player 1 (the mastermind) is connected to pin 2
LED strip for player 2 (the codebreaker) is connected to pin 3
LED strip that guides player 2 towards the right code is connected to pin A0

The buttons for player 1 (the mastermind):

Button 1 is connected to pin 7
Button 2 is connected to pin 6
Button 3 is connected to pin 5
Button 4 is connected to pin 4
Button 5 is connected to pin 8

The buttons for player 2:

Button 1 is connected to pin 10
Button 2 is connected to pin 11
Button 3 is connected to pin 12
Button 4 is connected to pin 13
Button 5 is connected to pin 9

Step 3: Writing the Code

For this project I used the Fast LED library for the NeoPixel LED strip.

The code:

#include

#define NUM_LEDS 4
#define LED_PIN_R 3
#define LED_PIN_L 2

CRGB led_R[NUM_LEDS];
CRGB led_L[NUM_LEDS];
CRGB led_3[NUM_LEDS];

//buttons Codebreaker
int button1 = 10;
int button2 = 11;
int button3 = 12;
int button4 = 13;

//button Rx
int button5 = 9;

//buttons Mastermind
int button6 = 7;
int button7 = 6;
int button8 = 5;
int button9 = 4;

//button Lx
int button10 = 8;

//button vars
int buttonLED1 = 0;
int buttonLED2 = 1;
int buttonLED3 = 2;
int buttonLED4 = 3;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;
int buttonState8 = 0;
int buttonState9 = 0;
int buttonState10 = 0;

int lastButtonState1 = 0;
int lastButtonState2 = 0;
int lastButtonState3 = 0;
int lastButtonState4 = 0;
int lastButtonState5 = 0;
int lastButtonState6 = 0;
int lastButtonState7 = 0;
int lastButtonState8 = 0;
int lastButtonState9 = 0;
int lastButtonState10 = 0;

int buttonCounter1 = 0;
int buttonCounter2 = 0;
int buttonCounter3 = 0;
int buttonCounter4 = 0;
int buttonCounter5 = 0;
int buttonCounter6 = 0;
int buttonCounter7 = 0;
int buttonCounter8 = 0;
int buttonCounter9 = 0;
int buttonCounter10 = 0;

//ready variables
boolean player1_ready = false;
boolean player2_ready = false;

//combination arrays

int player1Combo[] = {0,0,0,0};
int player2Combo[] = {0,0,0,0}; int matches[] = {0,0,0,0};

// matches counter
int good_match = 0;
int bad_match = 3;

boolean check_array[] = {false, false, false, false};
boolean check_array2[] = {false, false, false, false};

//win var
boolean win = false;

// for loop vars
int i = 0;
int x = 0;
int y = 0;

void setup() {
pinMode(button1, INPUT); pinMode(button2, INPUT); pinMode(button3, INPUT); pinMode(button4, INPUT); pinMode(button5, INPUT); pinMode(button6, INPUT); pinMode(button7, INPUT); pinMode(button8, INPUT); pinMode(button9, INPUT); pinMode(button10, INPUT);

FastLED.addLeds(led_R, NUM_LEDS);

for (int i = 0; i < NUM_LEDS; i++) { led_R[i] = CRGB(0, 0, 0); }

FastLED.show();

FastLED.addLeds(led_L, NUM_LEDS);

for (int i = 0; i < NUM_LEDS; i++) { led_L[i] = CRGB(0, 0, 0); }

FastLED.show();

FastLED.addLeds(led_3, NUM_LEDS);

for (int i = 0; i < NUM_LEDS; i++) { led_3[i] = CRGB(0, 0, 0); }

FastLED.show(); }

void setColor_R(int counter, int buttonNum) {
if (counter == 0) { led_R[buttonNum] = CRGB(0, 0, 0); }
if (counter == 1) { led_R[buttonNum] = CRGB(255, 0, 0); }
if (counter == 2) { led_R[buttonNum] = CRGB(0, 255, 0); }
if (counter == 3) { led_R[buttonNum] = CRGB(0, 0, 255); }
if (counter == 4) { led_R[buttonNum] = CRGB(255, 255, 0); }
if (counter == 5) { led_R[buttonNum] = CRGB(255, 0, 255); }
if (counter == 6) { led_R[buttonNum] = CRGB(0, 255, 255); }
if (counter == 7) { led_R[buttonNum] = CRGB(255, 50, 0); }
if (counter == 8) { led_R[buttonNum] = CRGB(255, 255, 255); }
FastLED.show(); }

void setColor_L(int counter, int buttonNum) {
if (counter == 0) { led_L[buttonNum] = CRGB(0, 0, 0); }
if (counter == 1) { led_L[buttonNum] = CRGB(255, 0, 0); }
if (counter == 2) { led_L[buttonNum] = CRGB(0, 255, 0); }
if (counter == 3) { led_L[buttonNum] = CRGB(0, 0, 255); }
if (counter == 4) { led_L[buttonNum] = CRGB(255, 255, 0); }
if (counter == 5) { led_L[buttonNum] = CRGB(255, 0, 255); }
if (counter == 6) { led_L[buttonNum] = CRGB(0, 255, 255); }
if (counter == 7) { led_L[buttonNum] = CRGB(255, 50, 0); }
if (counter == 8) { led_L[buttonNum] = CRGB(255, 255, 255); }
FastLED.show(); }

void loop() {

//I tried using functions for the buttons, but I kept getting bugs I couldn't solve, so I decided to just write out the code for every button, because that did work.

//button1
buttonState1 = digitalRead(button1);
if (buttonState1 != lastButtonState1) { if (buttonState1 == HIGH) { buttonCounter1++; }

delay(50); }
lastButtonState1 = buttonState1;
player2Combo[0] = buttonCounter1;
if (buttonCounter1 == 9) buttonCounter1 = 0;

//button2
buttonState2 = digitalRead(button2);
if (buttonState2 != lastButtonState2) { if (buttonState2 == HIGH) { buttonCounter2++; } delay(50); }
lastButtonState2 = buttonState2;
player2Combo[1] = buttonCounter2;
if (buttonCounter2 == 9) buttonCounter2 = 0;

//button3
buttonState3 = digitalRead(button3);
if (buttonState3 != lastButtonState3) { if (buttonState3 == HIGH) { buttonCounter3++; } delay(50); }
lastButtonState3 = buttonState3;
player2Combo[2] = buttonCounter3;
if (buttonCounter3 == 9) buttonCounter3 = 0;

//button4
buttonState4 = digitalRead(button4);
if (buttonState4 != lastButtonState4) { if (buttonState4 == HIGH) { buttonCounter4++; } delay(50); }
lastButtonState4 = buttonState4;
player2Combo[3] = buttonCounter4;
if (buttonCounter4 == 9) buttonCounter4 = 0;

//button 6
buttonState6 = digitalRead(button6);
if (buttonState6 != lastButtonState6) { if (buttonState6 == HIGH && player1_ready == false) { buttonCounter6++; }
delay(50); }
lastButtonState6 = buttonState6;
player1Combo[0] = buttonCounter6;
if (buttonCounter6 == 9) buttonCounter6 = 0;

//button7
buttonState7 = digitalRead(button7);
if (buttonState7 != lastButtonState7) { if (buttonState7 == HIGH && player1_ready == false) { buttonCounter7++; }
delay(50); }
lastButtonState7 = buttonState7;
player1Combo[1] = buttonCounter7;
if (buttonCounter7 == 9) buttonCounter7 = 0;

//button8
buttonState8 = digitalRead(button8);
if (buttonState8 != lastButtonState8) { if (buttonState8 == HIGH && player1_ready == false) { buttonCounter8++; }
delay(50); }
lastButtonState8 = buttonState8;
player1Combo[2] = buttonCounter8;
if (buttonCounter8 == 9) buttonCounter8 = 0;

//button 9
buttonState9 = digitalRead(button9);
if (buttonState9 != lastButtonState9) { if (buttonState9 == HIGH && player1_ready == false) { buttonCounter9++; }
delay(50); }
lastButtonState9 = buttonState9;
player1Combo[3] = buttonCounter9;
if (buttonCounter9 == 9) buttonCounter9 = 0;

//button Rx (5)
buttonState5 = digitalRead(button5);
if (buttonState5 != lastButtonState5){ if (buttonState5 == HIGH){
good_match = 0; bad_match = 3;
for (int i = 0; i < NUM_LEDS; i++) {
led_3[i] = CRGB(0, 0, 0); }

for (x = 0; x < 4; x++){
if (player1Combo[x] == player2Combo[x])
{
check_array[x] = true;
good_match++; } }
for (x = 0; x < 4; x++){
if (player1Combo[x] != player2Combo[x]) {
for (y = 0; y < 4; y++){
if (player1Combo[x] == player2Combo[y] && check_array[y] != true){
check_array[y] = true;
bad_match--;
break; } } } }
for (x = 0; x < 4; x++){
check_array[x] = false; } }
delay(50);
if (player1Combo[0] == player2Combo[0] && player1Combo[1] == player2Combo[1] && player1Combo[2] == player2Combo[2] && player1Combo[3] == player2Combo[3]){
win = true; } } }
lastButtonState5 = buttonState5;

// match colors
if (good_match == 0 && bad_match == 3){
for (int i = 0; i < NUM_LEDS; i++) {
led_3[i] = CRGB(0, 0, 0); }

for (int i = 0; i < good_match; i++){
led_3[i] = CRGB(50, 255, 50); check_array2[i] = true; }

for (int i = 3; i > bad_match; i--){
led_3[i] = CRGB(255, 50, 100); check_array2[i] = true; }

//button Lx (10)
buttonState10 = digitalRead(button10);
if (buttonState10 != lastButtonState10) {
if (buttonState10 == HIGH && player1_ready == false){
player1_ready = true; }
delay(50); }
lastButtonState10 = buttonState10;

if (win == true){
buttonCounter1 = 10;
buttonCounter2 = 10;
buttonCounter3 = 10;
buttonCounter4 = 10;
buttonCounter6 = 10;
buttonCounter7 = 10;
buttonCounter8 = 10;
buttonCounter9 = 10; }

setColor_R(buttonCounter1, buttonLED1);
setColor_R(buttonCounter2, buttonLED2);
setColor_R(buttonCounter3, buttonLED3);
setColor_R(buttonCounter4, buttonLED4);
setColor_L(buttonCounter6, buttonLED1);
setColor_L(buttonCounter7, buttonLED2);
setColor_L(buttonCounter8, buttonLED3);
setColor_L(buttonCounter9, buttonLED4);
FastLED.show();

}

Step 4: Making the Case

For this project I had limited time and limited resources for the time I had.
I decided to ultimately make the casing out of cardboard.

The entire cardboard plate I used was 39,5 cm x 18 cm.

The cardboard casing has 4 sides that get folded (on either end, so 8 total).
the side where the LED strips get placed is the first side which is 3 cm x 18 cm.
the side where the buttons are placed is the second one and is 2 cm x 18 cm.
after that comes the second to last side is 2 cm x 18 cm
and the last side which is connected to the bottom is also 2 cm x 18 cm.

Then comes the matter of folding the case to create the desired shape.
The LEDs I used are 0,5 cm x 0,5 cm and are 3,2 cm apart from each other and also from the side ends.
( (3,2 + 0,5) * 4) + 3,2 = 18

I punched holes in the cardboard where the LEDs are placed so the light shines through.

On the side with the buttons I didn't need to really measure anything on the X-axis, because the buttons are placed directly beneath the LEDs. The 5th button is placed exactly between the last button and the side end.

The 3rd LED strip uses the same measurements as the others, but is placed on the top side next to the player 2 LED strip.

After all the holes are punched and the measurements are right I spray painted the cardboard black.

After the paint had dried I connected all the buttons and LED strips to the casing. When placed it was time to solder all the parts to the right wires on a perf board. After that I connected it all to the Arduino.

Finally I cut out a cardboard plate of 25,5 cm x 18 cm and sprayed it black to use as a floor plate. I connected the floor plate to the casing using double sided sticky tape.

Connect the Arduino the a powersource and VOILA!
A playable electronic Mastermind!

Share

    Recommendations

    • Spotless Contest

      Spotless Contest
    • Microcontroller Contest

      Microcontroller Contest
    • Space Challenge

      Space Challenge
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    Comments

    That looks fun, you should upload a video of it in play :)