Arduino Meter Clock

本帖最後由 jamesting 於 2015-12-16 12:14 編輯

http://www.instructables.com/id/ ... o-Powered/?ALLSTEPS

早前無意中睇到呢個,心血來潮馬上做返一個

Arduino nano + DS1307 RTC
IMG_20151029_000630.jpg
2015-10-30 00:11

用塊細萬用板就夠晒
IMG_20151029_201702.jpg
2015-10-30 00:11



IMG_20151029_201708.jpg
2015-10-30 00:11


DC 0- 5V 錶頭係萬惡既X寶買返黎,後來發現拆左入面粒電阻,改做 VR 會方便好多
IMG_20151029_201650.jpg
2015-10-30 00:11



木板都係响萬惡既X寶訂返黎,交張 CAD 圖比賣家,連激光切割只係60 人仔(一共做左三套)
IMG_20151029_201444.jpg
2015-10-30 00:11



開始砌模型,第一次出CAD圖訂木,唔記得計切割損耗,所以啲位有小小唔 FIT ,最後要出動 AA 膠
IMG_20151029_203402.jpg
2015-10-30 00:11


IMG_20151029_203416.jpg
2015-10-30 00:11



完成
IMG_20151029_203822.jpg
2015-10-30 00:11



IMG_20151029_203828.jpg
2015-10-30 00:11


去片
https://youtu.be/dzvClpV1gh8


經我修改既 Code,本身唔係好熟寫詩,有問題請多多指教
  1. /*
  2. Analog Panel Meter - Clock ONLY Mode

  3. Instructables Link: http://www.instructables.com/id/Analog-VU-meter-and-Clock-Arduino-Powered/

  4. Credits:
  5.   bildr - DS1307 RTC code http://bildr.org/2011/03/ds1307-arduino/
  6. _____________________
  7. Wiring goes as follows:

  8. - USB red wire (5v) to VCC
  9. - USB black wire to GND

  10. - Buttons - Left/Down to Digital 2  (follow arduino tutorial: http://arduino.cc/en/uploads/Tutorial/button.png )
  11.            - Right/Up to Digital 4

  12. - DS1307 RTC  - SDA to Analog 4
  13.                - SCL to Analog 5

  14. - Second Analog Meter to Digital 3 (PWM)
  15. - Minute Analog Meter to Digital 6 (PWM)
  16. - Hour Analog Meter to Digital 9 (PWM)
  17. _______________________

  18. Note:
  19.   Because all meters are slightly different, the PWM values
  20.   need to be adjusted based on your design. Feel free to use
  21.   my code, but you will have to change some values because
  22.   it is set to work specifically with my meters.

  23. */

  24. #include "Wire.h"
  25. #define DS1307_ADDRESS 0x68
  26. byte zero = 0x00; //workaround for issue #527

  27. int downButton = 2;    // down push button to adjust time (pin 2)
  28. int upButton = 4;      // up push button to adjust time (pin 4)

  29. int hours = 0;
  30. int minutes = 0;
  31. int seconds = 0;
  32. int lastMinute = 0;
  33. int lastsecond = 0;
  34. int hoursMeter = 9;       // left meter (hours) is attached to pin 9
  35. int minutesMeter = 6;     // right meter (min) is attached to pin 6
  36. int secondsMeter = 3;     // right meter (Sec) is attached to pin 3

  37. void setup(){
  38.   Wire.begin();
  39.   Serial.begin(9600); //view time in serial monitor
  40.   
  41.   pinMode(hoursMeter, OUTPUT);      // Initialize Outputs & Inputs
  42.   pinMode(minutesMeter, OUTPUT);
  43.   pinMode(secondsMeter, OUTPUT);

  44. //test meter
  45.   digitalWrite(13, HIGH);
  46.   // fade in from min to max in increments of 5 points:
  47.   for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 1) {
  48.     // sets the value (range from 0 to 255):
  49.     analogWrite(hoursMeter, fadeValue);   
  50.     analogWrite(minutesMeter, fadeValue);
  51.     analogWrite(secondsMeter, fadeValue);
  52.     // wait for 30 milliseconds to see the dimming effect
  53.     delay(30);
  54.   }
  55.   delay(100);
  56.   // fade out from max to min in increments of 5 points:
  57.   for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 1) {
  58.     // sets the value (range from 0 to 255):
  59.     analogWrite(hoursMeter, fadeValue);   
  60.     analogWrite(minutesMeter, fadeValue);
  61.     analogWrite(secondsMeter, fadeValue);
  62.     // wait for 30 milliseconds to see the dimming effect
  63.     delay(30);
  64.   }

  65.   digitalWrite(13, LOW);
  66.   delay(100);
  67. //test meter

  68. }

  69. void loop(){
  70.   printDate();
  71.   secondLED();
  72.   delay(50);
  73.   
  74. // Changing the time with buttons
  75. if (digitalRead(downButton) == HIGH){
  76. byte second = 0;
  77. byte minute = minutes; // Don't get confused: minute is not the same as minutes
  78. byte hour = hours;     // Don't get confused: hour is not the same as hours
  79. if (minutes > 0){
  80. --minute;
  81. }else{
  82.   minute = 59;
  83. if (hours > 0){
  84.   -- hour;
  85. }else{
  86.    hour = 23;
  87. }}
  88. Wire.beginTransmission(DS1307_ADDRESS);
  89. Wire.write(zero); //stop Oscillator
  90.   
  91.   Wire.write(decToBcd(second));
  92.   Wire.write(decToBcd(minute));
  93.   Wire.write(decToBcd(hour));
  94.   delay(75);
  95.   Wire.write(zero); //start
  96.   
  97.   Wire.endTransmission();
  98. }

  99. if (digitalRead(upButton) == HIGH){
  100. byte second = 0;
  101. byte minute = minutes; // Don't get confused: minute is not the same as minutes
  102. byte hour = hours;     // Don't get confused: hour is not the same as hours
  103. if (minutes < 59){
  104. ++minute;
  105. }else{
  106.   minute = 0;
  107. if (hours < 23){
  108.   ++ hour;
  109. }else{
  110.    hour = 0;
  111. }}
  112. Wire.beginTransmission(DS1307_ADDRESS);
  113. Wire.write(zero); //stop Oscillator
  114.   
  115.   Wire.write(decToBcd(second));
  116.   Wire.write(decToBcd(minute));
  117.   Wire.write(decToBcd(hour));
  118. delay(75);
  119.   Wire.write(zero); //start
  120.   
  121.   Wire.endTransmission();
  122. }
  123.   }

  124. byte decToBcd(byte val){
  125. // Convert normal decimal numbers to binary coded decimal
  126.   return ( (val/10*16) + (val%10) );
  127. }

  128. byte bcdToDec(byte val)  {
  129. // Convert binary coded decimal to normal decimal numbers
  130.   return ( (val/16*10) + (val%16) );
  131. }

  132. void printDate(){

  133.   // Reset the register pointer
  134.   Wire.beginTransmission(DS1307_ADDRESS);
  135.   Wire.write(zero);
  136.   Wire.endTransmission();

  137.   Wire.requestFrom(DS1307_ADDRESS, 7);
  138.   
  139.   seconds = bcdToDec(Wire.read());
  140.   minutes = bcdToDec(Wire.read());
  141.   hours = bcdToDec(Wire.read() & 0b111111); //24 hour time

  142.   //print the date
  143.   Serial.print(hours);     // Un-comment to view time in serial monitor  
  144.   Serial.print(minutes);       //
  145.   Serial.println(seconds); // Don't forget to un-comment 'Serial.begin' in 'void setup' above

  146. // Specific meter levels for each hour
  147. // You will likly have to adjust these values.
  148. // Play around with the values until you get what you want.

  149. if (lastMinute != minutes){
  150.    if (hours == 1 || hours == 13){
  151.      analogWrite(hoursMeter, 21); // adjust left (hour) meter level
  152.       }
  153.     if (hours == 2 || hours == 14){
  154.      analogWrite(hoursMeter, 43); // adjust left (hour) meter level
  155.       }
  156.       if (hours == 3 || hours == 15){
  157.      analogWrite(hoursMeter, 65); // adjust left (hour) meter level
  158.       }
  159.        if (hours == 4 || hours == 16){
  160.      analogWrite(hoursMeter, 87); // adjust left (hour) meter level
  161.       }
  162.       if (hours == 5 || hours == 17){
  163.      analogWrite(hoursMeter, 108); // adjust left (hour) meter level
  164.       }
  165.        if (hours == 6 || hours == 18){
  166.      analogWrite(hoursMeter, 130); // adjust left (hour) meter level
  167.       }
  168.       if (hours == 7 || hours == 19){
  169.      analogWrite(hoursMeter, 152); // adjust left (hour) meter level
  170.       }
  171.       if (hours == 8 || hours == 20){
  172.      analogWrite(hoursMeter, 172); // adjust left (hour) meter level
  173.       }
  174.       if (hours == 9 || hours == 21){
  175.      analogWrite(hoursMeter, 192); // adjust left (hour) meter level
  176.       }
  177.        if (hours == 10 || hours == 22){
  178.      analogWrite(hoursMeter, 214); // adjust left (hour) meter level
  179.       }
  180.       if (hours == 11 || hours == 23){
  181.      analogWrite(hoursMeter, 234); // adjust left (hour) meter level
  182.       }
  183.        if (hours == 12){
  184.      analogWrite(hoursMeter, 254); // adjust left (hour) meter level
  185.       }
  186.        if (hours == 0){
  187.      analogWrite(hoursMeter, 0); // adjust left (hour) meter level
  188.       }
  189.       if (hours <= 18){
  190.        digitalWrite(12, HIGH);
  191.       }
  192.       if (hours >= 6){
  193.        digitalWrite(12, LOW);
  194.       }      

  195. if (minutes * 4.25 < 250){                  // increase PWM value by 4.75 each minute
  196.   analogWrite(minutesMeter, minutes * 4.25);   // I had to use 4.75 on my meter to get 30 minutes to point exactly straight up
  197. }else{                                      // perfect would be 4.25 because 4.25 * 60 = 255
  198.    analogWrite(minutesMeter, 250);
  199. }
  200.    lastMinute = minutes;
  201. }
  202.   if (seconds * 4.25 < 250){                  // increase PWM value by 4.75 each minute
  203.   analogWrite(secondsMeter, seconds * 4.25);   // I had to use 4.75 on my meter to get 30 minutes to point exactly straight up
  204. }else{                                      // perfect would be 4.25 because 4.25 * 60 = 255
  205.    analogWrite(secondsMeter, 250);
  206. }

  207. //   analogWrite(secondsMeter, seconds * 4);   // I had to use 4.75 on my meter to get 30 minutes to point exactly straight up
  208. }

  209. void secondLED(){
  210.   if (seconds > lastsecond){
  211.   FlashLED();
  212. }
  213.   lastsecond = seconds;
  214. }

  215. void FlashLED(){
  216.   digitalWrite(13, HIGH);
  217.   delay(5);
  218.   digitalWrite(13, LOW);
  219. }
複製代碼

好有趣
"H" "M" "S" 個背板係自己整嗎?

TOP

勁!
不過顆電可以用幾耐?

TOP

回覆 2# peter95811658

呢個係 DC 0-5V 錶頭
我一次淘左9個返黎
因為 0-5V 錶頭背板係分左 25 格,唔岩時間既 0-12 同 0-60
所以我就買多左一個分 30格既 DC 0-30V 錶頭返黎
SCAN 左個背板,再係電腦改返做 0-12 同 0-60

TOP

回覆 3# 燕飛

電池係比 DS1307 RTC IC 記憶用
依家暫時用住 USB 比電,計過電流用 50mA 左右,遲下再諗下用唔用電池比電

TOP

回覆  peter95811658

呢個係 DC 0-5V 錶頭
我一次淘左9個返黎
因為 0-5V 錶頭背板係分左 25 格,唔岩時間 ...
jamesting 發表於 2015-10-30 01:13


CHING有心機

TOP

正呀!好工藝!

TOP

中間那段 hour PWM adjust, 用查表向法會否更簡潔及易維護些

在起始定義 adjMeterLevel 陣列
  1. int adjMeterLevel[] = {   0, 21, 43, 65, 87, 108, 130, 152, 172, 192, 214, 234,
  2.                         254, 21, 43, 65, 87, 108, 130, 152, 172, 192, 214, 234 };
複製代碼
在文內引用
  1. if (lastMinute != minutes){
  2.     analogWrite(hoursMeter, adjMeterLevel[hours]);
  3.     ....
  4. }
複製代碼

TOP

回覆 8# Charcoal99

其實唔係我寫,我只係加左個秒錶落去

原作者咁寫,估計係方便其他人改
因為入面提到
  1. Note:
  2.   Because all meters are slightly different, the PWM values
  3.   need to be adjusted based on your design. Feel free to use
  4.   my code, but you will have to change some values because
  5.   it is set to work specifically with my meters.
複製代碼
  1. // Specific meter levels for each hour
  2. // You will likly have to adjust these values.
  3. // Play around with the values until you get what you want.
複製代碼

TOP

建議加一個溫度錶,一個濕度錶,再放一個上下倒轉錶扮鐘擺!啱啱用晒6支PWM pins!

TOP