找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4241|回复: 4
打印 上一主题 下一主题
收起左侧

16位环形WS2812全彩小灯Proteus仿真(Proteus8.6SP2)

[复制链接]
跳转到指定楼层
楼主

文件源自Proteus自带例程,“ws2812 button cycler”,全彩条,改为全彩环。

单片机源程序如下:
  1. // This is a demonstration on how to use an input device to trigger changes on your neo pixels.
  2. // You should wire a momentary push button to connect from ground to a digital IO pin.  When you
  3. // press the button it will change to a new pixel animation.  Note that you need to press the
  4. // button once to start the first animation!

  5. #include <Adafruit_NeoPixel.h>

  6. #define BUTTON_PIN   2    // Digital IO pin connected to the button.  This will be
  7.                           // driven with a pull-up resistor so the switch should
  8.                           // pull the pin to ground momentarily.  On a high -> low
  9.                           // transition the button press logic will execute.

  10. #define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.

  11. #define PIXEL_COUNT 16

  12. // Parameter 1 = number of pixels in strip,  neopixel stick has 8
  13. // Parameter 2 = pin number (most are valid)
  14. // Parameter 3 = pixel type flags, add together as needed:
  15. //   NEO_RGB     Pixels are wired for RGB bitstream
  16. //   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
  17. //   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
  18. //   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
  19. Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

  20. bool oldState = HIGH;
  21. int showType = 0;

  22. void setup() {
  23.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  24.   strip.begin();
  25.   strip.show(); // Initialize all pixels to 'off'
  26. }

  27. void loop() {
  28.   // Get current button state.
  29.   bool newState = digitalRead(BUTTON_PIN);

  30.   // Check if state changed from high to low (button press).
  31.   if (newState == LOW && oldState == HIGH) {
  32.     // Short delay to debounce button.
  33.     delay(20);
  34.     // Check if button is still low after debounce.
  35.     newState = digitalRead(BUTTON_PIN);
  36.     if (newState == LOW) {
  37.       showType++;
  38.       if (showType > 9)
  39.         showType=0;
  40.       startShow(showType);
  41.     }
  42.   }

  43.   // Set the last button state to the old state.
  44.   oldState = newState;
  45. }

  46. void startShow(int i) {
  47.   switch(i){
  48.     case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
  49.             break;
  50.     case 1: colorWipe(strip.Color(255, 0, 0), 50);  // Red
  51.             break;
  52.     case 2: colorWipe(strip.Color(0, 255, 0), 50);  // Green
  53.             break;
  54.     case 3: colorWipe(strip.Color(0, 0, 255), 50);  // Blue
  55.             break;
  56.     case 4: theaterChase(strip.Color(127, 127, 127), 50); // White
  57.             break;
  58.     case 5: theaterChase(strip.Color(127,   0,   0), 50); // Red
  59.             break;
  60.     case 6: theaterChase(strip.Color(  0,   0, 127), 50); // Blue
  61.             break;
  62.     case 7: rainbow(20);
  63.             break;
  64.     case 8: rainbowCycle(20);
  65.             break;
  66.     case 9: theaterChaseRainbow(50);
  67.             break;
  68.   }
  69. }

  70. // Fill the dots one after the other with a color
  71. void colorWipe(uint32_t c, uint8_t wait) {
  72.   for(uint16_t i=0; i<strip.numPixels(); i++) {
  73.     strip.setPixelColor(i, c);
  74.     strip.show();
  75.     delay(wait);
  76.   }
  77. }

  78. void rainbow(uint8_t wait) {
  79.   uint16_t i, j;

  80.   for(j=0; j<256; j++) {
  81.     for(i=0; i<strip.numPixels(); i++) {
  82.       strip.setPixelColor(i, Wheel((i+j) & 255));
  83.     }
  84.     strip.show();
  85.     delay(wait);
  86.   }
  87. }

  88. // Slightly different, this makes the rainbow equally distributed throughout
  89. void rainbowCycle(uint8_t wait) {
  90.   uint16_t i, j;

  91.   for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  92.     for(i=0; i< strip.numPixels(); i++) {
  93.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  94.     }
  95.     strip.show();
  96.     delay(wait);
  97.   }
  98. }

  99. //Theatre-style crawling lights.
  100. void theaterChase(uint32_t c, uint8_t wait) {
  101.   for (int j=0; j<10; j++) {  //do 10 cycles of chasing
  102.     for (int q=0; q < 3; q++) {
  103.       for (int i=0; i < strip.numPixels(); i=i+3) {
  104.         strip.setPixelColor(i+q, c);    //turn every third pixel on
  105.       }
  106.       strip.show();

  107.       delay(wait);

  108.       for (int i=0; i < strip.numPixels(); i=i+3) {
  109.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  110.       }
  111.     }
  112.   }
  113. }

  114. //Theatre-style crawling lights with rainbow effect
  115. void theaterChaseRainbow(uint8_t wait) {
  116.   for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
  117.     for (int q=0; q < 3; q++) {
  118.       for (int i=0; i < strip.numPixels(); i=i+3) {
  119.         strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
  120.       }
  121.       strip.show();

  122.       delay(wait);

  123.       for (int i=0; i < strip.numPixels(); i=i+3) {
  124.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  125.       }
  126.     }
  127.   }
  128. }

  129. // Input a value 0 to 255 to get a color value.
  130. // The colours are a transition r - g - b - back to r.
  131. uint32_t Wheel(byte WheelPos) {
  132.   WheelPos = 255 - WheelPos;
  133.   if(WheelPos < 85) {
  134.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  135.   }
  136.   if(WheelPos < 170) {
  137.     WheelPos -= 85;
  138.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  139.   }
  140.   WheelPos -= 170;
  141.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  142. }
复制代码

所有资料51hei提供下载:
ws2812 button cycler.zip (62.64 KB, 下载次数: 107)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:149389 发表于 2019-12-18 13:48 | 只看该作者

回复

使用道具 举报

板凳
ID:637324 发表于 2020-4-27 09:42 | 只看该作者
auduino和ws2812也能仿真,楼主好棒
回复

使用道具 举报

地板
ID:578423 发表于 2020-5-7 08:42 | 只看该作者
WS2812B库名是什么?谢谢
回复

使用道具 举报

5#
ID:240034 发表于 2020-5-7 14:25 | 只看该作者
有WS2812B这个元件名称吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表