Repository navigation
Expand file tree
/
Copy pathSensor.ino
More file actions
232 lines (197 loc) · 5.24 KB
/
Copy pathSensor.ino
File metadata and controls
232 lines (197 loc) · 5.24 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
Sensor.ino - Sensor Library for the Infinitag System.
Created by Jani Taxidis & Tobias Stewen & Florian Kleene.
Info: www.infinitag.io
All files are published under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
License: https://creativecommons.org/licenses/by-nc-sa/4.0/
*/
// Infinitag Libs
#include <Infinitag_Core.h>
// Vendor Libs
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <EEPROM.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <IRremote.h>
//#include <sensor_dhcp.h>
// Settings
#include "Settings.h"
// Infinitag Inits
Infinitag_Core infinitagCore;
// Vendor Inits
Adafruit_NeoPixel strip = Adafruit_NeoPixel(ledAmount, ledPin, NEO_GRBW + NEO_KHZ800);
decode_results irResults;
IRrecv irRecv(irReceivePin);
void setup() {
Serial.begin(57600);
checkAndSetBrightness();
// LEDs
strip.begin();
waitLed(2);
//Wait For Master to Boot
gI2cAddress = 0x22;
//gI2cAddress = queryI2CAddressFromMaster();
Wire.begin(gI2cAddress);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
// IR
irRecv.enableIRIn();
// Animate
animateNextStep = millis();
}
void loop() {
if (irRecv.decode(&irResults)) {
infinitagCore.irDecode(irResults.value);
if(infinitagCore.irRecvTeamId != playerTeamId)
{
setValidIrShot(irResults.value);
}
irRecv.enableIRIn();
}
animation();
delay(10);
}
void waitLed(int loops){
for (int i = 0; i < (loops * 2); i++) {
for (int cStep = 0; cStep < ledAmount; cStep++) {
for (int cLed = 0; cLed < ledAmount; cLed++) {
strip.setPixelColor(cLed, strip.Color(0, 0, 0, (cStep == cLed) ? c(255) : 0));
}
strip.show();
delay(166);
}
}
}
void checkAndSetBrightness() {
brightnessSensorValue = analogRead(brightnessSensorPin);
if (brightnessSensorValue <= 100) {
brightnessSensorFaktor = 0.1;
} else if (brightnessSensorValue <= 200) {
brightnessSensorFaktor = 0.3;
} else if (brightnessSensorValue <= 300) {
brightnessSensorFaktor = 0.6;
} else {
brightnessSensorFaktor = 1;
}
}
void brightnessTest() {
for (int cLed = 0; cLed < ledAmount; cLed++) {
strip.setPixelColor(cLed, strip.Color(0, 0, 0, 0));
}
strip.show();
delay(30);
brightnessSensorValue = analogRead(0);
for (int cLed = 0; cLed < ledAmount; cLed++) {
if (brightnessSensorValue <= 100) {
strip.setPixelColor(cLed, strip.Color(255, 0, 0, 0));
} else if (brightnessSensorValue <= 200) {
strip.setPixelColor(cLed, strip.Color(0, 255, 0, 0));
} else if (brightnessSensorValue <= 300) {
strip.setPixelColor(cLed, strip.Color(0, 0, 255, 0));
} else {
strip.setPixelColor(cLed, strip.Color(0, 0, 0, 255));
}
}
strip.show();
delay(100);
}
void receiveEvent(int howMany) {
int byteCounter = 0;
byte data[9] = {
B0,
B0,
B0,
B0,
B0,
B0,
B0,
B0,
B0,
};
while (Wire.available()) {
data[byteCounter] = Wire.read();
byteCounter++;
}
switch (data[0]) {
// Set TeamId
case 0x02:
if (byteCounter == 2) {
playerTeamId = data[1];
}
break;
// Set PlayerId
case 0x03:
if (byteCounter == 2) {
playerId = data[1];
}
break;
// Set Animation
case 0x04:
if (byteCounter == 9) {
animateCurrentStep = 0;
animateNextStep = 0;
animateCurrentAnimation = data[1];
animateTime = data[3] << 8 | data[2];
animateColor[0] = data[4];
animateColor[1] = data[5];
animateColor[2] = data[6];
animateColor[3] = data[7];
}
break;
// Set Alive
case 0x07:
if (byteCounter == 2) {
setAlive((data[1] == 0x01));
}
break;
}
}
void requestEvent() {
Wire.write(lastShot, 4);
lastShot[0] = 0;
lastShot[1] = 0;
lastShot[2] = 0;
lastShot[3] = 0;
}
void setValidIrShot(unsigned long code) {
byte result[3];
infinitagCore.irToBytes(code, &result[0]);
lastShot[0] = 0x06;
lastShot[1] = result[0];
lastShot[2] = result[1];
lastShot[3] = result[2];
}
void setAlive(bool alive) {
playerAlive = alive;
}
void animation() {
if (animateCurrentAnimation > 0) {
if (animateNextStep <= millis()) {
for (int i = 0; i < ledAmount; i++) {
strip.setPixelColor(i, strip.Color(
c(animatePattern[animateCurrentAnimation][animateCurrentStep][i] * animateColor[0] / 100),
c(animatePattern[animateCurrentAnimation][animateCurrentStep][i] * animateColor[1] / 100),
c(animatePattern[animateCurrentAnimation][animateCurrentStep][i] * animateColor[2] / 100),
c(animatePattern[animateCurrentAnimation][animateCurrentStep][i] * animateColor[3] / 100)
));
}
strip.show();
animateNextStep = millis() + animateTime;
animateCurrentStep++;
if (animateCurrentStep > animateSteps[animateCurrentAnimation] - 1) {
animateCurrentStep = 0;
}
}
} else {
for (int i = 0; i < ledAmount; i++) {
strip.setPixelColor(i, strip.Color(0,0,0,0));
}
strip.show();
checkAndSetBrightness();
}
}
uint16_t c(uint16_t inputColor) {
return gammaCorrection[((uint16_t)(brightnessSensorFaktor * (float)inputColor))];
}