-
Notifications
You must be signed in to change notification settings - Fork 0
/
LedRoulette.ino
207 lines (164 loc) · 4.83 KB
/
LedRoulette.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// -------------------- Led Roulette -------------------- //
//Written By: Aarni Junkkala
//Libraries
#include <Adafruit_NeoPixel.h>
//Pins
const byte ButtonStart = 2;
int LedPin = 3;
int speaker = 4;
//Led setup
int NUMPIXELS = 12; // number of neopixels in strip
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LedPin, NEO_GRB + NEO_KHZ800);
//Time between ball moving from one led to another
int delayval = 25; // 10 is too fast, 50 too slow.
int delayvalReset = 0; //Resetvalue
//Color of the ball
int redColor = 0;
int greenColor = 0;
int blueColor = 255;
//Color of fader
int redFader = 50;
int greenFader = 50;
int blueFader = 50;
//Winning Color
int redWin = 255;
int blueWin = 0;
int greenWin = 0;
//Color of start Setup
int redStart = 100;
int greenStart = 0;
int blueStart = 100;
float clicking = 220; //Tone that speaker will make clicking happen
//If true, button needs to pressed one more time to stop the roulette
bool SecondButtonPress = false;
void setup() {
Serial.begin(9600);
delayvalReset = delayval;
//Pin modes.
pinMode(ButtonStart, INPUT_PULLUP);
pinMode(speaker, OUTPUT);
//Starts the led library
pixels.begin();
//Effect to show that code has started running and to set all leds off
StartEffect();
}
void loop()
{
//Reads the ButtonStart. Holds the code untill button is pressed.
while (digitalRead(ButtonStart) == LOW)
{}
//Resets stats that are required to be resetted.
delayval = delayvalReset;
//Resets the leds before spinning
for (int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
//When game starts rolls 3 rounds ignoring input
for(int i = 0; i < 3; i++)
{
SpinBall();
}
//Repeats rounds for long as new ButtonStart input is detected
while (SecondButtonPress == true and digitalRead(ButtonStart) == LOW){
SpinBall();
}
//Last rounds before stopping, makes it fell more natural
for(int i = 0; i < 5; i++)
{
SpinBall();
}
//Reset leds
for(int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(NUMPIXELS - i - 1, pixels.Color(0, 0, 0));
}
//Slower rounds.
for(int k = 0; k < 2; k++)
{
SpinBallSlow();
}
//Final round
SpinBallFinale();
}
void StartEffect(){
//Resets all leds to be off.
for(int j = 0; j < NUMPIXELS; j++)
{
pixels.setPixelColor(j, pixels.Color(0, 0, 0));
}
pixels.show();
delay(50);
//Starting color, where spins one rouond showing color of purple.
for(int j = 0; j < NUMPIXELS; j++)
{
pixels.setPixelColor(j, pixels.Color(redStart, greenStart, blueStart));
delay(25);
pixels.show();
}
delay(50);
//Reset leds to be off.
for(int j = 0; j < NUMPIXELS; j++)
{
pixels.setPixelColor(j, pixels.Color(0, 0, 0));
delay(25);
pixels.show();
}
}
//Spins the ball for a single round
void SpinBall(){
for (int i=0; i < NUMPIXELS; i++)
{
//Resets the led that is 2 before current.
pixels.setPixelColor((i + NUMPIXELS - 2) % NUMPIXELS, pixels.Color(0, 0, 0));
//Sets the color of the ball
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
//and Fader
pixels.setPixelColor((i + NUMPIXELS - 1) % NUMPIXELS, pixels.Color(redFader, greenFader, blueFader));
pixels.show();
tone(speaker, clicking, 5);
delay(delayval);
}
}
//Spins the ball for a single round
void SpinBallSlow(){
for (int i=0; i < NUMPIXELS; i++)
{
//Resets the led that is 2 before current.
pixels.setPixelColor((i + NUMPIXELS - 1) % NUMPIXELS, pixels.Color(0, 0, 0));
//Sets the color of the ball
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
pixels.show();
tone(speaker, clicking, 5);
delayval += random(4,8);
delay(delayval);
}
}
void SpinBallFinale()
{
//Precalculates the winning number
int winNum = NUMPIXELS - random(1,NUMPIXELS); //Made this way, so if you have led ring of different amout of leds, you don't have change anything here.
for (int i = 0; i < winNum; i++)
{
//Resetoi ledit
for(int j = 0; j < NUMPIXELS; j++)
{
pixels.setPixelColor(j, pixels.Color(0, 0, 0));
}
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
if(i == winNum - 1){
pixels.setPixelColor(i, pixels.Color(redWin, blueWin, greenWin));
pixels.show();
tone(speaker, 1479.98, 100);
delay(100);
tone(speaker, 1760.00, 300);
}
else{
tone(speaker, clicking, 5);
pixels.show();
}
//For the end delay increases
delayval += random(20,45);
// Delay for a period of time (in milliseconds).
delay(delayval);
}
}