|
|

楼主 |
发表于 2014-5-3 16:04:19
|
显示全部楼层
本帖最后由 wasdpkj 于 2014-5-3 16:56 编辑
现在开始研究驱动全彩LED,我这里选择了WS2812模块(TB可以搜到):
WS2812特性:
- 5050高亮LED,内置控制芯片,仅需1个IO口即可控制多个LED
- 芯片内置整形电路,信号畸变不会累计,稳定显示
- 三基色256级亮度调剂,16万色真彩显示效果,扫描频率不低于400Hz/S
- 串行连级接口,能通过一根信号线完成数据的接收与解码
- 刷新速率30帧/秒时,低速连级模式连级数不小于512点
- 数据收发速度最高可达800Kbps
所用到的库:
Adafruit_NeoPixel
https://github.com/adafruit/Adafruit_NeoPixel
程序我是基于IDE自带的StringTointRGB做的修改:
接收端:
- #include <ZigduinoRadio.h>
- #include <Adafruit_NeoPixel.h>
- #define PIN 6
- // 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(60, PIN, NEO_GRB + NEO_KHZ800);
- String inString = ""; // string to hold input
- int currentColor = 0;
- int red = 0, green = 0, blue = 0;
- void setup() {
- ZigduinoRadio.begin(11);
- // Open serial communications and wait for port to open:
- Serial.begin(115200);
- // send an intro:
- Serial.println("\n\nString toInt() RGB:");
- Serial.println();
- strip.begin();
- strip.show(); // Initialize all pixels to 'off'
- }
- void loop() {
- int inChar;
- // Read serial input:
- if (ZigduinoRadio.available() > 0) {
- inChar = ZigduinoRadio.read();
- }
- if (Serial.available() > 0) {
- inChar = Serial.read();
- }
- if (isDigit(inChar)) {
- // convert the incoming byte to a char
- // and add it to the string:
- inString += (char)inChar;
- }
- // if you get a comma, convert to a number,
- // set the appropriate color, and increment
- // the color counter:
- if (inChar == ',') {
- // do something different for each value of currentColor:
- switch (currentColor) {
- case 0: // 0 = red
- red = inString.toInt();
- // clear the string for new input:
- inString = "";
- break;
- case 1: // 1 = green:
- green = inString.toInt();
- // clear the string for new input:
- inString = "";
- break;
- }
- currentColor++;
- }
- // if you get a newline, you know you've got
- // the last color, i.e. blue:
- if (inChar == '\n') {
- blue = inString.toInt();
- // set the levels of the LED.
- // subtract value from 255 because a higher
- colorWipe(strip.Color(red, green, blue), 10);
- ZigduinoRadio.beginTransmission();
- ZigduinoRadio.write("ColorWipe OK \n\r");
- ZigduinoRadio.endTransmission();
- // print the colors:
- Serial.print("Red: ");
- Serial.print(red);
- Serial.print(", Green: ");
- Serial.print(green);
- Serial.print(", Blue: ");
- Serial.println(blue);
- // clear the string for new input:
- inString = "";
- // reset the color counter:
- currentColor = 0;
- }
- }
- // 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);
- }
- }
复制代码
发送端:
- /*
- Run this sketch on two Zigduinos, open the serial monitor at 9600 baud, and type in stuff
- Watch the Rx Zigduino output what you've input into the serial port of the Tx Zigduino
-
- */
- #include <ZigduinoRadio.h>
- void setup()
- {
- ZigduinoRadio.begin(11);
- Serial.begin(115200);
- }
- void loop()
- {
- if (Serial.available())
- {
- ZigduinoRadio.beginTransmission();
- while(Serial.available())
- {
- char c = Serial.read();
- ZigduinoRadio.write(c);
- }
- ZigduinoRadio.endTransmission();
- }
- if (ZigduinoRadio.available())
- {
- while(ZigduinoRadio.available())
- Serial.write(ZigduinoRadio.read());
- }
- }
复制代码
将上面程序分别下载,将WS2812模块连接到接收端的Core RF,连线非常简单:DI-D6、VCC-5V、GND-GND
打开发送端的串口监视器,发送字符”0,255,0“,回车后返回”ColorWipe OK “既是成功:
这时你可以观察到接收端的LED变成绿色:
==================华丽的分割线====================
一个一个的输入字符太麻烦,令人高兴的是,IDE自带的StringTointRGB中还提供了Processing的上位机代码。
我将其打包了出来:
http://pan.baidu.com/s/1ntx60jR
确认发送端串口没被占用,打开上位机。现在你可以通过调色盘来改变LED颜色了!
视频:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|