-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_project2.ino
160 lines (154 loc) · 4.78 KB
/
final_project2.ino
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
//www.elegoo.com
//2016.12.12
//Erik Brown and Miguel Studejo
//Hexadecimal joystick game
//This program is a basic computer game that generates a number, displays it on a 4 digit digital display, then has the user move the joystick to a corresponding position
int latch=9; //74HC595 pin 9 STCP
int clock=10; //74HC595 pin 10 SHCP
int data=8; //74HC595 pin 8 DS
int x;//Randomly generated number
int delayTime=5100;
int lives=3;
int score=0;
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = A0; // analog pin connected to X output
const int Y_pin = A1; // analog pin connected to Y output
unsigned char table[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c
,0x39,0x5e,0x79,0x71,0x00};
void setup() {
pinMode(latch,OUTPUT);
pinMode(clock,OUTPUT);
pinMode(data,OUTPUT);
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void Display(unsigned char num)
{
digitalWrite(latch,LOW);
shiftOut(data,clock,MSBFIRST,table[num]);
digitalWrite(latch,HIGH);
}
void loop() {
do{
x=random(0,15);//Generate a random number between 0 and 15
switch(x){
case 0:
Display(0);
break;
case 1:
Display(1);
break;
case 2:
Display(2);
break;
case 3:
Display(3);
break;
case 4:
Display(4);
break;
case 5:
Display(5);
break;
case 6:
Display(6);
break;
case 7:
Display(7);
break;
case 8:
Display(8);
break;
case 9:
Display(9);
break;
case 10:
Display(10);
break;
case 11:
Display(11);
break;
case 12:
Display(12);
break;
case 13:
Display(13);
break;
case 14:
Display(14);
break;
case 15:
Display(15);
break;
}//Display the generated number on the display
delayTime=delayTime-150;//Decrease the time the user has to move the joystick every turn
delay(delayTime);//This is the time the user has to move the joystick to the correct position
if((x>0)&&(x<8)&&(x%2==1)){//Case if number is less than 8 and odd
if((analogRead(X_pin)>512)&&(analogRead(Y_pin)<512)){//Check if joystick is in the first quadrant
Serial.print("Good Job!\n");//Case if joystick position is correct
score++;
}
else{
Serial.print("That's not it!\n");//Case if joystick position is wrong
lives--;
Serial.print(lives);
Serial.print(" lives left!\n");
}
}
else if((x>0)&&(x<8)&&(x%2==0)){//Case if number is less than 8 and even
if((analogRead(X_pin)<512)&&(analogRead(Y_pin)<512)){//Check if joystick is in the second quadrant
Serial.print("Good Job!\n");//Case if joystick position is correct
score++;
}
else{
Serial.print("That's not it!\n");//Case if joystick position is wrong
lives--;
Serial.print(lives);
Serial.print(" lives left!\n");
}
}
else if((x>8)&&(x<16)&&(x%2==0)){//Case if number is greater than 8 and even
if((analogRead(X_pin)<512)&&(analogRead(Y_pin)>512)){//Check if joystick is in the third quadrant
Serial.print("Good Job!\n");//Case if joystick position is correct
score++;
}
else{
Serial.print("That's not it!\n");//Case if joystick position is wrong
lives--;
Serial.print(lives);
Serial.print(" lives left!\n");
}
}
else if((x>8)&&(x<16)&&(x%2==1)){//Case if number is greater than 8 and odd
if((analogRead(X_pin)>512)&&(analogRead(Y_pin)>512)){//Check if joystick is in the fourth quadrant
Serial.print("Good Job!\n");//Case if joystick position is correct
score++;
}
else{
Serial.print("That's not it!\n");//Case if joystick position is wrong
lives--;
Serial.print(lives);
Serial.print(" lives left!\n");
}
}
else if((x==8)||(x==0)){//Case if number is 0 or 8
if((digitalRead(SW_pin))==0){//Check if joystick is pressed down
Serial.print("Good Job!\n");//Case if joystick position is correct
score++;
}
else{
Serial.print("That's not it!\n");//Case if joystick position is wrong
lives--;
Serial.print(lives);
Serial.print(" lives left!\n");
}
}
}while(lives>0);//Loop ends when user loses all 3 lives
Serial.print("Game Over\n");//The game ends and the user's score is displayed
Serial.print("Score: ");
Serial.print(score);
delay(10000000000000);
}