查看“彩灯的多种玩法”的源代码
←
彩灯的多种玩法
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
{| style="width: 800px;" |- | <br> <big>此示例给出了控制彩灯的几种显示效果。 '''呼吸、流水、闪烁、彩虹、炫彩'''</big> <br> <br> <p style="color: #E87E05;font-size:135%">硬件清单</p> *[[mCookie-Battery]] *[[mCookie-Core]] *[[mCookie-Hub/zh|mCookie-Hub]] *[[Sensor-Color_LED/zh]] [[File:Sensor_ColorLED_setPixelsColor.jpg|600px|left]] <br> <br> <p style="color: #E87E05;font-size:135%">电路搭建</p> 将彩灯的<big>IN</big>接到Hub的6/7引脚,实际使用的是6号引脚。 <br> 将Battery、Core、Hub堆叠在一起,通过MicroUSB数据线接入电脑。 [[File:.jpg|600px|left]] <br> <br> <p style="color: #E87E05;font-size:135%">代码</p> *<big>呼吸灯</big> <source lang="cpp"> #include <Microduino_ColorLED.h> //引用彩灯库 #define PIN 6 //彩灯引脚 #define NUMPIXELS 16 //级联彩灯数量 #define val_max 255 #define val_min 0 ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号 void setup() { strip.begin(); //彩灯初始化 strip.show(); } void loop() { rainbowCycle( 255, 0, 0, 5);//红色呼吸 rainbowCycle( 0, 255, 0, 5);//绿色呼吸 rainbowCycle( 0, 0, 255, 5);//蓝色呼吸 } void colorSet(uint32_t c) { for (uint16_t i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); } strip.show(); } void rainbowCycle( int r, int g, int b, uint8_t wait) { for (int val = 0; val < 255; val++) { colorSet(strip.Color(map(val, val_min, val_max, 0, r), map(val, val_min, val_max, 0, g), map(val, val_min, val_max, 0, b))); delay(wait); } for (int val = 255; val >= 0; val--) { colorSet(strip.Color(map(val, val_min, val_max, 0, r), map(val, val_min, val_max, 0, g), map(val, val_min, val_max, 0, b))); delay(wait); } } </source> <br> <br> *<big>流水灯</big> <source lang="cpp"> #include <Microduino_ColorLED.h> //引用彩灯库 #define PIN 6 //彩灯引脚 #define NUMPIXELS 7 //级联彩灯数量 #define val_max 255 #define val_min 0 ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号 void setup() { strip.begin(); //彩灯初始化 strip.show(); } /*流水灯效果*/ void loop() { theaterChase(COLOR_RED, 2, 450); //红色流水灯效果,循环2次,两灯之间延时450ms theaterChase(COLOR_BLUE, 4, 400); //蓝色流水灯效果,循环4次,两灯之间延时400ms } //Theatre-style crawling lights. void theaterChase(uint32_t c, uint8_t n, uint8_t wait) { for (int j = 0; j < n; j++) { //do 10 cycles of chasing for (uint16_t i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); //turn every third pixel on strip.show(); delay(wait); strip.setPixelColor(i, 0); //turn every third pixel on strip.show(); } } } </source> <br> <br> *<big>闪烁灯</big> <source lang="cpp"> #include <Microduino_ColorLED.h> //引用彩灯库 #define PIN 6 //彩灯引脚 #define NUMPIXELS 7 //级联彩灯数量 #define val_max 255 #define val_min 0 ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号 void setup() { strip.begin(); //彩灯初始化 strip.show(); } void loop() { ledBlinkALL(COLOR_BLUE, 1000); } //第n号灯,颜色c,闪烁时间wait void ledBlink(uint32_t c, uint8_t n, uint8_t wait) { strip.setPixelColor(n, c); //turn every third pixel on strip.show(); delay(wait); strip.setPixelColor(n, 0); //turn every third pixel on strip.show(); delay(wait); } //全部灯,颜色c,闪烁时间wait void ledBlinkALL(uint32_t c, uint8_t wait) { for (uint16_t i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); } delay(wait); for (uint16_t i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, 0); strip.show(); } delay(wait); } </source> <br> <br> *<big>彩虹灯</big> <source lang="cpp"> #include <Microduino_ColorLED.h> //引用彩灯库 #define PIN 6 //彩灯引脚 #define NUMPIXELS 7 //级联彩灯数量 #define val_max 255 #define val_min 0 ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号 void setup() { strip.begin(); //彩灯初始化 strip.show(); } /*彩虹灯效*/ void loop() { rainbow(20); } 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); } } // 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); } </source> <br> <br> *<big>炫彩灯</big> <source lang="cpp"> #include <Microduino_ColorLED.h> //引用彩灯库 #define PIN 6 //彩灯引脚 #define NUMPIXELS 7 //级联彩灯数量 #define val_max 255 #define val_min 0 ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号 void setup() { strip.begin(); //彩灯初始化 strip.show(); } /*炫彩灯效*/ void loop() { rainbowCycle(20); } // 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); } } // 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); } </source> <br> <p style="color: #E87E05;font-size:135%">相关案例</p> [[Sensor-Color_LED_Reference|返回Sensor-ColorLED_Reference界面]] [[Sensor-Color_LED/zh | 返回Sensor-Color_LED界面]]
返回至
彩灯的多种玩法
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
Welcome
首页
创客大赛
大赛详情
3D打印
安装月球车
图形化编程
操控月球车
升级月球车
编程工具下载
软件下载
Arduino
Processing
Mixly
Scratch
模块套件
Microduino 102
mCookie 102
mCookie 202
mCookie 302
IBC
其他
应用套件
四轴飞行器
平衡车
小车CUBE
音乐播放器
刷卡音乐播放器
wifi气象站
彩虹音乐触摸灯
分贝检测仪
迎门汇报
LED点阵时钟
LED点阵屏幕
硬件
mCookie
Sensor
Microduino
MicroWrt
MicroNux
MicroRobot-Core
MicroRobot-CoreESP
ideaBoard
ideaBox
MicroMV
MicroAI
帮助
常见问题
帮助
工具
链入页面
相关更改
特殊页面
页面信息