Raul PalomoTre HensonGerman Gonzalez
Published © GPL3+

Memory Match

Test your memory in Memory Match! Each round you are shown a sequence of numbers that your must input back. The sequence grows each round.

BeginnerFull instructions provided3 hours684
Memory Match

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Seeed Studio 4-digit display - Seeed Grove Starter Kit for LaunchPad
×1
Seeed Studio Buzzer Seeed Grove Starter Kit for LaunchPad
×1
Breadboard (generic)
Breadboard (generic)
From Sidekick Basic Kit for TI LaunchPad
×1
Seeed Studio button
From Sidekick Basic Kit for TI LaunchPad
×4
Seeed Studio Jumper wire
From Sidekick Basic Kit for TI LaunchPad
×9
Seeed Studio LaunchPad MSP430G2 x 1
From Grove Starter Kit for LaunchPad
×1
Seeed Studio Jumper Wires
From Grove Starter Kit for LaunchPad
×2

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Diagram

Code

Memory Match Code

C/C++
C file to be used in an Arduino environment
/*
*/
#include <stdio.h> 
#include <stdlib.h> 
#include "TM1637.h"
#define CLK 39//pins definitions for TM1637 and can be changed to other ports
#define DIO 38
#define button1 28
#define button2 27
#define button3 10
#define button4 26
#define BUZZER_PIN 40

TM1637 tm1637(CLK, DIO); 
int sequence[1000] = {};
bool generate_num = true;
bool display_sequence = true;
bool wait_UI = true;
int index2 = 0; /*lets us know how many numbers, user has been shown*/
int UI_index = 0; /*lets us know */
bool read1 = false;
void setup()
{
  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
  pinMode(button1, INPUT);
  attachInterrupt(digitalPinToInterrupt(button1),digit1, RISING);
  pinMode(button2, INPUT);
  attachInterrupt(digitalPinToInterrupt(button2),digit2, RISING);
  pinMode(button3, INPUT);
  attachInterrupt(digitalPinToInterrupt(button3),digit3, RISING);
  pinMode(button4, INPUT);
  attachInterrupt(digitalPinToInterrupt(button4),digit4, RISING);
  pinMode(BUZZER_PIN, OUTPUT);
}

void  digit1()
{
  if (wait_UI&& not(read1)){
    display_digit(1);
    }
}
void  digit2()
{
  if (wait_UI&& not(read1)){
    display_digit(2);
    }
}
void  digit3()
{
  if (wait_UI&& not(read1)){
    display_digit(3);
    }
}
void  digit4()
{
    if (wait_UI && not(read1)){
    display_digit(4);
    }
}

void fail(){/*function to let users know that their input is wrong*/
  int score = index2-2;
  int ten = index2 / 10;
  int ones = index2 % 10;
  for(int f = 0; f < 3; f++){
  tm1637.display(0, 0);
  tm1637.display(1, 0);
  tm1637.display(2, 0);
  tm1637.display(3, 0);
  playTone(1000, 400); /*Plays the ugly noise when you fail*/
  tm1637.clearDisplay();
  }
  while(1){
  tm1637.display(2, ten);
  tm1637.display(3, ones);
  }
}

void display_digit(int x){
  read1 = true;
  tm1637.display(x-1, x);/*lets user know which button they pressed*/
  
  if (sequence[UI_index] == x){ /* If the input is correct, user moves on*/
  UI_index++;
  delay(200);
  tm1637.clearDisplay();
  read1 = false;
  }
  else{/* If the input is incorrext, fail*/
  fail();
  }
  }

void add_num(){ /*adds random number to end of sequence*/
  int n = 0;
  n = random(1,5);
  sequence[index2] = n;
  }

void d_seq(){ /* displays sequence for user to see*/
  for(int i = 0; i <= index2; i++){
      tm1637.clearDisplay();      
      delay(200);
      tm1637.display(sequence[i]-1, sequence[i]);
      delay(200);
      tm1637.clearDisplay();
      delay(200);
    }
  }

void playTone(int tone, int duration) 
{
  for (long i = 0; i < duration * 1000L; i += tone * 2) 
  {
    digitalWrite(BUZZER_PIN, HIGH);
    delayMicroseconds(tone);
    digitalWrite(BUZZER_PIN, LOW);
    delayMicroseconds(tone);
  }
}



void loop()
{
if (generate_num){
  add_num();
  generate_num = false;
  wait_UI = false;
  display_sequence = true;
}
if (display_sequence){
  d_seq();
  display_sequence = false;
  generate_num = false;
  wait_UI = true;
}
if(wait_UI){
if (UI_index> index2){
 playTone(750, 200);
 playTone(850, 200);
 UI_index = 0; /*User has to recall all numbers in the sequence each time*/
 index2++;
 wait_UI = false;
 display_sequence = false;
 generate_num = true;
}
}
}

Credits

Raul Palomo
1 project • 0 followers
Contact
Tre Henson
1 project • 0 followers
Contact
German Gonzalez
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.