Advertisement
Guest User

RBG LED PCB Clock

a guest
Jun 23rd, 2014
1,861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. #include <Rtc_Pcf8563.h>
  2. #include <Encoder.h>
  3. #include <Wire.h>
  4. #include <Adafruit_NeoPixel.h>
  5.  
  6. #define HOURPIN 1
  7. #define MINSECPIN 0
  8. #define APIN A7
  9. #define BPIN A6
  10. #define BUTTON A5
  11.  
  12. Encoder encoder(APIN,BPIN);
  13. Rtc_Pcf8563 rtc;
  14. Adafruit_NeoPixel hourStrip = Adafruit_NeoPixel(80, HOURPIN, NEO_GRB + NEO_KHZ800);
  15. Adafruit_NeoPixel minSecStrip = Adafruit_NeoPixel(120, MINSECPIN, NEO_GRB + NEO_KHZ800);
  16.  
  17. boolean buttonReleased = true;
  18. int state = 0; //0=normal, 1=set second, 2=set minute, 3=set hour, 4=set brightness
  19.  
  20. int tic = 0, second = 0, minute = 5, hour = 3;
  21. int minSecColor = 100, hourColor =50, brightness = 50;
  22. long oldPos = 0, newPos = 0, oldTime=0;
  23.  
  24. void setup() {
  25. pinMode(3, INPUT);
  26. hourStrip.begin();
  27. minSecStrip.begin();
  28. hourStrip.setBrightness(brightness);
  29. minSecStrip.setBrightness(brightness);
  30. hourStrip.show(); // Initialize all pixels to 'off'
  31. minSecStrip.show(); // Initialize all pixels to 'off'
  32.  
  33. for(int i=0; i<hourStrip.numPixels(); i++) {
  34. hourStrip.setPixelColor(i, Wheel((i+hourColor) & 255));
  35. }
  36. for(int i=0; i<minSecStrip.numPixels(); i++) {
  37. minSecStrip.setPixelColor(i, Wheel((i+minSecColor) & 255));
  38. }
  39.  
  40. hourStrip.show();
  41. minSecStrip.show();
  42.  
  43. rtc.initClock();
  44. rtc.setDate(14,6,3,0,10);//day, weekday, month, century, year
  45. rtc.setTime(3,5,10); //hr, min, sec
  46. rtc.setSquareWave(130); //sets to 32Hz square wave: format is 0b10000010 - bit 7 enables CLKOUT and bit 1:0 sets the frequency (00=32.768kHz, 01=1024Hz, 10=32Hz, 11=1Hz)
  47.  
  48. //attachInterrupt(3, tick, FALLING); //Attach 32Hz clock signal to our main clock function
  49.  
  50. }
  51.  
  52. void loop() {
  53. if(buttonReleased) {
  54. //Check if button has been newly pressed
  55. if(digitalRead(BUTTON) == LOW) { //User wants to advance to the next state
  56. state++;
  57. buttonReleased = false;
  58. if(state==5) {
  59. state = 0;
  60. rtc.setTime(hour, minute, second); //hr, min, sec
  61. }
  62. }
  63. } else if(digitalRead(BUTTON)==HIGH) {buttonReleased = true; }//Button has been released
  64.  
  65. //State 0: Normal clock stuff
  66. tick(); //Done on 32Hz interrupt
  67. if(state!=0) { //Set all pixels to dark since we're setting something. We put this here so we don't repeat code
  68. for(int i=0; i<minSecStrip.numPixels(); i++) {
  69. minSecStrip.setPixelColor(i, 0);
  70. }
  71. for(int i=0; i<hourStrip.numPixels(); i++) {
  72. hourStrip.setPixelColor(i, 0);
  73. }
  74. }
  75. //State 1: Set Second
  76. if(state==1) {
  77. newPos = encoder.read();
  78. if(newPos!=oldPos) {
  79. second += (newPos-oldPos);
  80. oldPos = newPos;
  81. if(second>59){ second -=60; }
  82. if(second<0){ second +=60; }
  83. }
  84. minSecStrip.setPixelColor(minute*2, 100, 100, 100);
  85. minSecStrip.setPixelColor(minute*2+1, 100, 100, 100);
  86. minSecStrip.setPixelColor(minute*2+2, 100, 100, 100);
  87. minSecStrip.setPixelColor(minute*2+3, 100, 100, 100);
  88. minSecStrip.setPixelColor(second*2, 255, 10, 255);
  89. minSecStrip.setPixelColor(second*2+1, 255, 10, 255);
  90. minSecStrip.setPixelColor(second*2+2, 255, 10, 255);
  91. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12), 200, 200, 200);
  92. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+1, 200, 200, 200);
  93. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+2, 200, 200, 200);
  94. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+3, 200, 200, 200);
  95. }
  96. //State 2: Set Minute
  97. if(state==2) {
  98. newPos = encoder.read();
  99. if(newPos!=oldPos) {
  100. minute += (newPos-oldPos);
  101. oldPos = newPos;
  102. }
  103. if(minute>59){ minute -=60; }
  104. if(minute<0){ minute +=60; }
  105. minSecStrip.setPixelColor(second*2, 100, 100, 100);
  106. minSecStrip.setPixelColor(second*2+1, 100, 100, 100);
  107. minSecStrip.setPixelColor(second*2+2, 100, 100, 100);
  108. minSecStrip.setPixelColor(minute*2, 255, 10, 255);
  109. minSecStrip.setPixelColor(minute*2+1, 255, 10, 255);
  110. minSecStrip.setPixelColor(minute*2+2, 255, 10, 255);
  111. minSecStrip.setPixelColor(minute*2+3, 255, 10, 255);
  112. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12), 200, 200, 200);
  113. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+1, 200, 200, 200);
  114. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+2, 200, 200, 200);
  115. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+3, 200, 200, 200);
  116. }
  117. //State 3: Set Hour
  118. if(state==3) {
  119. newPos = encoder.read();
  120. if(abs(newPos-oldPos)>3) {
  121. hour += (newPos-oldPos)/3;
  122. oldPos = newPos;
  123. }
  124. if(hour>11) { hour -=12; }
  125. if(hour<0) { hour +=12; }
  126.  
  127. minSecStrip.setPixelColor(second*2, 200, 200, 200);
  128. minSecStrip.setPixelColor(second*2+1, 200, 200, 200);
  129. minSecStrip.setPixelColor(second*2+2, 200, 200, 200);
  130. minSecStrip.setPixelColor(minute*2, 200, 200, 200);
  131. minSecStrip.setPixelColor(minute*2+1, 200, 200, 200);
  132. minSecStrip.setPixelColor(minute*2+2, 200, 200, 200);
  133. minSecStrip.setPixelColor(minute*2+3, 200, 200, 200);
  134. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12), 255, 10, 255);
  135. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+1, 255, 10, 255);
  136. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+2, 255, 10, 255);
  137. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+3, 255, 10, 255);
  138. }
  139. //State 4: Set Brightness
  140. if(state==4) {
  141. newPos = encoder.read();
  142. if(newPos!=oldPos) {
  143. brightness -= (newPos-oldPos);
  144. oldPos = newPos;
  145. }
  146. if(brightness>254) { brightness = 254; }
  147. if(brightness<1) { brightness = 1; }
  148. hourStrip.setBrightness(brightness);
  149. minSecStrip.setBrightness(brightness);
  150.  
  151. for(int i=0; i<minSecStrip.numPixels(); i++) {
  152. minSecStrip.setPixelColor(i, 255, 255, 255);
  153. }
  154. for(int i=0; i<hourStrip.numPixels(); i++) {
  155. hourStrip.setPixelColor(i, 255, 255, 255);
  156. }
  157. }
  158. hourStrip.show();
  159. minSecStrip.show();
  160. }
  161.  
  162. void tick() {//Do clock stuff at 32Hz; For some reason it actually does this at 64Hz, I'm not sure why...
  163. if(state==0) {
  164. //Advance 32Hz tick (actually 64Hz for some reason) and do second/minute/hour advances
  165. while(digitalRead(3)==LOW) {} //wait for rising edge of clock
  166. tic++;
  167. if(tic==32){
  168.  
  169. //if((millis()-oldTime)>=472) { //Substitue timer for RTC since it isn't working yet
  170. // oldTime = millis();
  171. tic = 0;
  172. second--;
  173. if(second==-1) {
  174. second = 119;
  175. minute--;
  176. if(minute==-1) {
  177. minute = 59;
  178. hour--;
  179. if(hour==-1){ hour = 11;}
  180. }
  181. }
  182. }
  183. for(int i=0; i<minSecStrip.numPixels(); i++) {
  184. minSecStrip.setPixelColor(i, Wheel((i+i+minSecColor) & 255));
  185. }
  186. minSecStrip.setPixelColor(second, 0, 0, 0);
  187. minSecStrip.setPixelColor(second+1, 128, 128, 128);
  188. minSecStrip.setPixelColor(second+2, 0, 0, 0);
  189. minSecStrip.setPixelColor(minute*2, 0, 0, 0);
  190. minSecStrip.setPixelColor(minute*2+1, 255, 255, 255);
  191. minSecStrip.setPixelColor(minute*2+2, 255, 255, 255);
  192. minSecStrip.setPixelColor(minute*2+3, 0, 0, 0);
  193. minSecColor++;
  194. if(minSecColor>255) minSecColor = 0;
  195. minSecStrip.show();
  196.  
  197. for(int i=0; i<hourStrip.numPixels(); i++) {
  198. hourStrip.setPixelColor(i, Wheel((hourColor) & 255));
  199. }
  200. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12), 0, 0, 0);
  201. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+1, 200, 200, 200);
  202. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+2, 200, 200, 200);
  203. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+3, 200, 200, 200);
  204. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+4, 200, 200, 200);
  205. hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+5, 0, 0, 0);
  206. hourColor--;
  207. if(hourColor==0) hourColor = 256;
  208. hourStrip.show();
  209. }
  210. }
  211.  
  212.  
  213. // Input a value 0 to 255 to get a color value.
  214. // The colours are a transition r - g - b - back to r.
  215. uint32_t Wheel(byte WheelPos) {
  216. if(WheelPos < 85) {
  217. return hourStrip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  218. } else if(WheelPos < 170) {
  219. WheelPos -= 85;
  220. return hourStrip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  221. } else {
  222. WheelPos -= 170;
  223. return hourStrip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement