-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fire.ino
172 lines (145 loc) · 3.36 KB
/
Fire.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
/**
* Arduino Uno - NeoPixel Fire
* v. 1.0
* Copyright (C) 2015 Robert Ulbricht
*/
#include <Adafruit_NeoPixel.h>
#define PIN 2 // data pin
#define CNT 7 // led count
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(CNT, PIN, NEO_GRBW + NEO_KHZ800);
uint32_t fire_color = strip.Color ( 80/4, 35/4, 00);
uint32_t off_color = strip.Color ( 0, 0, 0);
uint32_t white_color = strip.Color ( 255, 255, 255 );
///
/// Fire simulator
///
class NeoFire
{
Adafruit_NeoPixel &strip;
public:
NeoFire(Adafruit_NeoPixel&);
void Draw();
void Clear();
void AddColor(uint8_t position, uint32_t color);
void SubstractColor(uint8_t position, uint32_t color);
uint32_t Blend(uint32_t color1, uint32_t color2);
uint32_t Substract(uint32_t color1, uint32_t color2);
};
///
/// Constructor
///
NeoFire::NeoFire(Adafruit_NeoPixel& n_strip)
: strip (n_strip)
{
}
///
/// Set all colors
///
void NeoFire::Draw()
{
Clear();
for(int i=0;i<CNT;i++)
{
AddColor(i, fire_color);
int r = random(80);
uint32_t diff_color = strip.Color ( r, r/2, r/2);
SubstractColor(i, diff_color);
}
strip.show();
}
///
/// Set color of LED
///
void NeoFire::AddColor(uint8_t position, uint32_t color)
{
uint32_t blended_color = Blend(strip.getPixelColor(position), color);
strip.setPixelColor(position, blended_color);
}
///
/// Set color of LED
///
void NeoFire::SubstractColor(uint8_t position, uint32_t color)
{
uint32_t blended_color = Substract(strip.getPixelColor(position), color);
strip.setPixelColor(position, blended_color);
}
///
/// Color blending
///
uint32_t NeoFire::Blend(uint32_t color1, uint32_t color2)
{
uint8_t r1,g1,b1;
uint8_t r2,g2,b2;
r1 = (uint8_t)(color1 >> 16),
g1 = (uint8_t)(color1 >> 8),
b1 = (uint8_t)(color1 >> 0);
r2 = (uint8_t)(color2 >> 16),
g2 = (uint8_t)(color2 >> 8),
b2 = (uint8_t)(color2 >> 0);
return strip.Color(constrain(r1+r2, 0, 255), constrain(g1+g2, 0, 255), constrain(b1+b2, 0, 255));
}
///
/// Color blending
///
uint32_t NeoFire::Substract(uint32_t color1, uint32_t color2)
{
uint8_t r1,g1,b1;
uint8_t r2,g2,b2;
int16_t r,g,b;
r1 = (uint8_t)(color1 >> 16),
g1 = (uint8_t)(color1 >> 8),
b1 = (uint8_t)(color1 >> 0);
r2 = (uint8_t)(color2 >> 16),
g2 = (uint8_t)(color2 >> 8),
b2 = (uint8_t)(color2 >> 0);
r=(int16_t)r1-(int16_t)r2;
g=(int16_t)g1-(int16_t)g2;
b=(int16_t)b1-(int16_t)b2;
if(r<0) r=0;
if(g<0) g=0;
if(b<0) b=0;
return strip.Color(r, g, b);
}
///
/// Every LED to black
///
void NeoFire::Clear()
{
for(uint16_t i=0; i<strip.numPixels (); i++)
strip.setPixelColor(i, off_color);
}
NeoFire fire(strip);
void burn() {
fire.Draw();
delay(random(50,150)/1.75);
}
void intensity(int i){
strip.setBrightness(i);
}
void maxIntensity() {
for (int x = 60; x < 255; x = x + 10 ) {
strip.setBrightness(x);
burn();
}
}
void rampDownFire() {
for (int x = 255; x > 0; x-- ) {
strip.setBrightness(x);
delay(random(50,150)/1.75);
burn();
}
strip.setBrightness(0);
}
void cleanLEDs(){
strip.begin();
strip.clear();
strip.show();
strip.setBrightness(0);
}