Components and supplies
1
0.91" I2C OLED 128x32
2
Resistor 10k ohm
1
JST ph 2.0 6pin connector pair
1
47ohm Resistor
1
Arduino Nano R3
1
Momentary Tactile Switch Push Button
Tools and machines
1
3D Printer (generic)
Project description
Code
Cartridge_playtime_counter_v2.ino
arduino
1// Counter starts by input pin(3) = HIGH 2// Eeprom will be erased after input pin (4) HIGH for 10sec. 3// Totaltime will be updated after next counter start/stop. 4 5 6#include <EEPROM.h> 7#include "SSD1306Ascii.h" 8#include "SSD1306AsciiAvrI2c.h" 9#include <secTimer.h> 10 11secTimer myTimer; 12 13 14long seconds=0; //seconds 15 16 17// 0X3C+SA0 - 0x3C or 0x3D 18#define I2C_ADDRESS 0x3C 19#define SSD1306_128_32 20 21SSD1306AsciiAvrI2c oled; 22//------------------------------------------------------------------------------ 23 24 25 int button; // start_1 26 int zeroing; // Eeprom zeroing 27 int zeroing_bit; 28 int cycle; 29 int off; // display on/off 30 int flash=1; // display on bit 31 int cleared; // Tot nytn refresh bitti jos s alle 10 32 int curr_minutes; 33 int addrwr = 0; 34 int done; // Eeprom zeroing done one time, reset by button 35 long address = 0; 36 37 38 byte fourwr; 39 byte threewr; 40 byte twowr; 41 byte onewr; 42 43 long four; 44 long three; 45 long two; 46 long one; 47 48 49 long aikatotal; 50 long tothrs; //total time h 51 long totmin; //total time min 52 long totsec; //total time s 53 54 55 56void setup() { 57 58 Serial.begin(9600); 59 oled.begin(&Adafruit128x32, I2C_ADDRESS); 60 oled.setFont(Adafruit5x7); 61 pinMode(3, INPUT); 62 pinMode(4, INPUT); 63 pinMode(13, OUTPUT); 64 oled.setContrast(0); 65 66 aikatotal=(EEPROMReadlong(0)); 67 cycle=1; 68 off=0; 69 70} 71 72void loop(){ 73 74 addrwr=0; 75 76 button = digitalRead(3); 77 zeroing = digitalRead(4); 78 79 80 if(zeroing == HIGH && done==0){ 81 82 oled.setCursor(0, 0); 83 oled.setRow(1); 84 oled.set1X(); 85 oled.print("Eeprom clear"); 86 oled.setCursor(0, 0); 87 oled.setRow(2); 88 oled.set1X(); 89 oled.print("Press button 10s"); 90 delay(1000); 91 oled.clear(); 92 93 if (digitalRead(4)==HIGH){ 94 oled.setCursor(0, 0); 95 oled.setRow(1); 96 oled.set1X(); 97 oled.print("Eeprom clear"); 98 oled.setCursor(0, 0); 99 oled.setRow(2); 100 oled.set1X(); 101 oled.print("Press button 9s"); 102 delay(1000); 103 } 104 if (digitalRead(4)==HIGH){ 105 oled.setCursor(0, 0); 106 oled.setRow(2); 107 oled.set1X(); 108 oled.print("Press button 8s"); 109 delay(1000); 110 } 111 if (digitalRead(4)==HIGH){ 112 oled.setCursor(0, 0); 113 oled.setRow(2); 114 oled.set1X(); 115 oled.print("Press button 7s"); 116 delay(1000); 117 } 118 if (digitalRead(4)==HIGH){ 119 oled.setCursor(0, 0); 120 oled.setRow(2); 121 oled.set1X(); 122 oled.print("Press button 6s"); 123 delay(1000); 124 } 125 if (digitalRead(4)==HIGH){ 126 oled.setCursor(0, 0); 127 oled.setRow(2); 128 oled.set1X(); 129 oled.print("Press button 5s"); 130 delay(1000); 131 } 132 if (digitalRead(4)==HIGH){ 133 oled.setCursor(0, 0); 134 oled.setRow(2); 135 oled.set1X(); 136 oled.print("Press button 4s"); 137 delay(1000); 138 } 139 if (digitalRead(4)==HIGH){ 140 oled.setCursor(0, 0); 141 oled.setRow(2); 142 oled.set1X(); 143 oled.print("Press button 3s"); 144 delay(1000); 145 } 146 if (digitalRead(4)==HIGH){ 147 oled.setCursor(0, 0); 148 oled.setRow(2); 149 oled.set1X(); 150 oled.print("Press button 2s"); 151 delay(1000); 152 } 153 if (digitalRead(4)==HIGH){ 154 oled.setCursor(0, 0); 155 oled.setRow(2); 156 oled.set1X(); 157 oled.print("Press button 1s"); 158 delay(1000); 159 } 160 161 162 //delay(5000); 163 164 digitalWrite(13, LOW); 165 delay(100); 166 digitalWrite(13, HIGH); 167 delay(100); 168 digitalWrite(13, LOW); 169 delay(100); 170 digitalWrite(13, HIGH); 171 delay(100); 172 digitalWrite(13, LOW); 173 174 // zeroing_bit=digitalRead(4); 175 176 if (digitalRead(4)==HIGH){ 177 aikatotal=0; 178 EEPROMWritelong(addrwr, aikatotal); 179 addrwr+=4; 180 oled.setCursor(0, 0); 181 oled.setRow(3); 182 oled.set1X(); 183 oled.print("Done!!"); 184 done=1; 185 delay(2000); 186 oled.clear(); 187 } 188 189 else{ 190 oled.setCursor(0, 0); 191 oled.setRow(3); 192 oled.set1X(); 193 oled.print("Clear CANCELLED"); 194 195 delay(3000); 196 oled.clear(); 197 } 198 199} 200 201 if (button == HIGH && cycle==0){ 202 203 myTimer.startTimer(); 204 cycle=1; 205 off=0; 206 done=0; 207 digitalWrite(13, HIGH); 208 } 209 210 if(button == LOW && cycle==1){ 211 212 myTimer.stopTimer(); 213 cycle=0; 214 215 delay(2000); 216 digitalWrite(13, LOW); 217 aikatotal=aikatotal+((curr_minutes*60)+seconds); 218 219 tothrs=aikatotal/3600; 220 totmin=((aikatotal-(tothrs*3600))/60); 221 totsec=((aikatotal-(tothrs*3600))-(totmin*60)); 222 223 curr_minutes=0; 224 myTimer.startTimer(); 225 myTimer.stopTimer(); 226 flash=1; 227 228 if(off==0){ 229 oled.setCursor(0, 0); 230 oled.setRow(3); 231 oled.set1X(); 232 oled.print("Now Playing:"); 233 oled.print(" "); 234 } 235 236 237 EEPROMWritelong(addrwr, aikatotal); 238 addrwr+=4; 239 240 241 if(totsec <=10 && cleared==0){ 242 oled.clear(); 243 cleared=1; 244 245 } 246 247 if(totsec > 1 && cleared==1){ 248 cleared=0; 249 } 250 251 } 252 253 seconds=myTimer.readTimer(); 254 255 if(seconds==60){ 256 257 curr_minutes++; 258 myTimer.stopTimer(); 259 myTimer.startTimer(); 260 261 if(off==0){ 262 oled.setCursor(0, 0); 263 oled.setRow(3); 264 oled.set1X(); 265 oled.print("Now Playing:"); 266 oled.print(" "); 267 } 268 } 269 270if (off==0){ 271 oled.setCursor(0, 0); 272 oled.setRow(1); 273 oled.set1X(); 274 oled.print("Total:"); 275 oled.print(tothrs); 276 oled.print("h "); 277 oled.print(totmin); 278 oled.print("min "); 279 oled.print(totsec); 280 oled.print("s"); 281 282 oled.setCursor(0, 0); 283 oled.setRow(3); 284 oled.set1X(); 285 oled.print("Now Playing:"); 286 oled.print(curr_minutes); 287 oled.print("min "); 288 oled.print(seconds); 289 oled.print("s"); 290} 291 292 293 if(seconds==60){ 294 oled.clear(); 295 } 296 297 if(flash==1){ 298 delay(5000); 299 flash=0; 300 off=1; 301 digitalWrite(13, LOW); 302 delay(500); 303 digitalWrite(13, HIGH); 304 delay(500); 305 digitalWrite(13, LOW); 306 delay(500); 307 digitalWrite(13, HIGH); 308 delay(500); 309 digitalWrite(13, LOW); 310 oled.clear(); 311 312 } 313 314 315} 316 317void EEPROMWritelong(int addrwr, long aikatotal) 318 { 319 //Decomposition from a long to 4 bytes by using bitshift. 320 //One = Most significant -> Four = Least significant byte 321 byte fourwr = (aikatotal & 0xFF); 322 byte threewr = ((aikatotal >> 8) & 0xFF); 323 byte twowr = ((aikatotal >> 16) & 0xFF); 324 byte onewr = ((aikatotal >> 24) & 0xFF); 325 326 //Write the 4 bytes into the eeprom memory. 327 EEPROM.write(addrwr, fourwr); 328 EEPROM.write(addrwr + 1, threewr); 329 EEPROM.write(addrwr + 2, twowr); 330 EEPROM.write(addrwr + 3, onewr); 331 332 digitalWrite(13, LOW); 333 delay(200); 334 digitalWrite(13, HIGH); 335 delay(200); 336 digitalWrite(13, LOW); 337 delay(200); 338 digitalWrite(13, HIGH); 339 delay(200); 340 digitalWrite(13, LOW); 341 } 342 343 344 345long EEPROMReadlong(long address) 346 { 347 //Read the 4 bytes from the eeprom memory. 348 long four = EEPROM.read(address); 349 long three = EEPROM.read(address + 1); 350 long two = EEPROM.read(address + 2); 351 long one = EEPROM.read(address + 3); 352 353 //Return the recomposed long by using bitshift. 354 return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF); 355 } 356 357 358 359//------------------------------------------------------------------------------ 360 361
Downloadable files
Schematics
Schematics
Schematics
Schematics
Comments
Only logged in users can leave comments