Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <SPI.h>
- #include <RTClib.h>
- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_ST7735.h> // Hardware-specific library
- RTC_DS3231 RTC;
- //COLOR DEFINITIONS
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFE0
- #define WHITE 0xFFFF
- #define GREY 0x8410
- // ARRAY TO REPLACE SINGLE DIGITS WITH LEADING ZEROES
- char* leadingZero[]={
- "00", "01", "02", "03", "04", "05", "06", "07", "08", "09"};
- // VARIABLES TO HOLD COLOR VALUES
- int redValue = 0;
- int grnValue = 0;
- int bluValue = 0;
- //TFT SHIELD STUFF
- //Use these pins for the shield!
- #define sclk 13
- #define mosi 11
- #define cs 10
- #define dc 8
- #define rst 0 // you can also connect this to the Arduino reset
- // MORE SHIELD STUFF
- #if defined(__SAM3X8E__)
- #undef __FlashStringHelper::F(string_literal)
- #define F(string_literal) string_literal
- #endif
- int position = 0;
- int dim = 255;
- int bright = 0;
- long previousMillis = 0;
- int interval = 1000;
- //THIS LINE MAY VARY - SEE EXAMPLE SKETCHES FROM SHIELD
- Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);
- //DEFINITIONS FOR THE JOYSTICK
- #define Neutral 0
- #define Press 1
- #define Up 2
- #define Down 3
- #define Right 4
- #define Left 5
- void setup() {
- Wire.begin();
- RTC.begin();
- if (! RTC.isrunning()) {
- Serial.println("RTC is NOT running!");
- // following line sets the RTC to the date & time this sketch was compiled
- RTC.adjust(DateTime(__DATE__, __TIME__));
- }
- DateTime now = RTC.now();
- // MANAUALLY ENTER TIME WITH THIS LINE, COMMENT OUT IF USING RTC
- //setTime(16, 56, 0, 16, 9, 2014);
- // MORE TFT STUFF - SEE EXAMPLE SKETCHES
- tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
- //ROTATES DISPLAY 90 DEGREES FOR LANDSCAPE VIEW
- tft.setRotation(3);
- // analogWrite(5, 255); //jump "LITE" and Digital pin 5 to dim
- // BLANKS OUT THE SCREEN AND DRAWS THE BLACK BAR
- tft.fillScreen(WHITE);
- tft.fillRect(0, 50, 160, 30, BLACK);
- }
- void loop(){
- //PERFORMS A TASK EVERY SECOND WITHOUT USING DELAY
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > interval) {
- previousMillis = currentMillis;
- // UPDATES THE SCREEN
- drawTime();
- }
- }
- void drawTime(){
- // SETS THE VARIABLE t TO THE CURRENT TIME - PREVENTS OVERLAPPING
- DateTime now = RTC.now();
- //MAPS THE TIME TO CORRESPOND TO COLOR VALUES
- // CONVERTS 24 HOURS TO 0 - 255 STEPS, ETC.
- redValue = map(now.hour(), 0, 23, 0, 255);
- grnValue = map(now.minute(), 0, 59, 0, 255);
- bluValue = map(now.second(), 0, 59, 0, 255);
- //SETS THE TEXT COLOR AND TEXT BACKGROUND COLOR
- //USES THE GFX FUNCTION TO CONVERT THREE INTEGER VALUES TO A HEX COLOR VALUE
- // REPLACE TFT.COLOR565 WITH WHITE OR ANOTHER COLOR IF YOU WANT A STATIC TIME COLOR
- tft.setTextColor((tft.Color565(redValue, grnValue, bluValue)), BLACK);
- // YOU HAVE TO SET THE TEXT SIZE BEFORE YOU START PRINTING
- tft.setTextSize(3);
- //DRAWS RECTANGLES ABOVE AND BELOW THE BLACK BAR IN THE SAME COLOR
- tft.fillRect(0, 0, 160, 50, (tft.Color565(redValue, grnValue, bluValue)));
- tft.fillRect(0, 80, 160, 50, (tft.Color565(redValue, grnValue, bluValue)));
- // LINES UP THE CURSOR AND DRAWS THE HOUR
- // IF THE HOUR IS LESS THAN 10 IT CALLS A VALUE FROM THE LEADING ZERO ARRAY
- tft.setCursor(13, 54);
- if (now.hour() >= 0 && now.hour() < 10) {
- tft.print(leadingZero[now.hour()]);
- }
- else{
- tft.print(now.hour());
- }
- // INSERTS THE FIRST DIVIDING COLON
- tft.setCursor(48, 54);
- tft.print(":");
- // MOVES THE CURSOR AND DRAWS THE MINUTES, LEADING ZERO, ETC
- tft.setCursor(65, 54);
- if (now.minute() >= 0 && now.minute() < 10){
- tft.print(leadingZero[now.minute()]);
- }
- else{
- tft.print(now.minute());
- }
- // INSERTS THE SECOND COLON
- tft.setCursor(99, 54);
- tft.print(":");
- // MOVES THE CURSOR AND DRAWS THE SECONDS, LEADING ZERO, ETC
- tft.setCursor(115, 54);
- if (now.second() >= 0 && now.second() <10){
- tft.print(leadingZero[now.second()]);
- }
- else{
- tft.print(now.second());
- }
- }
- // Check the joystick position
- int CheckJoystick()
- {
- int joystickState = analogRead(3);
- if (joystickState < 50) return Left;
- if (joystickState < 150) return Down;
- if (joystickState < 250) return Press;
- if (joystickState < 500) return Right;
- if (joystickState < 650) return Up;
- return Neutral;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement