Advertisement
Guest User

RTC + oLed

a guest
Dec 2nd, 2021
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <RTClib.h>
  2. #include <Adafruit_SSD1306.h>
  3.  
  4. #define SCREEN_WIDTH 64 // OLED display width, in pixels, máx 128
  5. #define SCREEN_HEIGHT 16 // OLED display height, in pixels, máx 32
  6. #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  7. #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  8.  
  9. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  10.  
  11. // Instância de classe para o módulo RTC
  12. RTC_DS1307 rtc;
  13. char daysOfTheWeek[7][12] = {"Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"};
  14.  
  15. // Define pinos para as chaves tácteis
  16. int year, month, day, hour, minute, second;
  17. int long valorfinal = 0;
  18.  
  19.  
  20. void setup() {
  21. for (byte i = 0; i < 7; i++) {
  22. pinMode(i, INPUT_PULLUP);
  23. }
  24.  
  25. // Configura o RTC
  26. if (!rtc.begin()) {
  27. Serial.println("Não foi possível encontrar o RTC");
  28. while (1);
  29. }
  30. else {
  31. rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //Grava data e hora em que este programa for compilado
  32. }
  33. if (!rtc.isrunning()) {
  34. Serial.println("RTC não está funcionando");
  35. }
  36.  
  37. // inicializa display OLED com I2C e endereço 0x3C
  38. display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  39. display.clearDisplay();
  40. display.display();
  41.  
  42. telainicial();
  43. display.setTextSize(2);
  44. display.setTextColor(WHITE);
  45. }
  46.  
  47. void loop() {
  48. botoes();
  49. }
  50.  
  51. void botoes() {
  52. for (byte i = 0; i < 7; i++) {
  53. if (digitalRead[i] == LOW) { // ideal é mudar para o registrador
  54. while (!digitalRead(i)) {} // ideal é mudar para o registrador
  55. if (i < 5) {
  56. display.clearDisplay();
  57. display.setCursor(0, 0);
  58. valorfinal = (valorfinal * 10) + 1 + i;
  59. display.print(valorfinal);
  60. }
  61. else if (i == 5) {
  62. valorfinal = 0;
  63. display.clearDisplay();
  64. }
  65. else if (i == 6) {
  66. }
  67. display.display();
  68. delay(1);
  69. }
  70. }
  71. }
  72.  
  73. void telainicial() {
  74. display.clearDisplay();
  75. display.setTextSize(1);
  76. display.setTextColor(WHITE);
  77. display.setCursor(0, 0);
  78.  
  79. DateTime now = rtc.now();
  80. display.print(now.hour() < 10 ? "0" : "");
  81. display.print(now.hour(), DEC);
  82. display.print(':');
  83. display.print(now.minute() < 10 ? "0" : "");
  84. display.print(now.minute(), DEC);
  85. display.print(':');
  86. display.print(now.second() < 10 ? "0" : "");
  87. display.println(now.second(), DEC);
  88. // display.println();
  89. display.print(now.day() < 10 ? "0" : "");
  90. display.print(now.day(), DEC);
  91. display.print('/');
  92. display.print(now.month() < 10 ? "0" : "");
  93. display.print(now.month(), DEC);
  94. display.print('/');
  95. display.print(now.year(), DEC);
  96. display.display();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement