uni

Thing1's amazing uni repo
Log | Files | Refs

triathlon.ino (1273B)


      1 struct time { int hours, mins, secs; };
      2 
      3 void
      4 SEprintf(const char *fmt, ...) {
      5   static char sbuf[64] = { 0 };
      6   va_list ap;
      7   va_start(ap, fmt);
      8   vsnprintf(sbuf, 64, fmt, ap);
      9   va_end(ap);
     10   Serial.print(sbuf);
     11 }
     12 
     13 int
     14 readint() {
     15   while (!Serial.available()) ; 
     16   return Serial.parseInt();
     17 }
     18 
     19 void
     20 readtime(const char *sport, struct time *t) {
     21   SEprintf("How many hours did %s take: ", sport);
     22   t->hours = readint();
     23   Serial.println();
     24 
     25   SEprintf("How many mins did %s take: ", sport);
     26   t->mins = readint();
     27   Serial.println();
     28 
     29   SEprintf("How many secs did %s take: ", sport);
     30   t->secs = readint();
     31   Serial.println();
     32 }
     33 
     34 void
     35 addtimes(struct time t1, struct time t2, struct time t3, struct time *res) {
     36   int scarry = 0, mcarry = 0;
     37 
     38   res->secs = t1.secs + t2.secs + t3.secs;
     39   scarry = (res->secs / 60);
     40   res->secs %= 60;
     41 
     42   res->mins = t1.mins + t2.mins + t3.mins + scarry;
     43   mcarry = (res->mins / 60);
     44   res->mins %= 60;
     45 
     46   res->hours = t1.hours + t2.hours + t3.hours + mcarry;
     47 }
     48 
     49 void 
     50 setup() {
     51   struct time t1, t2, t3, res;
     52   Serial.begin(9600);
     53 
     54   readtime("swim", &t1);
     55   readtime("cycle", &t2);
     56   readtime("run", &t3);
     57 
     58   addtimes(t1, t2, t3, &res);
     59   SEprintf("Total time: %dhr %dmin %dsec.\n", res.hours, res.mins, res.secs);
     60 }
     61 
     62 void 
     63 loop() {
     64 }