1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
HardwareSerial &pc = Serial; const byte pLED = 13; unsigned int totalSeconds=0, hours, minutes, seconds; void setup() { pinMode(pLED, OUTPUT); pc.begin(9600); while(!pc); pc.println("Contador de tiempo de LED 13 activo (H:M:S)"); } void loop() { static unsigned long tNow = 0; static unsigned long tPrevious = 0; tNow = millis(); if( digitalRead(pLED) && tNow - tPrevious >= 1000 ) { tPrevious = tNow; totalSeconds += 1; hours = totalSeconds / 3600; minutes = totalSeconds % 3600 / 60; seconds = totalSeconds % 60; } pc.print(hours < 10 ? "0" : ""); pc.print(hours); pc.print(":"); pc.print(minutes < 10 ? "0" : ""); pc.print(minutes); pc.print(":"); pc.print(seconds < 10 ? "0" : ""); pc.println(seconds); delay(1000); } |