Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Rtc_Pcf8563.h>
- #include <Encoder.h>
- #include <Wire.h>
- #include <Adafruit_NeoPixel.h>
- #define HOURPIN 1
- #define MINSECPIN 0
- #define APIN A7
- #define BPIN A6
- #define BUTTON A5
- Encoder encoder(APIN,BPIN);
- Rtc_Pcf8563 rtc;
- Adafruit_NeoPixel hourStrip = Adafruit_NeoPixel(80, HOURPIN, NEO_GRB + NEO_KHZ800);
- Adafruit_NeoPixel minSecStrip = Adafruit_NeoPixel(120, MINSECPIN, NEO_GRB + NEO_KHZ800);
- boolean buttonReleased = true;
- int state = 0; //0=normal, 1=set second, 2=set minute, 3=set hour, 4=set brightness
- int tic = 0, second = 0, minute = 5, hour = 3;
- int minSecColor = 100, hourColor =50, brightness = 50;
- long oldPos = 0, newPos = 0, oldTime=0;
- void setup() {
- pinMode(3, INPUT);
- hourStrip.begin();
- minSecStrip.begin();
- hourStrip.setBrightness(brightness);
- minSecStrip.setBrightness(brightness);
- hourStrip.show(); // Initialize all pixels to 'off'
- minSecStrip.show(); // Initialize all pixels to 'off'
- for(int i=0; i<hourStrip.numPixels(); i++) {
- hourStrip.setPixelColor(i, Wheel((i+hourColor) & 255));
- }
- for(int i=0; i<minSecStrip.numPixels(); i++) {
- minSecStrip.setPixelColor(i, Wheel((i+minSecColor) & 255));
- }
- hourStrip.show();
- minSecStrip.show();
- rtc.initClock();
- rtc.setDate(14,6,3,0,10);//day, weekday, month, century, year
- rtc.setTime(3,5,10); //hr, min, sec
- 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)
- //attachInterrupt(3, tick, FALLING); //Attach 32Hz clock signal to our main clock function
- }
- void loop() {
- if(buttonReleased) {
- //Check if button has been newly pressed
- if(digitalRead(BUTTON) == LOW) { //User wants to advance to the next state
- state++;
- buttonReleased = false;
- if(state==5) {
- state = 0;
- rtc.setTime(hour, minute, second); //hr, min, sec
- }
- }
- } else if(digitalRead(BUTTON)==HIGH) {buttonReleased = true; }//Button has been released
- //State 0: Normal clock stuff
- tick(); //Done on 32Hz interrupt
- if(state!=0) { //Set all pixels to dark since we're setting something. We put this here so we don't repeat code
- for(int i=0; i<minSecStrip.numPixels(); i++) {
- minSecStrip.setPixelColor(i, 0);
- }
- for(int i=0; i<hourStrip.numPixels(); i++) {
- hourStrip.setPixelColor(i, 0);
- }
- }
- //State 1: Set Second
- if(state==1) {
- newPos = encoder.read();
- if(newPos!=oldPos) {
- second += (newPos-oldPos);
- oldPos = newPos;
- if(second>59){ second -=60; }
- if(second<0){ second +=60; }
- }
- minSecStrip.setPixelColor(minute*2, 100, 100, 100);
- minSecStrip.setPixelColor(minute*2+1, 100, 100, 100);
- minSecStrip.setPixelColor(minute*2+2, 100, 100, 100);
- minSecStrip.setPixelColor(minute*2+3, 100, 100, 100);
- minSecStrip.setPixelColor(second*2, 255, 10, 255);
- minSecStrip.setPixelColor(second*2+1, 255, 10, 255);
- minSecStrip.setPixelColor(second*2+2, 255, 10, 255);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12), 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+1, 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+2, 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+3, 200, 200, 200);
- }
- //State 2: Set Minute
- if(state==2) {
- newPos = encoder.read();
- if(newPos!=oldPos) {
- minute += (newPos-oldPos);
- oldPos = newPos;
- }
- if(minute>59){ minute -=60; }
- if(minute<0){ minute +=60; }
- minSecStrip.setPixelColor(second*2, 100, 100, 100);
- minSecStrip.setPixelColor(second*2+1, 100, 100, 100);
- minSecStrip.setPixelColor(second*2+2, 100, 100, 100);
- minSecStrip.setPixelColor(minute*2, 255, 10, 255);
- minSecStrip.setPixelColor(minute*2+1, 255, 10, 255);
- minSecStrip.setPixelColor(minute*2+2, 255, 10, 255);
- minSecStrip.setPixelColor(minute*2+3, 255, 10, 255);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12), 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+1, 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+2, 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+3, 200, 200, 200);
- }
- //State 3: Set Hour
- if(state==3) {
- newPos = encoder.read();
- if(abs(newPos-oldPos)>3) {
- hour += (newPos-oldPos)/3;
- oldPos = newPos;
- }
- if(hour>11) { hour -=12; }
- if(hour<0) { hour +=12; }
- minSecStrip.setPixelColor(second*2, 200, 200, 200);
- minSecStrip.setPixelColor(second*2+1, 200, 200, 200);
- minSecStrip.setPixelColor(second*2+2, 200, 200, 200);
- minSecStrip.setPixelColor(minute*2, 200, 200, 200);
- minSecStrip.setPixelColor(minute*2+1, 200, 200, 200);
- minSecStrip.setPixelColor(minute*2+2, 200, 200, 200);
- minSecStrip.setPixelColor(minute*2+3, 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12), 255, 10, 255);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+1, 255, 10, 255);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+2, 255, 10, 255);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+3, 255, 10, 255);
- }
- //State 4: Set Brightness
- if(state==4) {
- newPos = encoder.read();
- if(newPos!=oldPos) {
- brightness -= (newPos-oldPos);
- oldPos = newPos;
- }
- if(brightness>254) { brightness = 254; }
- if(brightness<1) { brightness = 1; }
- hourStrip.setBrightness(brightness);
- minSecStrip.setBrightness(brightness);
- for(int i=0; i<minSecStrip.numPixels(); i++) {
- minSecStrip.setPixelColor(i, 255, 255, 255);
- }
- for(int i=0; i<hourStrip.numPixels(); i++) {
- hourStrip.setPixelColor(i, 255, 255, 255);
- }
- }
- hourStrip.show();
- minSecStrip.show();
- }
- void tick() {//Do clock stuff at 32Hz; For some reason it actually does this at 64Hz, I'm not sure why...
- if(state==0) {
- //Advance 32Hz tick (actually 64Hz for some reason) and do second/minute/hour advances
- while(digitalRead(3)==LOW) {} //wait for rising edge of clock
- tic++;
- if(tic==32){
- //if((millis()-oldTime)>=472) { //Substitue timer for RTC since it isn't working yet
- // oldTime = millis();
- tic = 0;
- second--;
- if(second==-1) {
- second = 119;
- minute--;
- if(minute==-1) {
- minute = 59;
- hour--;
- if(hour==-1){ hour = 11;}
- }
- }
- }
- for(int i=0; i<minSecStrip.numPixels(); i++) {
- minSecStrip.setPixelColor(i, Wheel((i+i+minSecColor) & 255));
- }
- minSecStrip.setPixelColor(second, 0, 0, 0);
- minSecStrip.setPixelColor(second+1, 128, 128, 128);
- minSecStrip.setPixelColor(second+2, 0, 0, 0);
- minSecStrip.setPixelColor(minute*2, 0, 0, 0);
- minSecStrip.setPixelColor(minute*2+1, 255, 255, 255);
- minSecStrip.setPixelColor(minute*2+2, 255, 255, 255);
- minSecStrip.setPixelColor(minute*2+3, 0, 0, 0);
- minSecColor++;
- if(minSecColor>255) minSecColor = 0;
- minSecStrip.show();
- for(int i=0; i<hourStrip.numPixels(); i++) {
- hourStrip.setPixelColor(i, Wheel((hourColor) & 255));
- }
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12), 0, 0, 0);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+1, 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+2, 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+3, 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+4, 200, 200, 200);
- hourStrip.setPixelColor((hour*hourStrip.numPixels()/12)+5, 0, 0, 0);
- hourColor--;
- if(hourColor==0) hourColor = 256;
- hourStrip.show();
- }
- }
- // Input a value 0 to 255 to get a color value.
- // The colours are a transition r - g - b - back to r.
- uint32_t Wheel(byte WheelPos) {
- if(WheelPos < 85) {
- return hourStrip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
- } else if(WheelPos < 170) {
- WheelPos -= 85;
- return hourStrip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
- } else {
- WheelPos -= 170;
- return hourStrip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement