-
Notifications
You must be signed in to change notification settings - Fork 0
/
score_keeping.ino
179 lines (148 loc) · 4.63 KB
/
score_keeping.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright (c) 2024 Samuel Asebrook
*/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define the OLED screen
Adafruit_SSD1306 display(128, 64, &WIRE, -1);
// Define the pin numbers for the sensors
const int opticalSensorPin = A0; // 5V
const int limitSwitchPin = 2; // 3.3V
// LCD SDA Pin = A4
// LCD SCL Pin = A5
unsigned long startTime;
unsigned long elapsedTime;
int multiplier = 1;
int score = 0;
int highScore = 0;
bool gameStarted = false;
bool state0_moved = false;
bool state1_moved = false;
bool state2_moved = false;
bool state3_moved = false;
bool state4_moved = false;
int state_0, state_1, state_2, state_3, state_4 = 0; // Dummy variables strictly for local compilation purposes, REMOVE UPON GROUP COMPILATION
/*
void scrollText(int row, String message, int delayTime) {
message = message + " ";
for (int i = 0; i < message.length(); i++) {
lcd.setCursor(0, row);
lcd.print(message.substring(i));
delay(delayTime);
}
}
*/
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Hell Yeah!");
display.setCursor(0,16);
display.println("Pinball!");
display.display();
pinMode(opticalSensorPin, INPUT);
pinMode(limitSwitchPin, INPUT_PULLUP);
}
void loop() {
int opticalSensorState = digitalRead(opticalSensorPin);
int limitSwitchState = digitalRead(limitSwitchPin);
if (opticalSensorState == HIGH && !gameStarted) {
// The launcher has been pulled back, start the game
startTime = millis();
gameStarted = true;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Good Luck!");
display.setCursor(0,16);
display.println("You Will Need It!");
display.display();
delay(1000);
}
if ((state_1 == 1 && state1_moved == false) || (state_2 == 1 && state2_moved == false) || (state_3 == 1 && state3_moved == false) || (state_4 == 1 && state4_moved == false) ) {
score = score + (multiplier * 100);
if (state_1 == 1) {
state1_moved = true;
} else if (state_2 == 1) {
state2_moved = true;
} else if (state_3 == 1) {
state3_moved = true;
} else if (state_4 == 1) {
state4_moved = true;
}
}
if ((state_1 == 2 && state1_moved == true) || (state_2 == 2 && state2_moved == true) || (state_3 == 2 && state3_moved == true) || (state_4 == 2 && state4_moved == true) ) {
score = score + (multiplier * 200);
if (state_1 == 2) {
state1_moved = false;
} else if (state_2 == 2) {
state2_moved = false;
} else if (state_3 == 2) {
state3_moved = false;
} else if (state_4 == 2) {
state4_moved = false;
}
}
if ((state_1 == 3 && state1_moved == false) || (state_2 == 3 && state2_moved == false) || (state_3 == 3 && state3_moved == false) || (state_4 == 3 && state4_moved == false) ) {
score = score + (multiplier * 300);
if (state_1 == 3) {
state1_moved = true;
} else if (state_2 == 3) {
state2_moved = true;
} else if (state_3 == 3) {
state3_moved = true;
} else if (state_4 == 3) {
state4_moved = true;
}
}
if ((state_1 == 4 && state1_moved == true) || (state_2 == 4 && state2_moved == true) || (state_3 == 4 && state3_moved == true) || (state_4 == 4 && state4_moved == true) ) {
score = score + (multiplier * 1000);
if (state_1 == 4) {
state1_moved = false;
} else if (state_2 == 4) {
state2_moved = false;
} else if (state_3 == 4) {
state3_moved = false;
} else if (state_4 == 4) {
state4_moved = false;
}
}
if (state_1 == 4 && state_2 == 4 && state_3 == 4 && state_4 == 4) {
state_1 == 0;
state_2 == 0;
state_3 == 0;
state_4 == 0;
multiplier = multiplier * 2;
}
if (gameStarted && limitSwitchState == LOW) {
// The limit switch has been pressed, end the game
elapsedTime = millis() - startTime;
gameStarted = false;
// Calculate the score
score = score + (elapsedTime / 100);
if (score > highScore) {
highScore = score;
}
// Display the score on the OLED
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Score: ");
display.println(score);
display.setCursor(0,16);
display.print("High: ");
display.println(highScore);
display.display();
// Reset game score and score multiplier at end of game
score = 0;
multiplier = 1;
}
}