View Single Post
Old 10-03-2010, 07:12 AM   #3 (permalink)
FalconFour
EcoModding Lurker
 
FalconFour's Avatar
 
Join Date: Sep 2010
Location: Fresno, CA
Posts: 78

LEAF - '11 Nissan LEAF
Thanks: 4
Thanked 9 Times in 7 Posts
And a fun one, if at least to see how far the MPGuino's interrupt-generated clock differs from reality (if "1.024 milliseconds, we will call that a millisecond for our purposes" is any indicator )... a clock.

It's not complete. The "values editor" has to first be created here in order to set the time on the clock. But it does count time, and it seems to be working, so I'll provide that here as a starting point, and to get some feedback (particularly on if there's anything I can do to clean up all the mBuff parameters)...

First, a new function, char * clock():
Code:
char * clock() {
  unsigned int clockDelta = tank.loopCount / loopsPerSecond;
  // if over 24 hours, remove 1 day. will occur after running for more than 24 hours.
  while (clockDelta > 86400) clockDelta -= 86400;
  unsigned int clock = (clockBase + clockDelta);
  // if we then add the clock base and it's over 11:59pm, wrap around to midnight.
  // will occur once a day and will reset when the clockDelta check above catches it.
  if (clock > 86400) clock -= 86400;
  unsigned int clockSeconds = (clock % 60);
  // will still be seconds so divide later
  unsigned int clockMinutes = (clock - clockSeconds) % 3600;
  unsigned int clockHours = (clock - clockMinutes - clockSeconds) / 3600;
  // divide to get minutes from mod seconds
  clockMinutes /= 60;
  boolean clockAmPm = false;
  if (clockHours > 12) {
    clockHours -= 12;
    clockAmPm = true;
  }
  if (clockHours == 0) clockHours = 12;
  mBuff[0]='0'+clockHours/10;
  mBuff[1]='0'+clockHours%10;
  mBuff[2]=':';
  mBuff[3]='0'+clockMinutes/10;
  mBuff[4]='0'+clockMinutes%10;
  mBuff[5]=':';
  mBuff[6]='0'+clockSeconds/10;
  mBuff[7]='0'+clockSeconds%10;
  mBuff[8]=clockAmPm?'p':'a';
  mBuff[9]='m';
  return mBuff;
}
So, add that code above somewhere, I added it below instantgph() and above the Trip functions.

Then, a way to display it. Basically just call LCD:rint(clock()) anywhere. I added a new menu for it. Take the construct above and replace the blank line print with:
Code:
  LCD::print("Time: ");
  LCD::print(clock());
Then bam, you get a clock starting at 12am. Happiness!

Attached is a photo of my work thus far
Attached Thumbnails
Click image for larger version

Name:	20101003034408.jpg
Views:	140
Size:	102.7 KB
ID:	6975  

Last edited by FalconFour; 10-04-2010 at 09:11 AM.. Reason: Corrected bugs that would result in after-13pm oddities after 24 hours
  Reply With Quote