Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <SPI.h>
- #include <RTClib.h>
- #include <EEPROM.h>
- //#include <avr/pgmspace.h>
- //#include <Time.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[]={"12", "01", "02", "03", "04", "05", "06", "07", "08", "09"};
- // VARIABLES TO HOLD COLOR VALUES
- int redValue = 0;
- int grnValue = 0;
- int bluValue = 0;
- //VARIABLE FOR MENU STATE
- int menuState = 0;
- int brightnessState = 0;
- int hourState = 0;
- int varSet[] = {0, 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();
- //READ SAVED BRIGHTNESS AND 12/24 STATUS FROM PROGMEM
- brightnessState = EEPROM.read(254);
- hourState = EEPROM.read(255);
- if (brightnessState == 1){
- analogWrite(5, 100);
- }
- //Serial.begin(9600);
- // 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(){
- Serial.println(brightnessState);
- Serial.println(hourState);
- int joy = CheckJoystick();
- delay(60);
- switch (joy)
- {
- case Press:
- menuState = 1;
- menu();
- }
- //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
- //FOR 24 HR TIME
- if (hourState == 0){
- tft.setCursor(13, 54);
- if (now.hour() >= 0 && now.hour() < 10) {
- tft.print(leadingZero[now.hour()]);
- }
- else{
- tft.print(now.hour());
- }
- }
- //FOR 12 HR TIME
- if (hourState == 1){
- tft.setCursor(13, 54);
- int hour = now.hour();
- if (hour > 12){
- hour = hour - 12;
- tft.print(leadingZero[hour]);
- }
- else if (hour >= 0 && hour < 10) {
- tft.print(leadingZero[hour]);
- }
- else{
- tft.print(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());
- }
- }
- // FUNCTION TO 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;
- }
- void menu(){
- tft.fillScreen(WHITE);
- tft.setTextColor(BLACK, WHITE);
- tft.setTextSize(1);
- tft.setCursor(43, 2);
- tft.print("LEFT or RIGHT");
- tft.setCursor(27, 15);
- tft.print("to set brightness.");
- if (brightnessState == 0){
- tft.setCursor(65, 30);
- tft.print("HIGH");
- }
- if (brightnessState == 1){
- tft.setCursor(63, 30);
- tft.print(" LOW ");
- }
- tft.setCursor(47, 50);
- tft.print("UP or DOWN");
- tft.setCursor(39, 65);
- tft.print("for 12/24 hr.");
- if (hourState == 0){
- tft.setCursor(62, 80);
- tft.print("24 HR");
- }
- if (hourState == 1){
- tft.setCursor(62, 80);
- tft.print("12 HR");
- }
- tft.setCursor(37, 110);
- tft.print("PRESS to exit.");
- //delay(40);
- do{
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > 100){
- previousMillis = currentMillis;
- int joy = CheckJoystick();
- delay(60);
- switch (joy)
- {
- case Left:
- brightnessState = 1;
- analogWrite(5, 100);
- tft.setCursor(63, 30);
- tft.print(" LOW ");
- break;
- case Right:
- brightnessState = 0;
- analogWrite(5, 255);
- tft.setCursor(65, 30);
- tft.print("HIGH");
- break;
- case Up:
- hourState = 1;
- tft.setCursor(62, 80);
- tft.print("12 HR");
- break;
- case Down:
- hourState = 0;
- tft.setCursor(62, 80);
- tft.print("24 HR");
- break;
- case Press:
- tft.fillRect(0, 50, 160, 30, BLACK);
- menuState = 0;
- EEPROM.write(254, brightnessState); //SAVE BRIGHTNESS AND 12/24 STATUS TO EEPROM
- EEPROM.write(255, hourState); //SAVE BRIGHTNESS AND 12/24 STATUS TO EEPROM
- delay(100);
- return;
- break;
- }
- }
- }
- while (menuState == 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement