-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNerfGallery.ino
170 lines (146 loc) · 3.2 KB
/
NerfGallery.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
/**
* NERF gallery
*
* A shooting gallery for nerf type guns using piezo elements on targets.
*
* Servos hold the targets, which have a "good" side and either a "bad" side
* or a bonus side. One side is turned to the player, and when hit their
* score is adjusted accordingly. Nice sounds & reactions happen too.
*
* Copyright Nathan Gray, 2015
*/
#include <TimerOne.h>
#include <Bounce2.h>
#include <Servo.h>
#include <Metro.h>
#define FASTADC 1
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
int T_COUNT = 9;
const int LIGHTS = 11;
#include "Game.h"
// Game States
#define GAME_IDLE 0
#define GAME_PLAY 1
#define GAME_OVER 10
Target *targets[] = {
// A B C
new Target(0,0,-1), new Target(1,1,-1), new Target(2,2,-1), // 1
new Target(3,3,-1), new Target(4,4, 5), new Target(5,5,-1), // 2
new Target(6,6, 5), new Target(7,9,-1), new Target(8,10,5), // 3
};
//int T_COUNT = sizeof(targets) / sizeof(targets[0]);
// Display
#define NUM_LEDS 28
#define LED_PIN 24
const byte digits[10][7] = {
{0,1,1,1,1,1,1}, // 0
{0,0,0,0,0,1,1}, // 1
{1,1,0,1,1,0,1}, // 2
{1,1,0,0,1,1,1}, // 3
{1,0,1,0,0,1,1}, // 4
{1,1,1,0,1,1,0}, // 5
{1,1,1,1,1,1,0}, // 6
{0,1,0,0,0,1,1}, // 7
{1,1,1,1,1,1,1}, // 8
{1,1,1,0,1,1,1} // 9
};
// Different game types
Knockdown knockdown;
Timed timed;
Game *games[] = {
&knockdown,
&timed
};
Game *game;
Metro gameTick = Metro(100);
// State variables
byte currentState = GAME_IDLE;
int score = 0;
void setup() {
#if FASTADC
// set prescale to 16
sbi(ADCSRA,ADPS2) ;
cbi(ADCSRA,ADPS1) ;
cbi(ADCSRA,ADPS0) ;
#endif
Serial1.begin(9600);
Serial1.setTimeout(1000);
pinMode(LIGHTS,OUTPUT);
digitalWrite(LIGHTS,LOW);
// Servo check
for(byte i = 0; i < T_COUNT; i++)
{
delay(100);
targets[i]->setState(T_SIDE_B);
}
for(byte i = 0; i < T_COUNT; i++)
{
delay(100);
targets[i]->setState(T_SIDE_A);
}
for(byte i = 0; i < T_COUNT; i++)
{
delay(100);
targets[i]->setState(T_IDLE);
}
for(byte i = 0; i < T_COUNT; i++)
{
int score = targets[i]->hit();
}
delay(5000);
// Pick game type
Serial1.println("Choose");
for(byte i = 0; i < 2; i++)
{
targets[i]->bSideDelay = 0;
targets[i]->setState(T_SIDE_B);
}
int type = 10;
while(type == 10)
{
for(byte i = 0; i < 2; i++)
{
if(targets[i]->hit())
{
type = i;
}
}
}
game = games[type];
game->start(60);
digitalWrite(LIGHTS,HIGH);
}
void loop() {
if(gameTick.check() && !game->tick())
{
game->stop();
digitalWrite(LIGHTS,LOW);
delay(1000);
// Call this to make sure servos turn off
checkHits();
boolean start= false;
while(!start)
{
start = Serial1.find("Start");
Serial.print(".");
}
game->start(60);
digitalWrite(LIGHTS,HIGH);
gameTick.reset();
}
checkHits();
}
void checkHits() {
for(byte i = 0; i < T_COUNT; i++)
{
game->score(targets[i]->hit());
}
// Reset hit counter
Target::hit_counter = 0;
}