****EPISODE TITLE****
element14 presents | AirborneSurfer's VCP Profile | Project Videos
Arduino sketch:
//Project ColorTyme //Hexadecimal Color Code Clock //CC-BY-SA Matthew Eargle //element14 Presents http://element14.com/presents //AirborneSurfer Productions http://airbornesurfer.com //Included Libraries #include <Wire.h> #include <LedControl.h> #include <SPI.h> #include <Adafruit_RA8875.h> #include <RealTimeClockDS1307.h> /* Setup MAX7219 pin 8 is connected to the DataIn pin 7 is connected to the CLK pin 6 is connected to LOAD We have only a single MAX7219. */ LedControl lc=LedControl(8,7,6,1); //Define array for LED hex digits unsigned long delaytime=100; byte numbers[16] = { B11111100, B01100000, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110, B11101110, B11110011, B10011100, B11110001, B10011110, B10001110 }; //Define global variables int bOnes = 0; int bTwos = 0; int gOnes = 0; int gTwos = 0; int rOnes = 0; int rTwos = 0; int gCycle = 0; int rCycle = 0; int hours = 0; int minutes = 0; int seconds = 0; int seconds1 = 0; int minutes1 = 0; int hours1 = 0; int r = 0; int g = 0; int b = 0; float rCount = 0; float gCount = 0; float bCount = 0; uint16_t color = 0; int setPin = 17; int hSet = 2; int mSet = 4; int switchState=1; int mOnes = 0; int mTwos = 0; int hOnes = 0; int hTwos = 0; /* Adafruit RA8875 Library only supports hardware SPI at this time Connect SCLK to UNO Digital #13 (Hardware SPI clock) Connect MISO to UNO Digital #12 (Hardware SPI MISO) Connect MOSI to UNO Digital #11 (Hardware SPI MOSI) */ #define RA8875_INT 3 #define RA8875_CS 10 #define RA8875_RESET 9 //Define parameters and variables for TFT screen colors Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET); uint16_t tx, ty; void setup() { //DS1307 Setup pinMode(A3, OUTPUT); digitalWrite(A3, HIGH); pinMode(A2, OUTPUT); digitalWrite(A2, LOW); RTC.set24h(); RTC.start(); /* MAX72XX wakeup call */ lc.shutdown(0,false); /* set the brightness */ lc.setIntensity(0,1); /* clear the display */ lc.clearDisplay(0); int counter = 0; // RA8875 Initialization Serial.begin(9600); Serial.println("RA8875 start"); /* Initialize the display using 'RA8875_480x80', 'RA8875_480x128', 'RA8875_480x272' or 'RA8875_800x480' */ if (!tft.begin(RA8875_800x480)) { Serial.println("RA8875 Not Found!"); while (1); } Serial.println("Found RA8875"); tft.displayOn(true); tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight tft.PWM1out(255); //Time set controls setup pinMode(setPin,INPUT_PULLUP); pinMode(hSet,INPUT_PULLUP); pinMode(mSet,INPUT_PULLUP); } void loop() { //Loops through each function in turn readTime(); //Check for setting switch activate switchState=digitalRead(setPin); if (switchState==0) { settingTime(); }; displayTime(); delay(220); advanceTime(); compareTime(); //Set RGB values to current "count" values, rounding to nearest whole integer b = bCount; g = gCount; r = rCount; //Set current HMS to comparative values seconds1 = seconds; minutes1 = minutes; hours1 = hours; }; void readTime() { //RTC.readClock(); hours = RTC.getHours(); minutes = RTC.getMinutes(); seconds = RTC.getSeconds(); }; void displayTime() { lc.setColumn(0,2,numbers[bOnes]); lc.setColumn(0,1,numbers[bTwos]); lc.setColumn(0,4,numbers[gOnes]); lc.setColumn(0,3,numbers[gTwos]); lc.setColumn(0,6,numbers[rOnes]); lc.setColumn(0,5,numbers[rTwos]); color = ((r * 2048) + (g * 32) + (b)); tft.fillScreen(color); }; void advanceTime() { bOnes++; gCycle++; rCycle++; bCount = (bCount + 0.1255); gCount = (gCount + 0.004); rCount = (rCount + 0.00009); if (bOnes == 16) { bOnes = 0; bTwos++; }; if (bTwos == 16) { bTwos = 0; bCount = 0; }; if (b > 32) { bOnes = 0; bTwos = 0; bCount = 0; }; if (gCycle == 60) { gOnes++; gCycle = 0; }; if (gOnes >= 16) { gOnes = 0; gTwos++; }; if (gTwos >= 16) { gTwos = 0; gCount = 0; }; if (g > 64) { gOnes = 0; gTwos = 0; gCount = 0; } if (rCycle == 1436) { rOnes++; rCycle = 0; }; if (rOnes >= 16) { rOnes = 0; rTwos++; }; if (rTwos >= 16) { rTwos = 0; rCount = 0; }; if (r > 32) { rOnes = 0; rTwos = 0; rCount = 0; }; }; void compareTime() { if (seconds != seconds1) { if (seconds == 0) { bOnes = 0; bTwos = 0; bCount = 0; }; }; if (minutes != minutes1) { switch (minutes) { case 15: gOnes = 0; gTwos = 4; gCount = 16; break; case 30: gOnes = 0; gTwos = 8; gCount = 32; break; case 45: gOnes = 0; gTwos = 12; gCount = 48; break; }; }; if (hours != hours1) { switch (hours) { case 0: rOnes = 0; rTwos = 0; rCount = 0; break; case 1: rOnes = 11; rTwos = 0; rCount = 1.333; break; case 2: rOnes = 5; rTwos = 1; rCount = 2.667; break; case 3: rOnes = 0; rTwos = 2; rCount = 4; break; case 4: rOnes = 11; rTwos = 2; rCount = 5.333; break; case 5: rOnes = 5; rTwos = 3; rCount = 6.667; break; case 6: rOnes = 0; rTwos = 4; rCount = 8; break; case 7: rOnes = 11; rTwos = 4; rCount = 9.333; break; case 8: rOnes = 5; rTwos = 5; rCount = 10.667; break; case 9: rOnes = 0; rTwos = 6; rCount = 12; break; case 10: rOnes = 11; rTwos = 6; rCount = 13.333; break; case 11: rOnes = 5; rTwos = 7; rCount = 14.667; break; case 12: rOnes = 0; rTwos = 8; rCount = 16; break; case 13: rOnes = 11; rTwos = 8; rCount = 17.333; break; case 14: rOnes = 5; rTwos = 9; rCount = 18.667; break; case 15: rOnes = 0; rTwos = 10; rCount = 20; break; case 16: rOnes = 11; rTwos = 10; rCount = 21.333; break; case 17: rOnes = 5; rTwos = 11; rCount = 22.667; break; case 18: rOnes = 0; rTwos = 12; rCount = 24; break; case 19: rOnes = 11; rTwos = 12; rCount = 25.333; break; case 20: rOnes = 5; rTwos = 13; rCount = 26.667; break; case 21: rOnes = 0; rTwos = 14; rCount = 28; break; case 22: rOnes = 11; rTwos = 14; rCount = 29.333; break; case 23: rOnes = 5; rTwos = 15; rCount = 33.39; break; }; }; }; void settingTime() { while (switchState == 0) { RTC.stop(); seconds = 0; lc.setColumn(0,2,numbers[seconds]); lc.setColumn(0,1,numbers[seconds]); lc.setColumn(0,4,numbers[mOnes]); lc.setColumn(0,3,numbers[mTwos]); lc.setColumn(0,6,numbers[hOnes]); lc.setColumn(0,5,numbers[hTwos]); int hButton=digitalRead(hSet); int mButton=digitalRead(mSet); if (hButton == 0) { hours++; hOnes++; if (hOnes == 10) { hTwos++; hOnes = 0; }; if (hTwos == 2 && hOnes == 4) { hTwos = 0; hOnes = 0; hours = 0; }; }; if (mButton == 0) { minutes++; mOnes++; if (mOnes == 10) { mTwos++; mOnes = 0; }; if (mTwos == 6) { mTwos = 0; mOnes = 0; minutes = 0; }; }; switchState=digitalRead(setPin); delay(500); }; RTC.setHours(hours); RTC.setMinutes(minutes); RTC.setSeconds(0); RTC.start(); };