美文网首页Arduino
基于arduino的全彩WS2812虚拟仿真玩法

基于arduino的全彩WS2812虚拟仿真玩法

作者: 想啥做啥 | 来源:发表于2021-02-27 23:25 被阅读0次

接下来我们再来看下两种仿真环境的对比效果

基于ThinkerCAD的效果
基于proteus的效果

arduino版七彩灯

1、新建一个基于arduino的工程



2、代码复制过来,然后编译



3、仿真原理图中添加WS2812

4、仿真看效果


TinkerCAD版七彩灯

Tinkercad | From mind to design in minutes
1、放置要仿真的模块

2、连接导线


3、稍加整理下导线



4、复制代码仿真


代码:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
 
#define PIN 6
 
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
 
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.
 
void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code
 
 
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
 
void loop() {
 
  //rainbow(20);
  rainbowCycle(20);
  //theaterChaseRainbow(50);
}
 
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}
 
void rainbow(uint8_t wait) {
  uint16_t i, j;
 
  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
 
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;
 
  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
 
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();
 
      delay(wait);
 
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}
 
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();
 
      delay(wait);
 
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}
 
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

总结:

1、pretous针对这种纯数字器件仿真验证想法还是非常有意义的,能在一定程度上节约一些开销
2、Tinkercad目前只有在线版的,访问速度有点慢,提供的模块相对有限,但是学习基本东西完全够用
3、Adafruit NeoPixel参考官方介绍
Adafruit Industries, Unique & fun DIY electronics and kits

相关文章

  • 基于arduino的全彩WS2812虚拟仿真玩法

    接下来我们再来看下两种仿真环境的对比效果 arduino版七彩灯 1、新建一个基于arduino的工程 2、代码复...

  • ElecFun--Arduino_小板_RGB彩灯_ws2812

    这是一款实用ws2812驱动芯片的全彩RGB灯,因为ws2812是通过单线通讯的,所以,我们只需要一个单片机的一个...

  • ws2812 LED灯编程

    对于WS2812编程,arduino集成开发中有一个库Adafruit_NeoPixel可以很方便的让我们进行操作...

  • Arduino学习

    平台: Arduino 仿真: fritzing 在线仿真: 比较好的教程: 极客工坊 新浪博客

  • 【三维虚拟仿真】与【数据模型计算仿真】

    一、三维仿真与数据仿真的差别 【三维虚拟仿真】举例:虚拟工厂,工业仿真.....其实他属于3D可视化仿真(VR),...

  • [OS] 仿真

    仿真技术 许多虚拟机都是基于仿真来实现的。所谓仿真就是在一个具有某种接口和功能的系统或子系统上,实现另一种与之具有...

  • 无标题文章

    2017-10-24 今天我们学习了Arduino仿真开发环境设置及仿真运,serial.begin()串口定义函...

  • 南博教育全新推出仿真实训平台

    2019年1月25日,南博教育全新推出仿真实训平台,基于虚拟仿真、人机交互技术,针对特定学科课件内容进行3D数字内...

  • 思考|如何开发虚拟仿真实验教学项目

    随着虚拟仿真技术不断进步,应用虚拟仿真技术开展实验教学成为实验教学领域的热点话题。大量科技公司纷纷布局虚拟仿真教育...

  • 玩Arduino — 之全彩LED

    上一次玩了单色的LED,每个小灯泡都只能是一种颜色,今天我们来玩一下全彩的LED。 万物皆可RGB— 鲁迅 全彩L...

网友评论

    本文标题:基于arduino的全彩WS2812虚拟仿真玩法

    本文链接:https://www.haomeiwen.com/subject/miqwfltx.html