Advertisement
pleasedontcode

**Clock Display** rev_07

Dec 9th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Clock Display**
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-09 11:34:36
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* print a clock on display and each number is within */
  21.     /* a circle. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <SPI.h>
  28. #include <ILI9488.h>    //https://github.com/jaretburkett/ILI9488
  29. #include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
  30. #include <RTClib.h> // Include the RTC library
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void drawClock();
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t display_ILI9488_TFT_RST_PIN_D2        = 2;
  39. const uint8_t display_ILI9488_TFT_DC_PIN_D3     = 3;
  40.  
  41. /***** DEFINITION OF SPI PINS *****/
  42. const uint8_t display_ILI9488_TFT_SPI_PIN_MOSI_D11      = 11;
  43. const uint8_t display_ILI9488_TFT_SPI_PIN_MISO_D12      = 12;
  44. const uint8_t display_ILI9488_TFT_SPI_PIN_SCLK_D13      = 13;
  45. const uint8_t display_ILI9488_TFT_SPI_PIN_CS_D10        = 10;
  46.  
  47. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  48. /***** used to store raw data *****/
  49. bool    display_ILI9488_TFT_RST_PIN_D2_rawData      = 0;
  50. bool    display_ILI9488_TFT_DC_PIN_D3_rawData       = 0;
  51.  
  52. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  53. /***** used to store data after characteristic curve transformation *****/
  54. float   display_ILI9488_TFT_RST_PIN_D2_phyData      = 0.0;
  55. float   display_ILI9488_TFT_DC_PIN_D3_phyData       = 0.0;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. // Create display object
  59. ILI9488 tft(display_ILI9488_TFT_SPI_PIN_CS_D10, display_ILI9488_TFT_DC_PIN_D3, display_ILI9488_TFT_RST_PIN_D2);
  60. U8G2_FOR_ADAFRUIT_GFX u8g2;
  61. RTC_DS1307 rtc; // Create an RTC object
  62.  
  63. void setup(void)
  64. {
  65.     // put your setup code here, to run once:
  66.  
  67.     pinMode(display_ILI9488_TFT_RST_PIN_D2,  OUTPUT);
  68.     pinMode(display_ILI9488_TFT_DC_PIN_D3,   OUTPUT);
  69.     pinMode(display_ILI9488_TFT_SPI_PIN_CS_D10,  OUTPUT);
  70.    
  71.     // start the SPI library:
  72.     SPI.begin();
  73.    
  74.     // Initialize the display
  75.     tft.begin();
  76.     tft.setRotation(3); // Set rotation if needed
  77.     tft.fillScreen(ILI9488_BLACK); // Clear screen
  78.     u8g2.begin(tft); // Initialize U8g2 with the display
  79.  
  80.     // Initialize the RTC
  81.     if (!rtc.begin()) {
  82.         // RTC not found, handle error
  83.         while (1);
  84.     }
  85. }
  86.  
  87. void loop(void)
  88. {
  89.     // put your main code here, to run repeatedly:
  90.     drawClock(); // Call function to draw clock
  91.     delay(1000); // Update every second
  92. }
  93.  
  94. void drawClock() {
  95.     // Get current hour and minute
  96.     DateTime now = rtc.now(); // Get current time from RTC
  97.     int hour = now.hour(); // Get the current hour
  98.     int minute = now.minute(); // Get the current minute
  99.  
  100.     // Clear the previous clock
  101.     tft.fillScreen(ILI9488_BLACK);
  102.  
  103.     // Draw clock face
  104.     int centerX = tft.width() / 2;
  105.     int centerY = tft.height() / 2;
  106.     int radius = 100;
  107.  
  108.     // Draw circles for each hour
  109.     for (int i = 0; i < 12; i++) {
  110.         float angle = (i * 30) * (PI / 180); // 30 degrees for each hour
  111.         int x = centerX + radius * cos(angle);
  112.         int y = centerY + radius * sin(angle);
  113.         tft.drawCircle(x, y, 20, ILI9488_WHITE); // Draw circle for hour
  114.         u8g2.setFont(u8g2_font_ncenB08_tr); // Set font for hour numbers
  115.         u8g2.setCursor(x - 5, y + 5); // Position cursor
  116.         u8g2.print(i + 1); // Print hour number
  117.     }
  118.  
  119.     // Draw hands for the current time
  120.     float hourAngle = (hour % 12 + minute / 60.0) * 30; // Hour hand
  121.     float minuteAngle = minute * 6; // Minute hand
  122.  
  123.     // Draw hour hand
  124.     int hourX = centerX + (radius - 40) * cos(hourAngle * (PI / 180));
  125.     int hourY = centerY + (radius - 40) * sin(hourAngle * (PI / 180));
  126.     tft.drawLine(centerX, centerY, hourX, hourY, ILI9488_WHITE);
  127.  
  128.     // Draw minute hand
  129.     int minuteX = centerX + (radius - 20) * cos(minuteAngle * (PI / 180));
  130.     int minuteY = centerY + (radius - 20) * sin(minuteAngle * (PI / 180));
  131.     tft.drawLine(centerX, centerY, minuteX, minuteY, ILI9488_WHITE);
  132. }
  133.  
  134. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement