ESP01s小玩具里我最喜欢的一个,为啥?别问,帅就完事了。(3/3)

2019-12-13_152206.png

什么是性能的象征? R!G!B! 什么是男人的信仰? R!G!B! 你会选择女人还是RGB? 当然!R!G!B!

演示视频


首先安装WS2812支持库

点击项目>加载库>库管理器>搜索NeoPixel>安装库

2019-12-13_151106.png

因为,示例程序使用的就是这个库。当然你可以直接搜索WS2812,选择一个你觉得顺手的库


示例代码

  1
  2#include <Adafruit_NeoPixel.h>
  3#ifdef __AVR__
  4  #include <avr/power.h>
  5#endif
  6
  7#define PIN 2
  8
  9#define NUM_LEDS 16
 10
 11#define BRIGHTNESS 50
 12
 13Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);
 14
 15byte neopix_gamma[] = {
 16    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
 17    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
 18    1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,
 19    2,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  5,  5,  5,
 20    5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10,
 21   10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
 22   17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
 23   25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
 24   37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
 25   51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
 26   69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
 27   90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
 28  115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
 29  144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
 30  177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
 31  215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
 32
 33
 34void setup() {
 35  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
 36  #if defined (__AVR_ATtiny85__)
 37    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
 38  #endif
 39  // End of trinket special code
 40  strip.setBrightness(BRIGHTNESS);
 41  strip.begin();
 42  strip.show(); // Initialize all pixels to 'off'
 43}
 44
 45void loop() {
 46  // Some example procedures showing how to display to the pixels:
 47  colorWipe(strip.Color(255, 0, 0), 50); // Red
 48  colorWipe(strip.Color(0, 255, 0), 50); // Green
 49  colorWipe(strip.Color(0, 0, 255), 50); // Blue
 50  colorWipe(strip.Color(0, 0, 0, 255), 50); // White
 51
 52  whiteOverRainbow(20,75,5);  
 53
 54  pulseWhite(5); 
 55
 56  // fullWhite();
 57  // delay(2000);
 58
 59  rainbowFade2White(3,3,1);
 60
 61
 62}
 63
 64// Fill the dots one after the other with a color
 65void colorWipe(uint32_t c, uint8_t wait) {
 66  for(uint16_t i=0; i<strip.numPixels(); i++) {
 67    strip.setPixelColor(i, c);
 68    strip.show();
 69    delay(wait);
 70  }
 71} 
 72
 73void pulseWhite(uint8_t wait) {
 74  for(int j = 0; j < 256 ; j++){
 75      for(uint16_t i=0; i<strip.numPixels(); i++) {
 76          strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) );
 77        }
 78        delay(wait);
 79        strip.show();
 80      }
 81
 82  for(int j = 255; j >= 0 ; j--){
 83      for(uint16_t i=0; i<strip.numPixels(); i++) {
 84          strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) );
 85        }
 86        delay(wait);
 87        strip.show();
 88      }
 89}
 90
 91
 92void rainbowFade2White(uint8_t wait, int rainbowLoops, int whiteLoops) {
 93  float fadeMax = 100.0;
 94  int fadeVal = 0;
 95  uint32_t wheelVal;
 96  int redVal, greenVal, blueVal;
 97
 98  for(int k = 0 ; k < rainbowLoops ; k ++){
 99    
100    for(int j=0; j<256; j++) { // 5 cycles of all colors on wheel
101
102      for(int i=0; i< strip.numPixels(); i++) {
103
104        wheelVal = Wheel(((i * 256 / strip.numPixels()) + j) & 255);
105
106        redVal = red(wheelVal) * float(fadeVal/fadeMax);
107        greenVal = green(wheelVal) * float(fadeVal/fadeMax);
108        blueVal = blue(wheelVal) * float(fadeVal/fadeMax);
109
110        strip.setPixelColor( i, strip.Color( redVal, greenVal, blueVal ) );
111
112      }
113
114      //First loop, fade in!
115      if(k == 0 && fadeVal < fadeMax-1) {
116          fadeVal++;
117      }
118
119      //Last loop, fade out!
120      else if(k == rainbowLoops - 1 && j > 255 - fadeMax ){
121          fadeVal--;
122      }
123
124        strip.show();
125        delay(wait);
126    }
127  
128  }
129
130
131
132  delay(500);
133
134
135  for(int k = 0 ; k < whiteLoops ; k ++){
136
137    for(int j = 0; j < 256 ; j++){
138
139        for(uint16_t i=0; i < strip.numPixels(); i++) {
140            strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) );
141          }
142          strip.show();
143        }
144
145        delay(2000);
146    for(int j = 255; j >= 0 ; j--){
147
148        for(uint16_t i=0; i < strip.numPixels(); i++) {
149            strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) );
150          }
151          strip.show();
152        }
153  }
154
155  delay(500);
156
157
158}
159
160void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) {
161  
162  if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;
163
164  int head = whiteLength - 1;
165  int tail = 0;
166
167  int loops = 3;
168  int loopNum = 0;
169
170  static unsigned long lastTime = 0;
171
172
173  while(true){
174    for(int j=0; j<256; j++) {
175      for(uint16_t i=0; i<strip.numPixels(); i++) {
176        if((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){
177          strip.setPixelColor(i, strip.Color(0,0,0, 255 ) );
178        }
179        else{
180          strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
181        }
182        
183      }
184
185      if(millis() - lastTime > whiteSpeed) {
186        head++;
187        tail++;
188        if(head == strip.numPixels()){
189          loopNum++;
190        }
191        lastTime = millis();
192      }
193
194      if(loopNum == loops) return;
195    
196      head%=strip.numPixels();
197      tail%=strip.numPixels();
198        strip.show();
199        delay(wait);
200    }
201  }
202  
203}
204void fullWhite() {
205  
206    for(uint16_t i=0; i<strip.numPixels(); i++) {
207        strip.setPixelColor(i, strip.Color(0,0,0, 255 ) );
208    }
209      strip.show();
210}
211
212
213// Slightly different, this makes the rainbow equally distributed throughout
214void rainbowCycle(uint8_t wait) {
215  uint16_t i, j;
216
217  for(j=0; j<256 * 5; j++) { // 5 cycles of all colors on wheel
218    for(i=0; i< strip.numPixels(); i++) {
219      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
220    }
221    strip.show();
222    delay(wait);
223  }
224}
225
226void rainbow(uint8_t wait) {
227  uint16_t i, j;
228
229  for(j=0; j<256; j++) {
230    for(i=0; i<strip.numPixels(); i++) {
231      strip.setPixelColor(i, Wheel((i+j) & 255));
232    }
233    strip.show();
234    delay(wait);
235  }
236}
237
238// Input a value 0 to 255 to get a color value.
239// The colours are a transition r - g - b - back to r.
240uint32_t Wheel(byte WheelPos) {
241  WheelPos = 255 - WheelPos;
242  if(WheelPos < 85) {
243    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3,0);
244  }
245  if(WheelPos < 170) {
246    WheelPos -= 85;
247    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3,0);
248  }
249  WheelPos -= 170;
250  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0,0);
251}
252
253uint8_t red(uint32_t c) {
254  return (c >> 16);
255}
256uint8_t green(uint32_t c) {
257  return (c >> 8);
258}
259uint8_t blue(uint32_t c) {
260  return (c);
261}

Arduino IDE 编译烧录固件

这里我们不再啰嗦一遍,如何烧录上传固件了,需要的话可以看这里