-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZapper.h
117 lines (103 loc) · 3.88 KB
/
Zapper.h
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
#ifndef ZAPPER_H
#define ZAPPER_H
#include "Demo.h"
#include "Color.h"
#define ZAP_FREQUENCY 5 // Lower number means less frequent zap
// Zapper
// -----------------------------------------
// Simple animation to simulate an old school bug zapper
// Default color is a purple-ish blue
// Makes use of Piezo Speaker to simulate zapping sound
class Zapper: public Demo {
public:
Zapper() {
initialized = false;
}
~Zapper() {
delete pixels;
}
virtual void loop() {
if(!initialized){
// Initialize these values here as CircuitPlayground obj
// is only initialized after this class is instantiated
numPixels = CircuitPlayground.strip.numPixels();
pixels = new Color[numPixels];
// Read the sound sensor and use it to initialize the random number generator.
randomSeed(CircuitPlayground.soundSensor());
Serial.println("Zapper Demo:");
initialized = true;
}
// Loops happen pretty fast and we want the zaps to occur
// at random times but not too often
int zap = random(1000);
Serial.print("Random Zap value:"); Serial.println(zap);
if (zap < ZAP_FREQUENCY) {
// Found a bug to zap
// Play bug zapping sound and related animation
int zappedBug = random(numPixels); // Single LED to light when zap happens
Serial.print("Zapped Bug:"); Serial.println(zappedBug);
for (int i=0;i<numPixels;i++){
Serial.print("Setting led number:"); Serial.println(i);
if(i == zappedBug) {
pixels[i].setColor(25, 0, 255); // HSV 246, 100, 100
CircuitPlayground.strip.setPixelColor(i, pixels[i].getR(), pixels[i].getG(), pixels[i].getB());
}
else {
pixels[i].setColor(0, 0, 0); // Turn Off Other Pixels
CircuitPlayground.strip.setPixelColor(i, pixels[i].getR(), pixels[i].getG(), pixels[i].getB());
}
}
CircuitPlayground.strip.show();
delay(20);
for (int i=0;i<numPixels;i++){
pixels[i].setColor(255, 255, 255); // White Strobe
CircuitPlayground.strip.setPixelColor(i, pixels[i].getR(), pixels[i].getG(), pixels[i].getB());
}
CircuitPlayground.strip.show();
delay(20);
for (int i=0;i<numPixels;i++){
pixels[i].setColor(0, 0, 0); // Off
CircuitPlayground.strip.setPixelColor(i, pixels[i].getR(), pixels[i].getG(), pixels[i].getB());
}
pixels[zappedBug].setColor(25, 0, 255); // purple where bug is
CircuitPlayground.strip.show();
delay(20);
for (int i=0;i<numPixels;i++){
pixels[i].setColor(255, 255, 255); // White Strobe
CircuitPlayground.strip.setPixelColor(i, pixels[i].getR(), pixels[i].getG(), pixels[i].getB());
}
CircuitPlayground.strip.show();
delay(50);
for (int i=0;i<numPixels;i++){
pixels[i].setColor(0, 0, 0); // Off
CircuitPlayground.strip.setPixelColor(i, pixels[i].getR(), pixels[i].getG(), pixels[i].getB());
}
pixels[zappedBug].setColor(25, 0, 255); // purple where bug is
CircuitPlayground.strip.show();
delay(20);
// Back to steady state color
for (int i=0;i<numPixels;i++){
pixels[i].setColor(25, 0, 255); // HSV 246, 100, 100
CircuitPlayground.strip.setPixelColor(i, pixels[i].getR(), pixels[i].getG(), pixels[i].getB());
}
CircuitPlayground.strip.show();
CircuitPlayground.playTone(70, 500); // Bug Zapping sound
}
else {
// Display the big light in steady state (purple)
for (int i=0;i<numPixels;i++){
pixels[i].setColor(25, 0, 255); // HSV 246, 100, 100
CircuitPlayground.strip.setPixelColor(i, pixels[i].getR(), pixels[i].getG(), pixels[i].getB());
}
CircuitPlayground.strip.show();
}
}
virtual void modePress() {
// No modes defined for this demo
}
private:
boolean initialized;
int numPixels;
Color* pixels;
};
#endif