-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagic-Jam.ino
More file actions
135 lines (103 loc) · 3.31 KB
/
Magic-Jam.ino
File metadata and controls
135 lines (103 loc) · 3.31 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
#include <Adafruit_WS2801.h> // Link to WS2801 library: https://github.com/adafruit/Adafruit-WS2801-Library
int dataPin = 9; // LED strip data
int clockPin = 10; // LED strip clock
byte numPixel = 4; // Number of LEDs in the LED strip < THIS VARIABLE IS NOT DYNAMIC
byte blue = 0;
byte red = 2;
int t = 0;
int thermPin = 0; // Thermistor
int val = 0; // Temperature as 255 scale value
byte TempMax = 30; // Temperature at which the lamp is completely Red
byte TempMin = 15; // Temperature at which the lamp is completely Red
byte Coefficient = 0; // Coefficient used to convert celsius temperature into a 255 scale value
int UpdateInt = 2000; // Interval between temperature checks (in ms)
byte tempCelcius = 0; // Temperature in degrees Celcius
Adafruit_WS2801 strip = Adafruit_WS2801(numPixel, dataPin, clockPin);
//////////////////////////////////
// Initialisation
//////////////////////////////////
void setup() {
Serial.begin(9600);
strip.begin();
Coefficient = TempMax - TempMin;
Coefficient = 255/Coefficient;
effectPoliceFull(3000); // Startup effect, duration in ms in brackets
}
//////////////////////////////////
// Program
//////////////////////////////////
void loop() {
tempCelcius = int(Thermistor(analogRead(thermPin)));
val = tempCelcius * Coefficient; // Convert to 255 scale: 255 is TempMax and 0 is TempMin
Serial.print(tempCelcius);
Serial.print(" degrees c. - ");
if (tempCelcius >= TempMin && tempCelcius <= TempMax) {
if (val > 128) {
setColor(Color(val,255-val,0));
} else {
setColor(Color(val,255-val,128-val));
}
} else if (tempCelcius < TempMin) {
setColor(Color(0,255,128));
} else {
setColor(Color(255,0,0));
}
delay(UpdateInt);
}
//////////////////////////////////
// Gets temperature
//////////////////////////////////
double Thermistor(int RawADC) {
double Temp;
Temp = log(10000.0*((1024.0/RawADC-1)));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}
//////////////////////////////////
// Control LED strip
//////////////////////////////////
void setColor(uint32_t c) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
}
//////////////////////////////////
// Create a 24 bit color value from R,G,B
//////////////////////////////////
uint32_t Color(byte r, byte b, byte g)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
void effectPoliceFull(int i) {
while (i>0) {
strip.setPixelColor(blue,Color(0,0,255));
strip.setPixelColor(red,Color(255,0,0));
strip.show();
if (blue==3){blue = 0;}else{blue++;}
if (red==3){red = 0;}else{red++;}
delay(250); // (1000ms / 8) meaning it does 2 rounds in 1 second with 4 LEDs
i=i-250;
}
}
void effectPoliceLight(int i) {
while (i>0) {
strip.setPixelColor(blue,Color(0,0,255));
strip.setPixelColor((3 - blue),Color(0,0,0));
strip.setPixelColor(red,Color(255,0,0));
strip.setPixelColor((3 - red),Color(0,0,0));
strip.show();
if (blue==3){blue = 0;}else{blue++;}
if (red==3){red = 0;}else{red++;}
delay(250); // (1000ms / 8) meaning it does 2 rounds in 1 second with 4 LEDs
i=i-250;
}
}