Arduino Mastermind Logik Schematics Code WS2812

Mastermind - YT Video:


The game is designed for two players who play the game


Player 1 (guess numbers and position)


  • The role of the player is to guess the numbers of the other player and their order
  • The player uses a 5x pushbutton to enter the system, while 4 of the buttons function as a number incrementer at that position in the 0-9 range.
  • The fifth button sends the data for review. The addition is attempted, the combination is evaluated.
  • Data is recorded and visualized on the LCD display.
  • If the player guesses the number and its position, the green diode on that position lights up.
  • Otherwise, if the number is not entered, or when the number is guessed, but the position is not judged, the red diode lights up.
  • For a player, the game ends when 10 attempts are exceeded or when the combination is guessed.

  • Player 2 (determines combination of colors, position)


  • In the role of microcontroller Arduino Uno, or other, or AtMega chip.
  • Generates the number 0000-9999 that the player will guess
  • Sent data is verified by user by simple conditional logic
  • If both color and position are guessed, the corresponding diode turns green.
  • In the event of color failure or position, the respective LED lights up red.
  • The Arduino also verifies the number of moves the player has used, examines whether the game is over.
  • The game ends when player 1 guesses all numbers and their positions (all 4 diodes will be green), the entry will be blocked, or 10 attempts will fail.
  • The user is always informed about the game status on the display.

  • Hardware for the project

  • Arduino Uno R3
  • 4x RGB LED WS2812(one module)
  • 5x button - switching
  • LCD 20x4 or 16x2
  • I2C converter for LCD
  • Engagement



    LCD display with I2C converter Arduino Uno
    Vcc 5V
    GND GND
    SDA A4 (SDA)
    SCK/SCL A5 (SCK/SCL)
    Pushbutton Arduino Uno
    IN 1 D2
    IN 2 D3
    IN 3 D4
    IN 4 D5
    Enter D6
    RGB led diodes Arduino Uno
    DIN D13
    GND GND
    5V -
    The 5V pin is connected to an external power supply due to a large current draw, especially when all diodes are on, in the order of 200mA.

    Mastermind - playing field - DEBUG OFF

    Mastermind - DEBUG OFF - Logik

    Mastermind - playing field - DEBUG ON

    Mastermind - DEBUG ON - Logik

    Autodesk Eagle export (Wiring)

    Mastermind - schéma zapojenia - Arduino

    Mastermind - Arduino - Source

    Wokwi Mastermind simulation: HERE
    /*|----------------------------------------------------------------------------|*/
    /*|SKETCH PRE MASTERMIND IMPLEMENTACIU                                         |*/
    /*|VYHOTOVIL: martinius96@gmail.com                                            |*/
    /*|Doska: Arduino Uno / ATmega328P                                             |*/
    /*|MAXIMALNY POCET POKUSOV: 10                                                 |*/
    /*|POCET VSTUPOV: 5X PUSHBUTTON - SPINACIE TLACIDLA                            |*/
    /*|VYGENEROVANE 4-CISLIE: KAZDE CISLO JE ROZNE V ROZSAHU 0-9 BEZ OPAKOVANIA!   |*/
    /*|PRE ZOBRAZENIE VYGENEROVANEHO CISLA POUZITE DEBUG (ODKOMENTOVAT)            |*/
    /*|----------------------------------------------------------------------------|*/
    /*|VYSVETLIVKY PRE ZNAČKY NA DISPLEJI:                                         |*/
    /*| C --> uhadnute cislo na inej pozicii                                       |*/
    /*| = --> uhadnute cislo aj s poziciou (SPRAVNE CISLO!)                        |*/
    /*| - --> neuhadnute cislo, ani pozicia                                        |*/
    /*|----------------------------------------------------------------------------|*/
    /*|Podporte projekt pre viac variant: https://paypal.me/chlebovec              |*/
    /*|----------------------------------------------------------------------------|*/
    
    #define DEBUG //odkomentuj pre DEBUG INFO (ZOBRAZENIE HLADANEHO CISLA)
    //#define NULUJ //vynuluj cisla po zadani ENTEROM
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    LiquidCrystal_I2C lcd(0x27, 20, 4);
    
    const int buttonPin1 = 6;
    const int buttonPin2 = 5;
    const int buttonPin3 = 4;
    const int buttonPin4 = 3;
    const int buttonPin5 = 2;
    
    int cislo1 = 0;
    int cislo2 = 0;
    int cislo3 = 0;
    int cislo4 = 0;
    
    
    int cielovecislo1 = 0;
    int cielovecislo2 = 0;
    int cielovecislo3 = 0;
    int cielovecislo4 = 0;
    
    int buttonState1 = HIGH;
    int buttonState2 = HIGH;
    int buttonState3 = HIGH;
    int buttonState4 = HIGH;
    int buttonState5 = HIGH;
    
    int lastButtonState1 = HIGH;
    int lastButtonState2 = HIGH;
    int lastButtonState3 = HIGH;
    int lastButtonState4 = HIGH;
    int lastButtonState5 = HIGH;
    
    unsigned long lastDebounceTime1 = 0;
    unsigned long lastDebounceTime2 = 0;
    unsigned long lastDebounceTime3 = 0;
    unsigned long lastDebounceTime4 = 0;
    unsigned long lastDebounceTime5 = 0;
    unsigned long debounceInterval = 50;
    int pokus = 0;
    
    void setup() {
      lcd.begin(); //incializacia displeja na I2C zbernici
      lcd.backlight(); //zapnutie podsvietenia
      pinMode(buttonPin1, INPUT_PULLUP);
      pinMode(buttonPin2, INPUT_PULLUP);
      pinMode(buttonPin3, INPUT_PULLUP);
      pinMode(buttonPin4, INPUT_PULLUP);
      pinMode(buttonPin5, INPUT_PULLUP);
      randomSeed(analogRead(0));
      lcd.setCursor(0, 0); //nastav kurzor pre zapis na 1. riadok, 1. stlpec
      lcd.print("ZADANE CISLO:   0000");
      generuj_cisla();
    #ifdef DEBUG
      lcd.setCursor(0, 2);
      lcd.print("DEBUG ON:");
      lcd.setCursor(0, 3);
      lcd.print("HLADANE CISLO:  ");
      lcd.print(cielovecislo1);
      lcd.print(cielovecislo2);
      lcd.print(cielovecislo3);
      lcd.print(cielovecislo4);
    #endif
    }
    
    void loop() {
      if (pokus >= 10) {
        lcd.setCursor(0, 2);
        lcd.print("PREHRALI STE!");
        lcd.setCursor(0, 3);
        lcd.print("KONIEC HRY          ");
        while (1);
      } else {
        int reading1 = digitalRead(buttonPin1);
        int reading2 = digitalRead(buttonPin2);
        int reading3 = digitalRead(buttonPin3);
        int reading4 = digitalRead(buttonPin4);
        int reading5 = digitalRead(buttonPin5);
    
        if (reading1 != lastButtonState1) {
          lastDebounceTime1 = millis();
        }
    
        if (reading2 != lastButtonState2) {
          lastDebounceTime2 = millis();
        }
    
        if (reading3 != lastButtonState3) {
          lastDebounceTime3 = millis();
        }
    
        if (reading4 != lastButtonState4) {
          lastDebounceTime4 = millis();
        }
    
        if (reading5 != lastButtonState5) {
          lastDebounceTime5 = millis();
        }
    
        if ((millis() - lastDebounceTime1) > debounceInterval) {
          if (reading1 != buttonState1) {
            buttonState1 = reading1;
            if (buttonState1 == HIGH) {
              cislo1++;
              if (cislo1 > 9) {
                cislo1 = 0;
              }
            }
          }
          updateLCD();
        }
    
        if ((millis() - lastDebounceTime2) > debounceInterval) {
          if (reading2 != buttonState2) {
            buttonState2 = reading2;
            if (buttonState2 == HIGH) {
              cislo2++;
              if (cislo2 > 9) {
                cislo2 = 0;
              }
            }
          }
          updateLCD();
        }
    
        if ((millis() - lastDebounceTime3) > debounceInterval) {
          if (reading3 != buttonState3) {
            buttonState3 = reading3;
            if (buttonState3 == HIGH) {
              cislo3++;
              if (cislo3 > 9) {
                cislo3 = 0;
              }
            }
          }
          updateLCD();
        }
    
        if ((millis() - lastDebounceTime4) > debounceInterval) {
          if (reading4 != buttonState4) {
            buttonState4 = reading4;
            if (buttonState4 == HIGH) {
              cislo4++;
              if (cislo4 > 9) {
                cislo4 = 0;
              }
            }
          }
          updateLCD();
        }
    
        if ((millis() - lastDebounceTime5) > debounceInterval) {
          if (reading5 != buttonState5) {
            lcd.setCursor(16, 1);
            lcd.print(cislo1);
            lcd.print(cislo2);
            lcd.print(cislo3);
            lcd.print(cislo4);
            buttonState5 = reading5;
            if (buttonState5 == HIGH) {
              pokus++;
              updateLCD();
              over_cisla();
              #ifdef NULUJ
              cislo1 = 0;
              cislo2 = 0;
              cislo3 = 0;
              cislo4 = 0;
              #endif
            }
          }
        }
        lastButtonState1 = reading1;
        lastButtonState2 = reading2;
        lastButtonState3 = reading3;
        lastButtonState4 = reading4;
        lastButtonState5 = reading5;
      }
    }
    
    void updateLCD() {
      lcd.setCursor(16, 0);
      lcd.print(cislo1);
      lcd.print(cislo2);
      lcd.print(cislo3);
      lcd.print(cislo4);
      lcd.setCursor(0, 1);
      lcd.print("POKUS: ");
      lcd.print(pokus);
    }
    
    void generuj_cisla() {
      cielovecislo1 = random(0, 9);
      cielovecislo2 = random(0, 9);
      while (cielovecislo2 == cielovecislo1) {
        cielovecislo2 = random(0, 9);
      }
      cielovecislo3 = random(0, 9);
      while ((cielovecislo3 == cielovecislo1) || (cielovecislo3 == cielovecislo2)) {
        cielovecislo3 = random(0, 9);
      }
      cielovecislo4 = random(0, 9);
      while ((cielovecislo4 == cielovecislo1) || (cielovecislo4 == cielovecislo2) || (cielovecislo4 == cielovecislo3)) {
        cielovecislo4 = random(0, 9);
      }
    }
    
    void over_cisla() {
      if (cislo1 == cielovecislo1) {
        lcd.setCursor(10, 1);
        lcd.print("="); //UHADNUTE CISLO VRATANE POZICIE
      } else if ((cislo1 == cielovecislo2) || (cislo1 == cielovecislo3) || (cislo1 == cielovecislo4)) {
        lcd.setCursor(10, 1);
        lcd.print("C"); //UHADNUTE CISLO BEZ POZICIE
      } else {
        lcd.setCursor(10, 1);
        lcd.print("-"); //NEUHADNUTE CISLO, ANI POZICIA
      }
    
      if (cislo2 == cielovecislo2) {
        lcd.setCursor(11, 1);
        lcd.print("="); //UHADNUTE CISLO VRATANE POZICIE
      } else if ((cislo2 == cielovecislo1) || (cislo2 == cielovecislo3) || (cislo2 == cielovecislo4)) {
        lcd.setCursor(11, 1);
        lcd.print("C"); //UHADNUTE CISLO BEZ POZICIE
      } else {
        lcd.setCursor(11, 1);
        lcd.print("-"); //NEUHADNUTE CISLO, ANI POZICIA
      }
    
      if (cislo3 == cielovecislo3) {
        lcd.setCursor(12, 1);
        lcd.print("="); //UHADNUTE CISLO VRATANE POZICIE
      } else if ((cislo3 == cielovecislo1) || (cislo3 == cielovecislo2) || (cislo3 == cielovecislo4)) {
        lcd.setCursor(12, 1);
        lcd.print("C"); //UHADNUTE CISLO BEZ POZICIE
      } else {
        lcd.setCursor(12, 1);
        lcd.print("-"); //NEUHADNUTE CISLO, ANI POZICIA
      }
    
      if (cislo4 == cielovecislo4) {
        lcd.setCursor(13, 1);
        lcd.print("="); //UHADNUTE CISLO VRATANE POZICIE
      } else if ((cislo4 == cielovecislo1) || (cislo4 == cielovecislo2) || (cislo4 == cielovecislo3)) {
        lcd.setCursor(13, 1);
        lcd.print("C"); //UHADNUTE CISLO BEZ POZICIE
      } else {
        lcd.setCursor(13, 1);
        lcd.print("-"); //NEUHADNUTE CISLO, ANI POZICIA
      }
    
      if ((cislo1 == cielovecislo1) && (cislo2 == cielovecislo2) && (cislo3 == cielovecislo3) && (cislo4 == cielovecislo4)) {
        lcd.setCursor(0, 2);
        lcd.print("VYHRALI STE!");
        lcd.setCursor(0, 3);
        lcd.print("KONIEC HRY          ");
        while (1);
      }
    }