Project tutorial
Simon Mini Game

Simon Mini Game © GPL3+

Small self contained Arduino Pro Mini Simon game

  • 12 views
  • 0 comments
  • 0 respects

Components and supplies

Necessary tools and machines

3drag
3D Printer (generic)
FTDI Programmer

Apps and online services

About this project

This is a mini version of my Simon Memory Game. It incorporates a 1210 SMD white LED in each button cap there-by reducing the real estate required when the switch and LED are separate.

Video

Demo of game

3D Printing

Print the top and bottom using a 0.2mm layer height and a 20% infil. The bezel should have a layer height of 0.1mm. Drill out the PCB mounting holes with a 2.5mm drill and create a thread with a 3mm tap.

Adding the LEDs to the buttons

I used cheap 12x12 buttons and inserted a 1210 white LED under the button cap.

Solder thin wires to the LED. Try and ensure that the lead is mounted under the LED and not on the side of the LED otherwise it makes it hard to insert. Also bring the leads out in opposite corners:

Once you have soldered it, test the LED by using a resistor connected to a battery or similar. Cut the positive lead shorter that the other lead (this is the anode). I found that if the leads are soldered on the bottom of the LED as shown above, you can insert the wires and position the LED on top of the switch. The button cap should just go straight on. Pull the wires taught and solder them to the PCB.

PCB board

The PCB was made using the Toner method. I have included the Eagle files so you can get them manufactured if you wish.

The Arduino Pro Mini and MAX7219 IC are mounted on the back of the board. If your board doesn't have through hole plating, use the machined headers to create a IC socket for the MAX7219 IC

The switches with LEDs, Buzzer and 2 Digit Common Cathode 0.56" 7-Segment display are mounted on the front of the board.

Use 6mm M3 screws to hold the board in place. I suggest you insert the bezel in the front panel, place the 7 segment display in the hole, mount the board and solder the display once it has been pushed down to sit flat with the front panel.

Battery and Power Switch

Add a mini rocker switch and 9V battery and wire up to board.

Software

Connect a FTDI programmer to the Arduino Pro Mini and upload the software provided through the Arduino IDE.

Code

SimonV1.inoC/C++
/*
  ARDUINO SIMON
  Code: jbrad2089@gmail.com
 */
#include "Piano.h"
#include <NewTone.h>
#include "LedControl.h"

// FR ELISE (Initial tune)
const uint16_t melody_elise[] PROGMEM = {
  DUR_4|NOTE_E5, DUR_4|NOTE_DS5, DUR_4|NOTE_E5, DUR_4|NOTE_DS5, DUR_4|NOTE_E5, DUR_4|NOTE_B4, DUR_4|NOTE_D5, DUR_4|NOTE_C5, DUR_2|NOTE_A4, 
  DUR_8|NOTE_C4, DUR_4|NOTE_E3, DUR_4|NOTE_A4, DUR_2|NOTE_B4, DUR_8|NOTE_E3, DUR_4|NOTE_GS3, DUR_4|NOTE_B4, DUR_2|NOTE_C5,
  DUR_8|NOTE_E3,
  DUR_4|NOTE_E5, DUR_4|NOTE_DS5, DUR_4|NOTE_E5, DUR_4|NOTE_DS5, DUR_4|NOTE_E5, DUR_4|NOTE_B4, DUR_4|NOTE_D5, DUR_4|NOTE_C5, DUR_1|NOTE_A4, 
  DUR_4|REST, END_OF_TUNE 
};

// HOME ON THE RANGE (Win tune for reaching 99)
const uint16_t melody_range[] PROGMEM = {
  DUR_8|NOTE_G3, DUR_8|NOTE_G3, DUR_8|NOTE_C4, DUR_8|NOTE_D4, DUR_4|NOTE_E4, DUR_8|NOTE_C4, DUR_8|NOTE_B3, DUR_8|NOTE_A3, DUR_8|NOTE_F4, DUR_8|NOTE_F4, DUR_4|NOTE_F4,
  DUR_8|NOTE_E4, DUR_8|NOTE_F4, DUR_4|NOTE_G4, DUR_8|NOTE_C4, DUR_8|NOTE_C4, DUR_8|NOTE_C4, DUR_8|NOTE_B3, DUR_8|NOTE_C4, DUR_1|NOTE_D4,
  DUR_8|NOTE_G3, DUR_8|NOTE_G3, DUR_8|NOTE_C4, DUR_8|NOTE_D4, DUR_4|NOTE_E4, DUR_8|NOTE_C4, DUR_8|NOTE_B3, DUR_8|NOTE_A3, DUR_8|NOTE_F4, DUR_8|NOTE_F4, DUR_4|NOTE_F4,
  DUR_8|NOTE_F4, DUR_8|NOTE_F4, DUR_6|NOTE_E4, DUR_8|NOTE_D4, DUR_8|NOTE_C4, DUR_8|NOTE_B3, DUR_8|NOTE_C4, DUR_8|NOTE_D4, DUR_2|NOTE_C4, DUR_2|REST,
  DUR_2|NOTE_G4, DUR_8|NOTE_F4, DUR_6|NOTE_E4, DUR_8|NOTE_D4, DUR_1|NOTE_E4, 
  DUR_8|NOTE_G3, DUR_8|NOTE_G3, DUR_4|NOTE_C4, DUR_8|NOTE_C4, DUR_8|NOTE_C4, DUR_8|NOTE_C4, DUR_8|NOTE_C4, DUR_8|NOTE_B3, DUR_8|NOTE_C4, DUR_2|NOTE_D4,
  DUR_8|NOTE_G3, DUR_8|NOTE_G3, DUR_8|NOTE_C4, DUR_8|NOTE_D4, DUR_4|NOTE_E4, DUR_8|NOTE_C4, DUR_8|NOTE_B3, DUR_8|NOTE_A3, DUR_8|NOTE_F4, DUR_8|NOTE_F4, DUR_4|NOTE_F4,
  DUR_8|NOTE_F4, DUR_8|NOTE_F4, DUR_4|NOTE_E4, DUR_8|NOTE_D4, DUR_8|NOTE_C4, DUR_8|NOTE_B3, DUR_8|NOTE_C4, DUR_8|NOTE_D4, DUR_1|NOTE_C4,
  DUR_4|REST, END_OF_TUNE 
};

// Define pins
#define SPEAKER 5

// LED display
#define DATA 11
#define CLOCK 3
#define LOAD 2
LedControl lc=LedControl(DATA,CLOCK,LOAD,1);

//Switches and LEDS
#define RED_PIN 6
#define BLU_PIN 7
#define YEL_PIN 8
#define GRN_PIN 9

uint8_t colorToPin[] = {0, RED_PIN, BLU_PIN, YEL_PIN, GRN_PIN };
uint16_t colorToNote[] = {0, DUR_8|NOTE_A3, DUR_8|NOTE_C4, DUR_8|NOTE_E4, DUR_8|NOTE_G4 };
#define NOTE_WRONG DUR_4|NOTE_A2 

#define RANDOM_SEED_PIN A0

int score;                //The current number of steps
uint8_t simonSays[100];   //Step storage buffer
int lastLED = 0;          //Used when playing a tune so as not to repeat the same LED twice in a row

void setup() 
{
  //Speaker
  pinMode(SPEAKER, OUTPUT);

  //LCD Digits
  lc.shutdown(0,false);   //Wake up MAX7219
  lc.setScanLimit(0, 2);  //Using 2 digits
  lc.setIntensity(0,1);   //Minimum Brightness
  lc.clearDisplay(0);     //Clear display

  //Buttons / LEDs
  for (int p = 1; p <= 4; p++)
  {
    int pin = colorToPin[p];
    pinMode(pin, OUTPUT);
    digitalWrite(pin, HIGH);
  }

  //Play a tune as an opening theme
  randomSeed(analogRead(RANDOM_SEED_PIN));
  playSong(melody_elise, true);

  //Set players score
  score = 0;
  showValue(score, true);  
}

void loop() 
{
  //Get next note to play
  simonSays[score++] = random(1,5);
  showValue(score, true);
  //Play current tune
  for (int n = 0; n < score; n++)
  {
    showLED(simonSays[n], true);
    playNote(colorToNote[simonSays[n]] & 0x1FFF | DUR_4, false);
    showLED(simonSays[n], false);
  }

  //wait for response
  for (int n = 0; n < score; n++)
  {
    int expected = simonSays[n];
    int button = getPressedButton(expected);
    while (button == 0)
    {
      delay(50);
      button = getPressedButton(expected);
    }
    if (button != expected)
    {
      //Lose game
      flashValue(score, 10, 200);
      score = 0;
      break;
    }
    else if (score == 99)
    {
      //Win game
      flashValue(score, 20, 100);
      playSong(melody_range, false);
      score = 0;
      break;
    }
  }

  delay(1000);
}


// Plays a given song to completion
// melody - Pointer to notes array in program memory
// withLEDs - true to show random LED on each note played
int playSong(uint16_t* melody, bool withLEDs)
{

  //First count every note to play so we can show a count down
  int thisNote = 0;
  int numberOfNotes = 0;
  uint16_t noteRaw = pgm_read_word(&melody[thisNote++]);
  while (noteRaw != END_OF_TUNE)
  {
    if ((noteRaw & 0x1FFF) != REST)
    {
      numberOfNotes++;
    }
    noteRaw = pgm_read_word(&melody[thisNote++]);
  } //while

  //Play each note in the melody until the END_OF_TUNE note is encountered
  thisNote = 0;
  noteRaw = pgm_read_word(&melody[thisNote++]);
  while (noteRaw != END_OF_TUNE)
	{
    if ((noteRaw & 0x1FFF) != REST)
    {
      if (withLEDs)
      {
        showValue(numberOfNotes, true);
      }
      numberOfNotes--;
    }
		playNote(noteRaw, withLEDs);
    noteRaw = pgm_read_word(&melody[thisNote++]);
	} //while

	delay(50);
  noNewTone(SPEAKER);
  if (withLEDs)
  {
    lc.clearDisplay(0);     //Clear display
  }
}

uint16_t playNote(uint16_t noteRaw, bool withLEDs)
{
	// to calculate the note duration, take one second divided by the note type.
	// e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
	uint16_t frequency = noteRaw & 0x1FFF;
	uint8_t duration = (noteRaw & 0xE000) >> 13;
	if (duration == 7)
		duration = 8;
	uint16_t noteDuration = 1800 / duration;

	int led = 0;
	if (frequency != REST)
	{
    if (withLEDs)
    {
      //Switch on a different LED to last time
      led = random(1,5);
      while (led == lastLED)
      {
        led = random(1,5);
      }
      lastLED = led;
      
      showLED(led, true);
    }
		NewTone(SPEAKER, frequency, noteDuration);
	}
		
	// to distinguish the notes, set a minimum time between them.
	// the note's duration + 30% seems to work well:
	uint16_t pauseBetweenNotes = (noteDuration * 13) / 10;
	while (pauseBetweenNotes > 0) {
		_delay_ms(1);
		pauseBetweenNotes--;
	}

	if (frequency != REST)
	{
		// stop the tone playing:
		noNewTone(SPEAKER);
    if (withLEDs)
    {
      showLED(led, false);
    }
	}
	return frequency;
}

//Flashes a value on the display
// value - number to flash
// repeat - number of times to flash
// delta - time in mS between each state
void flashValue(int value, int repeat, int delta)
{
  for (int x=0; x < repeat;x++)
  {
    showValue(value, (x & 1) != 0);
    delay(delta);
  }
  showValue(value, true);
}

//Display a numeric value
// value - 0 to 99
// on - true to show value, false to turn off display
void showValue(int value, bool on)
{
  value = min(value, 99);
  int tens = value / 10;
  int units = value % 10;
  if (on)
  {
    lc.setDigit(0,1,units,false);
  }
  else 
  {
    lc.setChar(0,1,' ', false);
  }
  if (on && tens != 0)
  {
    lc.setDigit(0,0,tens,false);
  }
  else
  {
    lc.setChar(0,0,' ', false);
  }
}

//Shows or hides a LED
// led - 1 to 4, LED to change
// on - true is on, false is off
void showLED(int led, bool on)
{
  if (led > 0 && led <=4)
  {
    uint8_t pin = colorToPin[led];
    pinMode(pin, OUTPUT);
    digitalWrite(pin, (on) ? LOW : HIGH);
  }
}

//Tests if a button is pressed
// pin - Data pin button is connected to
// returns true if button is pressed
bool isButtonPressed(int pin)
{
    pinMode(pin, INPUT);      //Configure for input
    digitalWrite(pin, LOW);   //No pullup resistor
    bool state = (digitalRead(pin) == LOW);
    pinMode(pin, OUTPUT);     //Configure for output
    digitalWrite(pin, HIGH);  //Turn off LED
    return state;
}

//Tests if any of the buttons have been pressed and released
//  expected - button that should be pressed
//  returns the button that was pressed
//  notes: Displays LED and plays tone while button is pressed.
//  if button pressed matches expected, plays correct note otherwise plays NOTE_WRONG
int getPressedButton(int expected)
{
  bool pressed = false;
  int button = 0;
  uint8_t pin = 0;
  for (int b = 1; b <= 4; b++)
  {
    pin = colorToPin[b];
    if (isButtonPressed(pin))
    {
      button = b;
      break;
    }
  }
  
  if (pin != 0)
  {
    if (isButtonPressed(pin))
    {
      _delay_ms(5);
      if (isButtonPressed(pin))
      {
        //Play tone continiously while button is pressed.
        //Play NOTE_WRONG if button is not what is expected
        NewTone(SPEAKER, ((button == expected) ? colorToNote[button] : NOTE_WRONG) & 0x1FFF, 0);
        while (isButtonPressed(pin))
        {
        }
        noNewTone(SPEAKER);
        pressed = true;
      }
    }
  }
  return (pressed) ? button : 0;
}
Piano.hC Header File
#ifndef piano_h
#define piano_h

// Constants for notes
#define REST	 0
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define END_OF_TUNE 0xFFFF

#define DUR_8 0xE000
#define DUR_6 0xC000
#define DUR_4 0x8000
#define DUR_3 0x6000
#define DUR_2 0x4000
#define DUR_1 0x2000

#endif

Custom parts and enclosures

Case - Top
Case - Bottom
Display Bezel

Schematics

Schematic
Schematic ml9qtvawk9
Eagle Files
Schematic and PCB in Eagle Format
eagle_files_XhKh6vOxcv.zip

Comments

Similar projects you might like

Tacoyaki (Lights Out) Game

Project tutorial by John Bradnam

  • 337 views
  • 0 comments
  • 1 respect

Arduino Pocket Game Console + A-Maze - Maze Game

Project tutorial by Alojz Jakob

  • 10,485 views
  • 8 comments
  • 28 respects

Breadboard to PCB Part 1 - Making the Simon Says Game

Project tutorial by Katie Kristoff and Monica Houston

  • 8,462 views
  • 11 comments
  • 31 respects

Simon Style Memory Game with Agent Based Programming

Project tutorial by Embrio

  • 1,778 views
  • 4 comments
  • 8 respects

A Simple Simon Says Game

Project showcase by Arduino_Scuola

  • 28,581 views
  • 15 comments
  • 20 respects

ATtiny85 Mini Arcade: Snake

Project tutorial by Arduino “having11” Guy

  • 2,816 views
  • 7 comments
  • 19 respects
Add projectSign up / Login