After spending some time with the TM1638 LED display modules, the thoughts wandered to what sort of games they could be used with. The numbers and buttons merged into the thought of a number memory game – similar in theory of the popular “Simon” game by Milton Bradley:

Now back to the future. Instead of having four colours to blink in a certain sequence, our “Simone” game will randomly choose eight digits from one to eight. Then it (she?) will blink them across the module from left to right. At first the game starts with one digit, then two, all the way to eight. After the numbers have been displayed the user needs to key in the matching sequence of digits using the eight buttons below the display.

The purpose of this game is to simply test the user’s short term memory. When the game first starts the user is prompted to select a level, from one being the easiest to eight the most difficult. The greater the level, the less amount of time between the display of the digits to remember. This sounds odd but wait until the video at the end of this article for a demonstration.

Hardware

All you need is a regular Arduino or compatible board of some sort, the TM1638 display module, and if you like beeps a piezo buzzer. I have mounted the buzzer and a header for the display on a protoshield, with the buzzer connected to digital eleven:

Software

The Arduino sketch was written in v23 and is as follows:

Arduino

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

// simple numerical memory game using TM1638 LED display and Arduino style board

// John Boxall CC by-sa-nc tronixstuff.com/projects | March 2012

#include <TM1638.h> // See http://code.google.com/p/tm1638-library/

// define a TM1638 module on data pin 8, clock pin 7 and strobe pin 6

TM1638module(8,7,6);

bytebuttons;

intlevel;

voidsetup()

{

pinMode(11,OUTPUT);// for piezo buzzer

randomSeed(analogRead(0));// for random number generator

preGame();

}

voidpreGame()

{

// say Hello and get the level the user wants to play

// the level is the number of milliseconds between displaying each number to remember

module.setDisplayToString(" HELLO ",1);

for(intz=1;z<5;z++)

{

piezoBeep(z);

}

module.setDisplayToString(" LEVEL? ",0);

do// wait for user to select level

{

buttons=module.getButtons();

}

while(buttons==0);

switch(buttons)

{

case1:

level=1000;

break;

case2:

level=900;

break;

case4:

level=800;

break;

case8:

level=700;

break;

case16:

level=600;

break;

case32:

level=500;

break;

case64:

level=400;

break;

case128:

level=300;

break;

}

module.clearDisplay();

}

voidpiezoBeep(inttype)// used to make beeps

{

longduration=250000;

intfreq;

switch(type)

{

case1:

freq=600;

break;

case2:

freq=700;

break;

case3:

freq=800;

break;

case4:

freq=900;

break;

case5:

freq=1000;

break;

case6:

freq=1100;

break;

case7:

freq=1200;

break;

case8:

freq=1300;

break;

}

intperiod=(1.0/freq)*1000000;

longelapsed_time=0;

while(elapsed_time<duration)

{

digitalWrite(11,HIGH);

delayMicroseconds(period/2);

digitalWrite(11,LOW);

delayMicroseconds(period/2);

elapsed_time+=(period);

}

}

voidplayGame()

{

intgameNumbers[9];// stores numbers to remember

intuserNumbers[9];// stores users' presses

intcount=1;// tracks number of digits per round

intz;// for various loops etc.

booleancorrect=true;

delay(1000);

do

{

for(intz=1;z<9;z++)// get random numbers for game

{

gameNumbers[z]=random(1,9);

}

// display numbers to remember

for(inti=1;i<=count;i++)

{

module.setDisplayDigit(gameNumbers[i],(i-1),false);

piezoBeep(gameNumbers[i]);

delay(level);

module.clearDisplay();

}

// get user attempts

for(z=1;z<=count;z++)

{

do// wait for user to press button

{

buttons=module.getButtons();

}

while(buttons==0);

delay(300);// for debounce

switch(buttons)

{

case1:

userNumbers[z]=1;

break;

case2:

userNumbers[z]=2;

break;

case4:

userNumbers[z]=3;

break;

case8:

userNumbers[z]=4;

break;

case16:

userNumbers[z]=5;

break;

case32:

userNumbers[z]=6;

break;

case64:

userNumbers[z]=7;

break;

case128:

userNumbers[z]=8;

break;

}

module.setDisplayDigit(userNumbers[z],(z-1),false);

delay(200);

module.clearDisplay();

}

// check for incorrect entries

for(intz=1;z<=count;z++)

{

if(userNumbers[z]!=gameNumbers[z])

{

correct=false;

}

}

count++;

}

while((correct==true)&&(count<9));

delay(1000);

if(correct==true)

{

module.setDisplayToString("YOU WIN ",0);

}

if(correct==false)

{

module.setDisplayToString("YOU LOSE",0);

}

delay(1000);

module.setDisplayToString("GO AGAIN",0);

delay(1000);

module.clearDisplay();

}

voidloop()

{

playGame();

delay(1000);

}

The sketch isn’t anything special, and gives the user the framework for perhaps something more involved or customised. Or at least a good distraction from doing some real work. *ahem* However here it is in action:

Conclusion

Although the “Simone” game was quite simple, and a quick knock-up job – I’m sure those of you with more imagination could have some fun with the sketch and so on. It is easy to follow and another interesting use of the display modules – the best $10 I’ve spent for some time.

Have fun and keep checking into tronixstuff.com. Why not follow things on twitter, Google+, subscribe for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other – and we can all learn something.