|
本帖最后由 Cambridge 于 2016-3-5 19:27 编辑
从某宝买来了一个圆形的12位灯带,刚驱动好之后,惊叹于他完美的色彩,这么漂亮的东西,只是驱动一下看看颜色未免太浪费了吧。刚好他是12位的,不正好跟时钟是一样的吗,带着这个想法去睡觉,今天早上翘了节课来完成这个泡妞计划哈哈哈。
先看看几张图片,拍的不太漂亮嘿嘿嘿。
然后就说说效果。最靠近接线的地方就是12点(第0盏灯),渐变色的是秒针,红色的是时针,绿色的是分钟。分针会根据秒针来渐变色,时钟会根据分针来渐变色。当时针分钟重合的时候就会有5分钟的彩虹。
接线比较简单,DS1307的SDA和SCL分别接A4和A5,WS2812的DIN接arduino的D6脚。 - #include <Adafruit_NeoPixel.h>
- #include <Wire.h>
- #include "RTClib.h"
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- #define PIN 6
- RTC_DS1307 rtc;
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);
- int h=0,m=0,s=0;
- int lastsecond=0,lastminute=0,lasthour=0,scale=0;
- void setup(){
-
- #if defined (__AVR_ATtiny85__)
- if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
- #endif
- strip.begin();
- strip.show(); // Initialize all pixels to 'off'
- // Serial.begin(9600);
-
- #ifdef AVR
- Wire.begin();
- #else
- Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
- #endif
- rtc.begin();
-
- }
- void loop(){
- DateTime now = rtc.now();
- h=now.hour();
- m=now.minute();
- s=now.second();
- if(h>=12)h=h-12;
- strip.setBrightness(64);
- if(h!=m/5){
-
- /*hour*/
- if(h!=lasthour){
- strip.setPixelColor(lasthour, strip.Color(0,0,0));//清空上一盏灯
- lasthour=h;
- //if(lasthour==0)lasthour=12;
- }
- strip.setPixelColor(h, strip.Color(150,0,4*m));
- strip.show();
- delay(20);
-
- /*minute*/
- if(m/5!=lastminute){
- strip.setPixelColor(lastminute, strip.Color(0,0,0));//清空上一盏灯
- lastminute=m/5;
- //if(lastminute==0)lastminute=12;
- }
- scale=m/5;
- scale=50*(m-scale*5);
- strip.setPixelColor(m/5, strip.Color(s+scale,150,0));
- strip.show();
- delay(20);
- }
-
- /*second*/
- uint16_t j;
- for(j=0; j<256; j++) {
- DateTime now = rtc.now();
- s=now.second();
- if(s/5!=lastsecond){
- strip.setPixelColor(lastsecond, strip.Color(0,0,0));//清空上一盏灯
- lastsecond=s/5;
- //if(lastsecond==0 )lastsecond=12;
- strip.show();
- if(lastsecond-1==h || lastminute-1==h)hour1(h);
- if(lastsecond-1==m/5)minute1(m/5);
- }
- if(h==m/5){
- if(m/5!=lastminute){
- strip.setPixelColor(lastminute, strip.Color(0,0,0));
- lastminute=m/5;
- //if(lastminute==0)lastminute=12;
- }
- strip.setPixelColor(h, Wheel(((h * 256 / strip.numPixels()) + j) & 255));
- }
- strip.setPixelColor(s/5, Wheel(((s/5 * 256 / strip.numPixels()) + j) & 255));
- strip.show();
- delay(20);
- }
-
- }
- void hour1(uint8_t hh) {
- strip.setPixelColor(hh, strip.Color(150,0,4*m));
- strip.show();
- delay(20);
- }
- void minute1(uint8_t mm) {
- strip.setPixelColor(mm, strip.Color(scale+s,150,0));
- strip.show();
- delay(20);
- }
- 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);
- }
复制代码
-------------------------------------------------------------分割线---------------------------------------------------------------------------------
最新更新,把分散的电路整理到一起了,做成了一个成品的时钟
下面共享stl文件,可以自己3d打印出来,也可以找我打印哈哈哈。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|