CurieNano的情感交互灯
本帖最后由 芥末秋刀鱼 于 2017-7-22 14:31 编辑之前申请的curienano,现在终于做完了。这个主要是利用curienano上的加速度传感器和这里传感器来采集灯的倾斜角度和加速度,根据角度的不同变化,来使WS2182发出不同颜色的光。
灯罩是学姐设计的,很漂亮
材料:
curienano*1
ws2812B灯带
电容开关(带灯)
小米充电宝*1
外壳
螺丝若干
http://www.dfrobot.com.cn/goods-1343.html
http://www.dfrobot.com.cn/goods-852.html
充电宝给WS2812和curienano供电,电容开关控制通断,充电宝位于灯的底部,其供电和配重效果,达到不倒翁的效果。
ws2812信号线接D6口
制作步骤:
按照如下接线图接好后,将充电宝固定在底部,作为配重
接线图:
电容开关没找到素材,PS不会画,所以没画
接好线后在上面盖一块硬纸片,然后将灯带粘在上面,最后再将灯体上下连接起来(图上底下的那个PCB是之前老师和学姐做的,我直接放上面了,你们可以用灯带和硬纸片代替)
上效果图:
上视频:
http://v.youku.com/v_show/id_XMjg4Njk4MzA1Ng
http://v.youku.com/v_show/id_XMjg4Njk4NDEyOA
这是第一次写出来的,第二次更改了效果,然后烧着烧着 板就检测不到端口了,等DF技术大佬解决后我再试新的效果发上来。
代码(第二版):
/*
Name: curienanoLED.ino
Created: 2017/6/19 15:37:33
Author:QDY
*/
#include <CurieIMU.h> //传感器库
#include <MadgwickAHRS.h> //Madgwick滤波
#include<Adafruit_NeoPixel.h>//ws2812b库
#define PIN 6//控制引脚
#define NUMPIXELS 9//灯珠数
//静止状态
#ifdef __AVR__
#include <avr/power.h>
#endif
int i = 0;
int br = 127;//亮度
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
Madgwick filter; //初始化传感器
unsigned long microsPerReading, microsPrevious;
float accelScale, gyroScale;
void setup() {
//初始化串口
Serial.begin(9600);
strip.begin();
strip.show();
//初始化设备
CurieIMU.begin();
//设置陀螺仪采样频率
CurieIMU.setGyroRate(25);
//设置加速度计采样频率
CurieIMU.setAccelerometerRate(25);
//设置加速度计范围为2G
CurieIMU.setAccelerometerRange(2);
//设置陀螺仪范围为250度/秒
CurieIMU.setGyroRange(250);
filter.begin(25);
//初始化变量以更新正确率
microsPerReading = 1000000 / 25;
microsPrevious = micros();
//陀螺仪自动校准
CurieIMU.autoCalibrateGyroOffset();
//加速度自动计校准
CurieIMU.autoCalibrateAccelerometerOffset(X_AXIS, 0);
CurieIMU.autoCalibrateAccelerometerOffset(Y_AXIS, 0);
CurieIMU.autoCalibrateAccelerometerOffset(Z_AXIS, 1);
//初始化WS2812B
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
strip.begin();
strip.show();
}
void loop() {
//加速度计和陀螺仪原始数据
int aix, aiy, aiz; //加速度
int gix, giy, giz; //陀螺仪
//转换后的数据
float ax, ay, az;
float gx, gy, gz;
//处理后的数据
int roll, pitch, heading;
unsigned long microsNow;
//检查是否要读取数据并更新过滤器
microsNow = micros();
if (microsNow - microsPrevious >= microsPerReading) {
//得到加速度计和陀螺仪的姿态数据
CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
//从原始数据转换为重力和度/秒单位
ax = convertRawAcceleration(aix);
ay = convertRawAcceleration(aiy);
az = convertRawAcceleration(aiz);
gx = convertRawGyro(gix);
gy = convertRawGyro(giy);
gz = convertRawGyro(giz);
//使用Madgwick库中的功能函数updateIMU()滤波
filter.updateIMU(gx, gy, gz, ax, ay, az);
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
Serial.print("Orientation: ");
Serial.print(heading);
Serial.print(" ");
Serial.print(pitch);
Serial.print(" ");
Serial.println(roll);
//增加以前的时间,所以我们保持适当的速度
microsPrevious = microsPrevious + microsPerReading;
}
delay(30);
Serial.print(" ");
Serial.print(abs(pitch));
Serial.print(" ");
Serial.println(abs(roll));
//初始化灯带
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 255, 255, 255);
strip.show();
}
//LED控制
if (abs(pitch) <= 5 && abs(roll) <= 5) {
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color(0, 255, 0), 50); // Green
colorWipe(strip.Color(0, 0, 255), 50); // Blue
theaterChase(strip.Color(127, 127, 127), 50); // White
theaterChase(strip.Color(127, 0, 0), 50); // Red
theaterChase(strip.Color(0, 0, 127), 50); // Blue
rainbow(20);
rainbowCycle(20);
theaterChaseRainbow(50);
}
else if (abs(pitch) <= 10 && abs(roll) <= 10) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 255, 130, 71);
strip.show();
}
}
else if (abs(pitch) <= 15 && abs(roll) <= 15) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 0, 0, 255);
strip.show();
}
}
else if (abs(pitch) <= 20 && abs(roll) <= 20) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 138, 43, 226);
strip.show();
}
}
else if (abs(pitch) <= 25 && abs(roll) <= 25) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 238, 0, 0);
strip.show();
}
}
else if (abs(pitch) <= 30 && abs(roll) <= 30) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 205, 16, 118);
strip.show();
}
}
else if (abs(pitch) <= 35 && abs(roll) <= 35) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 50, 205, 50);
strip.show();
}
}
else if (abs(pitch) <= 40 && abs(roll) <= 40) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 205, 102, 29);
strip.show();
}
}
else if (abs(pitch) <= 45 && abs(roll) <= 45) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 85, 26, 139);
strip.show();
}
}
}
//加速度计范围
float convertRawAcceleration(int aRaw) {
float a = (aRaw * 2.0) / 32768.0;
return a;
}
//陀螺仪范围
float convertRawGyro(int gRaw) {
float g = (gRaw * 250.0) / 32768.0;
return g;
}
// 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);
}
库文件:
另外两个DF资料库里有
非常感谢DFrobot和arduino中文社区提供的这次试用机会。
程序比较简单,就是一个简单的灯的效果,希望以后可以向社区大佬学习。
好漂亮!设计感十足~
页:
[1]