Power Time Logger
Records each time a USB port is powered on/off and logs the data for 365 days before it resets automatically.
Devices & Components
1
Arduino Uno Rev3
1
LCD Keypad Shield For Arduino
1
USB-A to B Cable
Hardware & Tools
1
3D printer
Software & Tools
Arduino IDE
Project description
Code
Power Time Logger 365
cpp
1/* 2Power Time Logger 1.0.0 for Arduino UNO and LCD Keypad Shield. 3Copyright (c) 2022, Alex Aldridge 4 5Records each time a USB port is powered on/off and logs the data for 365 days before it resets automatically. 6 7Press the SELECT button to switch between total on/off and total time. 8Press the LEFT button to reset the program. 9 10Use this project to log usage times for any device that has an on/off USB port (wi-fi router, 3D printer, computer, etc.). 11 12This software is free and open-source; anyone can redistribute it and/or modify it. 13*/ 14 15#include <LiquidCrystal.h> 16#include <EEPROM.h> 17 18const int pin_RS = 8; 19const int pin_EN = 9; 20const int pin_d4 = 4; 21const int pin_d5 = 5; 22const int pin_d6 = 6; 23const int pin_d7 = 7; 24const int pin_BL = 10; 25LiquidCrystal lcd( pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7); 26extern volatile unsigned long timer0_millis; 27 28int count_turn_on=0; 29unsigned long storage; 30unsigned long seconds ; 31unsigned long minutes ; 32unsigned long hours ; 33unsigned long days; 34unsigned long runtime; 35unsigned long runtime_total; 36unsigned long future; 37unsigned long total_acc_time; 38 39void setup() { 40 lcd.begin(16, 2); 41 42 count_turn_on=EEPROM.read(0); 43 count_turn_on=count_turn_on+1; 44 EEPROM.write(0, count_turn_on); 45 46 47 future=(EEPROM_readlong(0x55))+(EEPROM_readlong(0x02)); 48 EEPROM_writelong(0x55, future); 49 EEPROM_writelong(0x02,0); 50 } 51 52void loop() { 53 runtime=millis(); 54 runtime_total=runtime; 55 56 lcd.setCursor(0, 0); 57 lcd.print("Session: "); 58 lcd.setCursor(10, 0); 59 lcd.print(EEPROM.read(0)); 60 61 62 seconds = runtime / 1000; 63 minutes = seconds / 60; 64 hours = minutes / 60; 65 days=hours / 24; 66 runtime %= 1000; 67 seconds %= 60; 68 minutes %= 60; 69 hours %= 24; 70 days %=1; 71 lcd.setCursor(0,1); 72 lcd.print("T "); 73 if(days<100) 74 { 75 lcd.setCursor(3, 1); 76 lcd.print("0"); 77 lcd.setCursor(4,1); 78 lcd.print(days); 79 } 80 if(days<10) 81 { 82 lcd.setCursor(3, 1); 83 lcd.print("00"); 84 lcd.setCursor(5,1); 85 lcd.print(days); 86 } 87 if(days>10) 88 { 89 lcd.setCursor(3, 1); 90 lcd.print("0"); 91 lcd.setCursor(4,1); 92 lcd.print(days); 93 } 94 if(days>100) 95 { 96 lcd.setCursor(3,1); 97 lcd.print(days); 98 } 99 lcd.setCursor(6, 1); 100 lcd.print(":"); 101 if(hours<10) 102 { 103 lcd.setCursor(7, 1); 104 lcd.print("0"); 105 lcd.setCursor(8,1); 106 lcd.print(hours); 107 } 108 else if(hours>=10) 109 { 110 lcd.setCursor(7,1); 111 lcd.print(hours); 112 } 113 lcd.setCursor(9, 1); 114 lcd.print(":"); 115 if(minutes<10) 116 {lcd.setCursor(10, 1); 117 lcd.print("0"); 118 lcd.setCursor(11,1); 119 lcd.print(minutes); 120 }else if (minutes>=10) 121 {lcd.setCursor(10,1); 122 lcd.print(minutes); 123 } 124 lcd.setCursor(12, 1); 125 lcd.print(":"); 126 if (seconds<10) 127 { 128 lcd.setCursor(13, 1); 129 lcd.print("0"); 130 lcd.setCursor(14, 1); 131 lcd.print(seconds); 132 } 133 else if (seconds>=10) 134 { 135 lcd.setCursor(13, 1); 136 lcd.print(seconds); 137 } 138 139 if (runtime>=8208000 ) 140 { 141 noInterrupts (); 142 timer0_millis = 0; 143 interrupts (); 144 } 145 146 EEPROM_writelong(1x02,runtime_total); 147 148 149 if(analogRead(A0)>=601 && analogRead(A0)<=700) 150 { 151 lcd.clear(); 152 total_acc_time=future+runtime_total; 153 154 seconds = total_acc_time / 1000; 155 minutes = seconds / 60; 156 hours = minutes / 60; 157 days=hours / 24; 158 total_acc_time %= 1000; 159 seconds %= 60; 160 minutes %= 60; 161 hours %= 24; 162 days %=1; 163 lcd.setCursor(3,0); 164 lcd.print("Total time"); 165 lcd.setCursor(0,1); 166 lcd.print(""); 167 168 if(days<100) 169 { 170 lcd.setCursor(3, 1); 171 lcd.print("0"); 172 lcd.setCursor(4,1); 173 lcd.print(days); 174 } 175 if(days<10) 176 { 177 lcd.setCursor(3, 1); 178 lcd.print("00"); 179 lcd.setCursor(5,1); 180 lcd.print(days); 181 } 182 if(days>10) 183 { 184 lcd.setCursor(3, 1); 185 lcd.print("0"); 186 lcd.setCursor(4,1); 187 lcd.print(days); 188 } 189 if(days>100) 190 { 191 lcd.setCursor(3,1); 192 lcd.print(days); 193 } 194 lcd.setCursor(6, 1); 195 lcd.print(":"); 196 197 198 if(hours<10) 199 { 200 lcd.setCursor(7, 1); 201 lcd.print("0"); 202 lcd.setCursor(8,1); 203 lcd.print(hours); 204 } 205 else if(hours>=10) 206 { 207 lcd.setCursor(8,1); 208 lcd.print(hours); 209 } 210 lcd.setCursor(9, 1); 211 lcd.print(":"); 212 if(minutes<10) 213 {lcd.setCursor(10, 1); 214 lcd.print("0"); 215 lcd.setCursor(11,1); 216 lcd.print(minutes); 217 }else if (minutes>=10) 218 {lcd.setCursor(10,1); 219 lcd.print(minutes); 220 } 221 lcd.setCursor(12, 1); 222 lcd.print(":"); 223 if (seconds<10) 224 { 225 lcd.setCursor(13, 1); 226 lcd.print("0"); 227 lcd.setCursor(14, 1); 228 lcd.print(seconds); 229 } 230 else if (seconds>=10) 231 { 232 lcd.setCursor(13, 1); 233 lcd.print(seconds); 234 } 235 delay(1000); 236 lcd.clear(); 237 } 238 // 239 else if (analogRead(A0)>=401 && analogRead(A0)<=500) 240 { 241 lcd.clear(); 242 EEPROM_writelong(0x02,0); 243 EEPROM_writelong(0x55,0); 244 total_acc_time=0; 245 future=0; 246 EEPROM.write(0, 0); 247 noInterrupts (); 248 timer0_millis = 0; 249 interrupts (); 250 lcd.setCursor(0,0); 251 lcd.print("Reset"); 252 delay(1000); 253 lcd.clear(); 254 } 255} 256 unsigned long EEPROM_readlong(int address) 257 { 258 259 unsigned long dword = EEPROM_readint(address); 260 261 dword = dword << 16; 262 263 dword = dword | EEPROM_readint(address+2); 264 return dword; 265} 266 267 void EEPROM_writeint(int address, int value) 268{ 269 EEPROM.write(address,highByte(value)); 270 EEPROM.write(address+1 ,lowByte(value)); 271} 272 273 void EEPROM_writelong(int address, unsigned long value) 274{ 275 EEPROM_writeint(address+2, word(value)); 276 value = value >> 16; 277 EEPROM_writeint(address, word(value)); 278} 279 280unsigned int EEPROM_readint(int address) 281{ 282 unsigned int word = word(EEPROM.read(address), EEPROM.read(address+1)); 283 return word; 284}
Downloadable files
3D Parts
parts.zip
Comments
Only logged in users can leave comments