Cambridge 发表于 2016-3-20 11:05:06

RGBCLOCK第二版---小太鼓面世啦

本帖最后由 Cambridge 于 2016-3-27 11:05 编辑


有没有觉得很萌呢!

之前做的RGBclock感觉有点简陋哈哈,连完线写个程序跑了下之后就放一边了,这样有点浪费唉,于是就开始做模型打印出来,加上电路组装成一个完整的小太鼓时钟。
而且比第一版多了设置闹钟的功能,并且用processing做了个上位机来设置闹钟。闹钟可以根据星期几来设置,而且利用TF卡模块和SimpleSdAudio库来储存和播放音乐,效果十分好啊。


那么怎么看时间呢?!红色代表的是时针,这个很简单,变色的代表了秒针,有点酷炫哈哈,然后分钟也会慢慢变色,从绿色渐变到橙色,代表了新的一个5分钟到9分钟,例如15分是绿色,19分就是橙色了。
大家可以下载和修改程序,例如把上位机的界面修改了,更改自己喜欢的音乐做铃声,等等。

注意!先要在EEPROM的地址1,2分别写入当前的星期和日才能正常运行!
关于铃声,请看链接http://www.arduino.cn/thread-2944-1-1.html


接线如下:


arduino代码:
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "RTClib.h"
#include <SimpleSDAudio.h>

#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 6

#define BIGBUFSIZE (2*512)      // bigger than 2*512 is often only possible on Arduino megas!
uint8_t bigbuf;

RTC_DS1307 rtc;

//初始化ws2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

String data1 = "";

StringmusicName ;

char charactor;
//记得改音乐的名字,TF卡里面的也要改,短一点比较好。
charmusicName0 = "XIAO.AFM";
charmusicName1 = "FEEL.AFM";
charmusicName2 = "YOUNG.AFM";
charmusicName3 = "WHAT.AFM";
charmusicName4 = "ME.AFM";
charmusicName5 = "RESET.AFM";

int h = 0,m = 0,s = 0,d = 0;
int lastsecond = 0,lastminute = 0,lasthour = 0,scale = 0;
int alarmH1 = 0,alarmM1 = 0;
int alarmH2 = 0,alarmM2 = 0;
int alarmh1 = 0,alarmm1 = 0;
int alarmh2 = 0,alarmm2 = 0;
int music = 0,week = 0,WEEKDAY = 0,MUSIC = 0,LASTDAY = 0;

void setup(){
pinMode(13,OUTPUT);

#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(57600);

#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif

rtc.begin();
if (! rtc.isrunning()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
digitalWrite(13,LOW);

SdPlay.setWorkBuffer(bigbuf, BIGBUFSIZE);
SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER);

//EEPROM里面储存了星期几和对应的闹钟
WEEKDAY = EEPROM.read(1);
LASTDAY = EEPROM.read(2);
MUSIC = EEPROM.read(3);
alarmH1 = EEPROM.read((WEEKDAY-1) * 4 + 4);
alarmM1 = EEPROM.read((WEEKDAY-1) * 4 + 5);
alarmH2 = EEPROM.read((WEEKDAY-1) * 4 + 6);
alarmM2 = EEPROM.read((WEEKDAY-1) * 4 + 7);

//下面这段主要是消除杂音
SdPlay.setFile(musicName0);
delay(1000);
SdPlay.play();
delay(500);
SdPlay.stop();
}
void loop(){

DateTime now = rtc.now();
h = now.hour();
m = now.minute();
s = now.second();
d = now.day();

if(LASTDAY !=d){
    LASTDAY = d;
    WEEKDAY++;
    if(WEEKDAY > 7){
      WEEKDAY = 1;
    }
    EEPROM.write(2,d);
    EEPROM.write(1,WEEKDAY);
    alarmH1 = EEPROM.read((WEEKDAY-1) * 4 + 4);
    alarmM1 = EEPROM.read((WEEKDAY-1) * 4 + 5);
    alarmH2 = EEPROM.read((WEEKDAY-1) * 4 + 6);
    alarmM2 = EEPROM.read((WEEKDAY-1) * 4 + 7);
}
//判断闹钟
Alarm(alarmH1,alarmM1);
Alarm(alarmH2,alarmM2);

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++) {
    //eeprom_write();
    getdata();
    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);//in case hour pixel or minute pixel misses
      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);
}


/*print imformation*/
//Serial.println(h);
// Serial.println(m);
// Serial.println(s);
Serial.println(alarmH1);
Serial.println(alarmM1);
Serial.println(alarmH2);
Serial.println(alarmM2);
Serial.println(WEEKDAY);
Serial.println(MUSIC);
//Serial.println(data);
//strip.clear();
//delay(5000);
}

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);
}

//从上位机获取数据
void getdata(){
if(Serial.available()){
    //clear values   
    data1 = "";
    while(Serial.available()){
      charactor = (char) Serial.read();
      data1 += charactor;
    }
    week = getWeek(data1);
    music = getMusic(data1);
    alarmh1 = getHour1(data1);
    alarmm1 = getMinute1(data1);
    alarmh2 = getHour2(data1);
    alarmm2 = getMinute2(data1);
    EEPROM.write((week - 1) * 4 + 4,alarmh1);
    EEPROM.write((week - 1) * 4 + 5,alarmm1);
    EEPROM.write((week - 1) * 4 + 6,alarmh2);         
    EEPROM.write((week - 1) * 4 + 7,alarmm2);   
    EEPROM.write(3,music);   
   
}
}

/*alarm function*/
void Alarm(int ah,int am){
if(ah == h && am == m && (ah + am) != 0){
    switch(MUSIC){
    case 1:SdPlay.setFile(musicName1);break;
    case 2:SdPlay.setFile(musicName2);break;
    case 3:SdPlay.setFile(musicName3);break;
    case 4:SdPlay.setFile(musicName4);break;
    case 5:SdPlay.setFile(musicName5);break;
}
    SdPlay.worker();
    SdPlay.play();
    uint16_t i, j;
    while(ah == h && am == m){
       DateTime now = rtc.now();
       m=now.minute();   
       for(j=0; j<256; j++) {
         for(i=0; i<strip.numPixels(); i++) {
         strip.setPixelColor(i, Wheel((i+j) & 255));
         }
       strip.show();
       delay(20);
       }
    }
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i,strip.Color(0,0,0));
    }
   
}
SdPlay.stop();
}

int getHour1(String p){
int h = 0,mark = 0,value = 0;
while(p != 'h'){
    h++;
}
while(p != 'm'){
    mark++;
}
for(int i = h+1;i < mark;i++){
    value = 10 * value + (int)p - '0';
}
return value;
}

int getMinute1(String p){
int m = 0,mark = 0,value = 0;
while(p != 'm'){
    m++;
}
while(p != 'H'){
    mark++;
}
for(int i = m+1;i < mark;i++){
    value = 10 * value + (int)p - '0';
}
return value;
}

int getHour2(String p){
int H = 0,mark = 0,value = 0;
while(p != 'H'){
    H++;
}
while(p != 'M'){
    mark++;
}
for(int i = H + 1;i < mark;i++){
    value = 10 * value + (int)p - '0';
}
return value;
}

int getMinute2(String p){
int m = 0,mark = 0,value = 0;
while(p != 'M'){
    m++;
}
while(p != 'w'){
    mark++;
}
for(int i = m+1;i < mark;i++){
    value = 10 * value + (int)p - '0';
}
return value;
}

int getMusic(String p){
int s = 0,value = 0;
while(p != 's'){
    s++;
}
value = (int)p - '0';
return value;
}

int getWeek(String p){
int w = 0,value = 0;
while(p != 'w' ){
    w++;
}
value = (int)p - '0';
return value;
}

processing 代码:
PImage img;

import processing.serial.*;

Serial port;

boolean alarm1hbox = false;
boolean alarm1mbox = false;
boolean alarm1sbox = false;

boolean alarm2hbox = false;
boolean alarm2mbox = false;

boolean overboxconnect = false;
boolean overboxweek = false;

boolean click0 = false;//click for connect
boolean click1 = false;//click for setting week

boolean clickmusic = false;//click for setting music

boolean alarm1click1 = false;//click for setting hour1
boolean alarm1click2 = false;//click for setting minute1
boolean alarm1click3 = false;//click for getting data and sending data

boolean alarm2click1 = false;//click for setting hour2
boolean alarm2click2 = false;//click for setting minute2

String data1 = "";

String arduinoPort;

String [] weekday = new String;
String [] musicName = new String;//change the music name in setup()

int h1 = 0 ,m1 = 0;
int h2 = 0 ,m2 = 0;
int week = 1;
int music = 0;

void setup(){
size(768,576);
smooth();

//change the name into yours
img = loadImage("cat2.jpg");
Serial.list();

weekday = "Monday";
weekday = "Tuesday";
weekday = "Wednesday";
weekday = "Thursday";
weekday = "Friday";
weekday = "Saturday";
weekday = "Sunday";

//change the name into yours
musicName = "";
musicName = "Feel The Light";
musicName = "Lana Del Rey";
musicName = "what for";
musicName = "Breathe Me";
musicName = "Reset";

}

void draw(){
background(255);
picture();

UI();

judge();

Print();

music();

if(alarm1sbox && click0 &&music != 0 && mousePressed ){
    port.write(data1);
}

}

void picture(){
image(img,0,0,768,576);
}

void UI(){
fill(255);
strokeWeight(5);
stroke(0);
strokeJoin(ROUND);
rect(250,10,215,40);//Setting Alarm
rect(50,230,100,40);//TIME
rect(50,380,100,40);//MUSIC
strokeWeight(3);

if(mouseX > 250 && mouseX < 390 && mouseY >100 && mouseY <130){
    stroke(104);
    strokeWeight(5);
    overboxconnect = true;
}else{
    stroke(0);
    strokeWeight(3);
    overboxconnect = false;
}
rect(250,100,140,30);//connect

if(mouseX > 250 && mouseX < 410 && mouseY >180 && mouseY <210){
    stroke(104);
    strokeWeight(5);
    overboxweek = true;
}else{
    stroke(0);
    strokeWeight(3);
    overboxweek = false;
}
rect(250,180,160,30);//WEEK

if(mouseX > 250 && mouseX < 300 && mouseY > 230 && mouseY < 260){
    stroke(104);
    strokeWeight(5);
    alarm1hbox = true;
}else{
    stroke(0);
    strokeWeight(3);
    alarm1hbox = false;
}
rect(250,230,50,30);//hour

if(mouseX > 360 && mouseX < 410 && mouseY > 230 && mouseY < 260){
    stroke(104);
    strokeWeight(5);
    alarm1mbox = true;
}else{
    stroke(0);
    strokeWeight(3);
    alarm1mbox = false;
}
rect(360,230,50,30);//minute

fill(152);

if(mouseX > 500 && mouseX < 550 && mouseY > 230 && mouseY < 260){
    stroke(104);
    strokeWeight(5);
    alarm1sbox = true;
}else{
    stroke(0);
    strokeWeight(3);
    alarm1sbox = false;
}
rect(500,230,50,30);//set

fill(255);

if(mouseX > 250 && mouseX < 300 && mouseY > 280 && mouseY < 310){
    stroke(104);
    strokeWeight(5);
    alarm2hbox = true;
}else{
    stroke(0);
    strokeWeight(3);
    alarm2hbox = false;
}
rect(250,280,50,30);//hour

if(mouseX > 360 && mouseX < 410 && mouseY > 280 && mouseY < 310){
    stroke(104);
    strokeWeight(5);
    alarm2mbox = true;
}else{
    stroke(0);
    strokeWeight(3);
    alarm2mbox = false;
}
rect(360,280,50,30);//minute

fill(152);
/*
if(mouseX > 500 && mouseX < 550 && mouseY > 280 && mouseY < 310){
    stroke(104);
    strokeWeight(5);
    alarm2sbox = true;
}else{
    stroke(0);
    strokeWeight(3);
    alarm2sbox = false;
}
rect(500,280,50,30);//set*/

}

void Print(){
textSize(25);
fill(0);
text("Setting Alarm",280,40);
text("TIME",70,260);
text("MUSIC",60,410);
text("WEEK:",260,205);
text(week,350,205);
text(h1,255,255);
text(":",325,250);
text(m1,365,255);
text("set",508,252);
if(alarm1click3 && click0){
    text("Set An Alarm At" + ":" + h1 + ":" + m1 +"," + weekday,250,460);
    text(h2 + ":" + m2,450,495);
    if( music == 0){
      text("Error!No Music!",250,530);
    }else{
      text("Music:" + musicName,250,530);
    }
}

text(h2,255,305);
text(":",325,300);
text(m2,365,305);


if(click0 == true){
    text("disconnect",255,122);
}else if(click0 == false){
    text("connect",270,122);
}

}

void judge(){

if(overboxweek){
    if(mousePressed){
      week = 1;
      click1 = true;
      alarm1click1 = false;
      alarm1click2 = false;
      alarm1click3 = false;
      alarm2click2 = false;
      alarm2click1 = false;
      clickmusic = false;
    }
}

if(alarm1hbox){
    if(mousePressed){
      h1 = 0;
      alarm1click1 = true;
      alarm1click2 = false;
      alarm1click3 = false;
      alarm2click2 = false;
      alarm2click1 = false;
      click1 = false;
      clickmusic = false;
    }
}

if(alarm1mbox){
    if(mousePressed){
      m1 = 0;
      alarm1click2 = true;
      alarm1click1 = false;
      alarm1click3 = false;
      alarm2click2 = false;
      alarm2click1 = false;
      click1 = false;
      clickmusic = false;
    }
}

if(alarm1sbox){
    if(mousePressed){
      alarm1click1 = false;
      alarm1click2 = false;
      alarm1click3 = true;
      alarm2click2 = false;
      alarm2click1 = false;
      click1 = false;
      clickmusic = false;
    }
}

if(alarm1click3 && click0){
    data1 = "a" + "h" + h1 + "m" + m1 + "H" + h2 + "M" + m2 + "w" + week + "s" + music;
}else{
    data1 = "";
}

if(alarm2hbox){
    if(mousePressed){
      h2 = 0;
      alarm2click1 = true;
      alarm2click2 = false;
      alarm1click3 = false;
      alarm1click1 = false;
      alarm1click2 = false;
      click1 = false;
      clickmusic = false;
    }
}

if(alarm2mbox){
    if(mousePressed){
      m2 = 0;
      alarm2click2 = true;
      alarm2click1 = false;
      alarm1click3 = false;
      alarm1click1 = false;
      alarm1click2 = false;
      click1 = false;
      clickmusic = false;
    }
}

}

void music(){

fill(255);
stroke(0);
strokeWeight(3);

if(clickmusic == false){
    rect(250,380,200,30);
    fill(120);
    rect(450,380,30,30);
    triangle(460,390,470,390,465,400);
    fill(0);
    textSize(25);
    text(musicName,260,405);
}else{
   
    rect(250,380,200,30);
   
    if(mouseX > 250 && mouseX < 450 && mouseY > 410 && mouseY < 440){
      fill(0,0,255);
      if(mousePressed){
      music = 1;
      clickmusic = false;
      }
    }else{
      fill(255);
    }
    rect(250,410,200,30);
   
    if(mouseX > 250 && mouseX < 450 && mouseY > 440 && mouseY < 470){
      fill(0,0,255);
      if(mousePressed){
      music = 2;
      clickmusic = false;
      }
    }else{
      fill(255);
    }
    rect(250,440,200,30);
    if(mouseX > 250 && mouseX < 450 && mouseY > 470 && mouseY < 500){
      fill(0,0,255);
      if(mousePressed){
      music = 3;
      clickmusic = false;
      }
    }else{
      fill(255);
    }
    rect(250,470,200,30);
   
    if(mouseX > 250 && mouseX < 450 && mouseY > 500 && mouseY < 530){
      fill(0,0,255);
      if(mousePressed){
      music = 4;
      clickmusic = false;
      }
    }else{
      fill(255);
    }
    rect(250,500,200,30);
    if(mouseX > 250 && mouseX < 450 && mouseY > 530 && mouseY < 560){
      fill(0,0,255);
      if(mousePressed){
      music = 5;
      clickmusic = false;
      }
    }else{
      fill(255);
    }
    rect(250,530,200,30);
   
    fill(0);
    textSize(25);
    text(musicName,260,435);
    text(musicName,260,465);
    text(musicName,260,495);
    text(musicName,260,525);
    text(musicName,260,555);
    fill(120);
    rect(450,380,30,30);
    triangle(460,400,470,400,465,390);
}

}

void mousePressed(){

if(overboxconnect){
    click0 = !click0;
    if(click0 == true){
      arduinoPort = Serial.list();
      port =new Serial(this,arduinoPort,57600);
    }else{
      port.stop();
    }
}

if(mouseX > 450 && mouseX < 490 && mouseY > 380 && mouseY < 410){
      clickmusic = !clickmusic;
      alarm2click1 = false;
      alarm2click2 = false;
      alarm1click3 = false;
      alarm1click1 = false;
      alarm1click2 = false;
      click1 = false;
      music = 0;
    }
   
   
}

void keyPressed(){
//week
if(click1){
    week = key - '0';
}
if(week > 7 || week < 1){
    week = 1;
}
//hour
if(alarm1click1){
    h1 = 10 * h1 + key - '0';
}
if(h1 >= 24 || h1 < 0){
      h1 = 0;
}
//minute
if(alarm1click2){
    m1 = 10 * m1 + key - '0';
}
if(m1 >= 60 || m1 < 0){
      m1 = 0;
}
//hour
if(alarm2click1){
    h2 = 10 * h2 + key - '0';
}
if(h2 >= 24 || h2 < 0){
      h2 = 0;
}
//minute
if(alarm2click2){
    m2 = 10 * m2 + key - '0';
}
if(m2 >= 60 || m2 < 0){
      m2 = 0;
}
}

打个小广告,需要打印外壳的可以找我啦哈哈哈

沧海笑1122 发表于 2016-3-20 18:45:23

赞一个,太厉害了

Vegetablebird 发表于 2017-7-31 16:58:24

楼主模型有两个是重复的少了腿

Damn_intuition 发表于 2017-8-9 18:50:56

萌萌达!!
真是一个有意思的设计;P;P

redtxd 发表于 2017-8-17 09:51:14

决心仿制。。。。:lol

504835618 发表于 2018-1-14 13:21:09

请问下processing上位机怎么弄,一点概念都没得,楼主普及下

pysz 发表于 2018-3-6 16:38:57

赞一个,太厉害了

yoyojacky 发表于 2018-3-6 17:18:59

赞! 不知道怎么玩儿啊?是不是要敲什么东西?

cityant 发表于 2018-3-10 10:05:35

这个不错:lol
页: [1]
查看完整版本: RGBCLOCK第二版---小太鼓面世啦