Guest User

Untitled

a guest
Nov 19th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. //-----------LED Settings-----------
  2. const int row[4] = {3, 4, 5, 6}; //pins for A, B, C, D rows in the LED matrix. see drawing in instructions
  3. const int col[4] = {7, 8, 9, 10}; //pins for 1, 2, 3, 4 columns in the LED matrix
  4. byte rowByte[4] = {0, 0, 0, 0}; //table that stores and indexes the bytes used to display
  5. int rowCount = 0; //index used while looping through bytes to display on matrix
  6. int delayTime = 300; //microseconds. delay time between each row beeing displayed in LED matrix. scanning display
  7.  
  8. //-----------Sleep Settings-----------
  9. #include "LowPower.h" //library for deep sleep
  10. unsigned long onTime = 20*1000; //time display stays on before going to deep sleep
  11. unsigned long sleepTimer = 0; //used to time how long the watch has been awake
  12. volatile bool woke = false; //AF
  13. int pushBtn = 2; //pin for push button used to interrupt sleep
  14. bool awakened = false;
  15.  
  16. //-----------Time Settings-----------
  17. #include <Wire.h> //i2c library
  18. #include "RTClib.h"
  19. #include <EEPROM.h> //library for reading/writing to eeprom
  20. RTC_DS3231 rtc; //name of attached RTC
  21. bool DST = 1; //used to signal if daylight savings time is active
  22. int timeOrDate = false; //indicates which display option to show. time or date
  23. int counterDST = 0; //counts number of button presses since awakening. used to toggle DST mode
  24. DateTime now;
  25.  
  26. //-----------Other Variables-----------
  27. volatile unsigned long prevInterrupt = 0; //used for debouncing push button
  28.  
  29. void wakeFunction();
  30. void displayRow(int, byte);
  31.  
  32. void setup()
  33. {
  34. //do you want to set the time? uncomment this line after first upload and time has been set
  35. //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  36. //rtc.adjust(DateTime(2018, 7, 5, 22, 51, 0)); //year, month, day, hour, minute, second
  37.  
  38. pinMode(pushBtn, INPUT_PULLUP);
  39. for(int i = 0; i < 4; i++) //turns all LEDs completely off
  40. {
  41. pinMode(row[i], OUTPUT);
  42. pinMode(col[i], OUTPUT);
  43. }
  44. allOff();
  45.  
  46. delay(40);
  47. attachInterrupt(0, wakeFunction, FALLING);
  48. DST = EEPROM.read(0); //gets the current DST settings
  49. }
  50.  
  51. void loop()
  52. {
  53. if(woke)
  54. {
  55. if(millis() - prevInterrupt > 100) //if button has been pressed for 100ms
  56. {
  57. if(!digitalRead(pushBtn))
  58. {
  59. awakened = true; //flag to start led matrix
  60. timeOrDate = !timeOrDate; //toggle between time and day being shown
  61. counterDST++; //increment button counter each time button is pressed after waking
  62. sleepTimer = millis(); //start the sleep countdown
  63. woke = false;
  64. now = rtc.now(); //gets the current time from DST
  65. now = now + TimeSpan(0, DST, 0, 0); //adds DST variable to time
  66. }
  67. else
  68. woke = false;
  69. }
  70. }
  71.  
  72. if(awakened)
  73. {
  74. if(millis() - sleepTimer < onTime) //if display has been on for less than 30 sec
  75. {
  76. if(timeOrDate) //loads the current time into bytes to be displayed
  77. {
  78. rowByte[0] = now.hour() / 10; //hour second digit
  79. rowByte[1] = now.hour() % 10; //hour first digit
  80. rowByte[2] = now.minute() / 10; //minute second digit
  81. rowByte[3] = now.minute() % 10; //minute first digit
  82. }
  83. else //loads the day and month into bytes to be displayed
  84. {
  85. rowByte[0] = now.day() / 10; //day first digit
  86. rowByte[1] = now.day() % 10; //day second digit
  87. rowByte[2] = now.month() / 10; //month first digit
  88. rowByte[3] = now.month() % 10; //month second digit
  89. }
  90.  
  91. displayRow(rowCount, rowByte[rowCount]); //shows one byte at the time on the matrix
  92. delayMicroseconds(delayTime);
  93. rowCount++; //increments byte to be shown on matrix
  94. if(rowCount > 3)
  95. rowCount = 0;
  96.  
  97. if(counterDST > 15) //if the push button has been pressed 15 times since wakeup
  98. {
  99. DST = !DST; //toggles DST hour added to time shown
  100. EEPROM.write(0, DST); //save DST state in eeprom
  101. counterDST = 0; //reset button counter
  102. }
  103. }
  104. else //when display has been on for 30 sec
  105. {
  106. timeOrDate = false; //set to false so time is first thing being shown on wakeup
  107. counterDST = 0; //reset button counter
  108. woke = false;
  109. awakened = false;
  110. }
  111. }
  112.  
  113. if(!woke && !awakened)
  114. {
  115. allOff();
  116. delay(30);
  117. LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //zzzz
  118. }
  119. }
  120.  
  121. void wakeFunction() //interrupt function when button is pressed
  122. {
  123. woke = true; //flags the microcontroller as just woken up
  124. prevInterrupt = millis(); //saves millis for debouncing
  125. //goes to main loop
  126. }
  127.  
  128. void displayRow(int inRow, byte inByte) //handles one row to display on the matrix at the time
  129. {
  130. //turns off all rows and columns
  131. for(int i = 0; i < 4; i++)
  132. {
  133. digitalWrite(row[i], LOW);
  134. digitalWrite(col[i], HIGH);
  135. }
  136.  
  137. digitalWrite(row[inRow], HIGH); //turns on the selected row
  138. for(int i = 0; i < 4; i++)
  139. digitalWrite(col[i], !bitRead(inByte, i)); //turns on the selected columns
  140. }
  141.  
  142. void allOff() //turn all IO off before going to sleep to save power
  143. {
  144. for(int i = 0; i < 4; i++)
  145. {
  146. digitalWrite(row[i], LOW); //rows must be set low to turn off
  147. digitalWrite(col[i], LOW); //columns must be set high to turn off
  148. }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment