Interfacing Game - Arduino
This is a circuit built with a Teensy3.2 microcontroller and a program written with Arduino, to create a small game of matching numbers. The circuit will display a random number, and the user must press the connected button on the same number twice to win.
The circuit utilizes an 7-segment display to display a random number, a shift register to output the information, a button to receive user input and a variety of cables and resistors. The program was written using a standard looping format.
Source Code
1 /**
2 ** Name: Mostapha Abdelaziz
3 **/
4
5 #include <Bounce2.h> // import Bounce2
6
7 void countUpdate(int num = -1);
8
9 Bounce pushButton = Bounce();
10
11 const int TIMING = 500;
12
13 int SER = 14; //data
14 int LATCH = 15; //RCLK
15 int CLOCK = 16; //SRCLK
16
17 int pshBttn = 19;
18
19 int count = 0;
20 int savedCount = 0;
21
22 int bttnCount = 0;
23 int roundCount = 1;
24
25 unsigned long lastTime = 0;
26
27 // decimal representations of flipped(common anode) binary numbers for the 7-Segment display, ending with the decimal and display off
28 int numbers[12] = {136, 235, 76, 73, 43, 25, 24, 203, 8, 9, 247, 255};
29
30 void setup() {
31 Serial.begin(9600);
32 Serial.print("Round #");
33 Serial.print(roundCount);
34 Serial.println("…");
35 // set pin modes
36 pinMode(LATCH, OUTPUT);
37 pinMode(CLOCK, OUTPUT);
38 pinMode(SER, OUTPUT);
39 pinMode(pshBttn, INPUT);
40
41 // attach pbs pin to bounce object
42 pushButton.attach(pshBttn);
43 pushButton.interval(5);
44
45 // shuffle random function
46 randomSeed(analogRead(22));
47 }
48
49 void loop() {
50 // update bounce object
51 pushButton.update();
52
53 // if buton was pressed, use match game logic
54 if (pushButton.fell() == true) {
55 updateCheckMatch();
56 }
57
58 // change the count after the set timing interval
59 if ((millis() - lastTime) >= TIMING) {
60 countUpdate();
61 lastTime = millis();
62 }
63 }
64
65 void countUpdate(int num = -1) {
66 if (num == -1) {
67 // generate random number
68 int prevRand = count;
69 while (count == prevRand) {
70 count = random(0, 10);
71 }
72 }
73 else {
74 count = num;
75 }
76
77 // set latch low until changes are made then return to high
78 digitalWrite(LATCH, LOW);
79 shiftOut(SER, CLOCK, MSBFIRST, numbers[count]);
80 digitalWrite(LATCH, HIGH);
81 }
82
83 void updateCheckMatch() {
84 bttnCount++;
85 Serial.print("PBS press # ");
86 Serial.print(bttnCount);
87 Serial.print(" ==> ");
88 Serial.println(count);
89 delay(2000);
90 // if this is the first press save count, otherwise compare and restart
91 if (bttnCount == 1) {
92 savedCount = count;
93 } else {
94 if (savedCount == count) {
95 Serial.println("**MATCH**");
96 Serial.println();
97 // blink for two seconds
98 for (int i = 0; i < 4; i++) {
99 countUpdate(11);
100 delay(250);
101 countUpdate(savedCount);
102 delay(250);
103 }
104 } else {
105 Serial.println("NO Match… period.");
106 Serial.println();
107 // display the decimal point on the 7-segment display for 2-seconds
108 for (int i = 0; i < 4; i++) {
109 // set latch low until changes are made then return to high
110 countUpdate(10);
111 delay(250);
112 countUpdate(11);
113 delay(250);
114 }
115 }
116 // restart, new round
117 bttnCount = 0;
118 roundCount++;
119 Serial.print("Round #");
120 Serial.print(roundCount);
121 Serial.println("…");
122 }
123 lastTime = millis();
124 }
125