19609| 7
|
[项目] 用3D手势传感器制作一个蘑菇灯 |
作者:吴磊 今天买了1个3D手势识别迷你传感器,恩,对,就是3D手势识别迷你传感器,测试一下后,发现挺有意思的,想赶紧做一个东西出来看看效果。3D、3D,对就用3D打印机打印个东西,打印什么呢,像动画偏离的大蘑菇,不如做个大蘑菇灯吧。 视频效果 ▲3D打印图 材料清单 连接标注 【传感器】 3D手势传感器 UNO R3控制板 VCC -> 5V GND -> GND SCL -> SCL SDK -> SDK D -> D8 MCLR -> D9 【RGB灯】 WS2812 RGB LED Module VCC -> 5V GND -> GND D -> D7 装配图 模型下载: 程序代码 [mw_shl_code=applescript,true]#include <Adafruit_NeoPixel.h>#include <DFRobot_Gesture.h> #include <Wire.h> #define Lamp 6 //黄灯 #define Dio 8 //数字IO口 #define MCLR 9 //传感器复位(低电平有效) #define PIN 10 //灯IO #define NUMBER 7 //共有5个灯 unsigned char cmd; unsigned long RGBtime = 0, RGBtime1 = 0, RGBtime2 = 0; uint32_t a1 = 0, b1 = 1, c1 = 2, d1 = 3, e1 = 4, a2 = 0, b2 = 1, c2 = 2, d2 = 3, e2 = 4; uint32_t Light = 0;//灯的亮度 unsigned int dstance_1, dstance_2, dstance_3, dstance_4, dstance_5, dstance_6, dstance_7, dstance_8, dstance_9; unsigned int Mark = 1; //1:红色(255,0,0),2:绿色(0, 255, 0),3:蓝色(0, 0, 255),4:黄色(255, 255, 0) //5:蓝绿色(0, 255, 255),6:品红色(255, 0, 255),7:白色(255, 255, 255) DFRobot_Gesture myGesture; Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMBER, PIN, NEO_GRB + NEO_KHZ800); //灯参数 void distance() { dstance_1 = 0; dstance_2 = 0; dstance_3 = 0; dstance_4 = 0; dstance_5 = 0; dstance_6 = 0; dstance_7 = 0; dstance_8 = 0; dstance_9 = 0; } void RGB_0(uint32_t x, uint32_t a, uint32_t b, uint32_t c) //RGB灯1个灯亮一个颜色 { strip.setPixelColor(x, strip.Color( a, b, c) ); //0,指第1个RGB灯,后面的值是R,G,B其值为0-255 strip.show(); } void RGB_1(uint32_t a, uint32_t b, uint32_t c)//RGB灯4个灯亮同一个颜色 { strip.setPixelColor(0, strip.Color( a, b, c) ); //0,指第1个RGB灯,后面的值是R,G,B其值为0-255 strip.setPixelColor(1, strip.Color( a, b, c) ); // strip.setPixelColor(2, strip.Color( a, b, c) ); strip.setPixelColor(3, strip.Color( a, b, c) ); strip.setPixelColor(4, strip.Color( a, b, c) ); strip.setPixelColor(5, strip.Color( a, b, c) ); strip.setPixelColor(6, strip.Color( a, b, c) ); strip.show(); } void RGB_2(uint32_t x, uint32_t y, uint32_t a, uint32_t b, uint32_t c) //RGB灯2个灯亮同一个颜色 { strip.setPixelColor(x, strip.Color( a, b, c) ); //0,指第1个RGB灯,后面的值是R,G,B其值为0-255 strip.setPixelColor(y, strip.Color( a, b, c) ); // strip.show(); } void RGB_3(uint32_t a, uint32_t b, uint32_t c) //3个RGB灯亮红绿蓝3个颜色 { strip.setPixelColor(a, strip.Color( 255, 0, 0) ); //a,指第a+1位上的RGB灯,从0开始;后面的值是R,G,B其值为0-255 strip.setPixelColor(b, strip.Color( 0, 255, 0) ); // strip.setPixelColor(c, strip.Color( 0, 0, 255) ); strip.show(); } void RGB_4(uint32_t a, uint32_t b, uint32_t c, uint32_t d) //4个RGB灯亮红绿蓝白四个颜色 { strip.setPixelColor(a, strip.Color( 255, 0, 0) ); //a,指第a+1位上的RGB灯,从0开始;后面的值是R,G,B其值为0-255 strip.setPixelColor(b, strip.Color( 0, 255, 0) ); // strip.setPixelColor(c, strip.Color( 0, 0, 255) ); strip.setPixelColor(d, strip.Color( 255, 255, 255) ); strip.show(); } void setup() { Wire.begin(); strip.begin(); //启动RGB灯 Serial.begin(115200); // 打开串口,设置速率为115200 bps pinMode(Dio, INPUT); pinMode(MCLR, OUTPUT); pinMode(Lamp,OUTPUT); digitalWrite(MCLR, HIGH); digitalWrite(Lamp,LOW); } void loop() { if (Mark == 0)// Mark = 7; if (Mark == 8)// Mark = 1; delay(50); switch (Mark)//选择亮那种颜色的灯 { case 1: RGB_1(Light, 0, 0); break; case 2: RGB_1(0, Light, 0); break; case 3: RGB_1(0, 0, Light); break; case 4: RGB_1(Light, Light, 0); break; case 5: RGB_1(0, Light, Light); break; case 6: RGB_1(Light, 0, Light); break; case 7: RGB_1(Light, Light, Light); break; default: RGB_1(0, 0, 0); digitalWrite(Lamp,LOW); } if (digitalRead(Dio) == 0)//判断 { myGesture.I2C1_MasterRead(md.buf, 26, 0x42); //The address is:0x42 cmd = myGesture.mgcProcMsg(); //process the message if (md.sensorData.posZ > 0 && md.sensorData.posZ < 8000) //距离 { Light = 255; digitalWrite(Lamp,HIGH); } else if (md.sensorData.posZ >= 8000 && md.sensorData.posZ < 15000) // { Light = 220; digitalWrite(Lamp,HIGH); } else if (md.sensorData.posZ >= 15000 && md.sensorData.posZ < 22000) // { Light = 185; digitalWrite(Lamp,HIGH); } else if (md.sensorData.posZ >= 22000 && md.sensorData.posZ < 29000) { Light = 150; digitalWrite(Lamp,HIGH); } else if (md.sensorData.posZ >= 29000 && md.sensorData.posZ < 36000) { Light = 120; digitalWrite(Lamp,HIGH); } else if (md.sensorData.posZ >= 36000 && md.sensorData.posZ < 44000) { Light = 80; digitalWrite(Lamp,HIGH); } else if (md.sensorData.posZ >= 44000 && md.sensorData.posZ < 52000) { Light = 50; digitalWrite(Lamp,HIGH); } else if (md.sensorData.posZ >= 52000 && md.sensorData.posZ < 60000) { Light = 20; digitalWrite(Lamp,HIGH); } else if (md.sensorData.posZ >= 60000 && md.sensorData.posZ < 65000 ) { Light = 0; digitalWrite(Lamp,LOW); } if (cmd != GI_NOGESTURE ) // { if (cmd == GI_AIRWHEEL_CW) //顺时针转 { Serial.println("CW"); Mark += 1; } else if (cmd == GI_AIRWHEEL_CCW) //逆时针转 { Serial.println("CCW"); Mark -= 1; } } } }[/mw_shl_code] 代码压缩包下载:程序.zip 小伙伴们赶快行动吧,特别是晚上,看着挺美的哦! |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed