[Solved] What am I missing?

struct bellEventStruct {
  unsigned char startHour : 5;
  unsigned char startMinute : 6;
  unsigned char eventFlag : 1;
} currentEvent;

struct todClockStruct {
  unsigned char hours : 5;
  unsigned char minutes : 6;
  unsigned char seconds : 6;
  unsigned char tenths : 4;
  unsigned char format : 2;
  unsigned long timestamp;
} tod;

byte tDnDeciseconds;
byte tDnSecondsOnes;
byte tDnSecondsTens;
byte tDnMinutesOnes;
byte tDnMinutesTens;
byte tDnHoursOnes;
byte tDnHoursTens;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  tod.hours = 2;
  tod.minutes = 59;
  tod.seconds = 0;
  tod.tenths = 0;
  currentEvent.startHour = 3;
  currentEvent.startMinute = 0;
  unsigned long a = (currentEvent.startHour * 36000) + (currentEvent.startMinute * 600);
  Serial.println(a);
  unsigned long b = (tod.hours * 36000) + (tod.minutes * 600) + (tod.seconds * 10) + tod.tenths;
  Serial.println(b);
  unsigned long timerTime = a - b;
  Serial.println(timerTime);
  tDnHoursTens = timerTime / 360000;
  timerTime -= tDnHoursTens * 360000;
  tDnHoursOnes = timerTime / 36000;
  timerTime -= tDnHoursOnes * 36000;
  tDnMinutesTens = timerTime / 6000;
  timerTime -= tDnMinutesTens * 6000;
  tDnMinutesOnes = timerTime / 600;
  timerTime -= tDnMinutesOnes * 600;
  tDnSecondsTens = timerTime / 100;
  timerTime -= tDnSecondsTens * 100;
  tDnSecondsOnes = timerTime / 10;
  timerTime -= tDnSecondsOnes * 10;
  tDnDeciseconds = timerTime;
  char buf[80];
  sprintf(buf, "%u%u:%u%u:%u%u.%u", tDnHoursTens, tDnHoursOnes, tDnMinutesTens, tDnMinutesOnes, tDnSecondsTens, tDnSecondsOnes, tDnDeciseconds);
  Serial.println(buf);
}

void loop() {
  // put your main code here, to run repeatedly:

}

results in serial monitor:

108000
41864
66136
01:50:13.6

41864 should be 107400, making 66136 600 instead, making the formatted output 00:01:00.0. What is wrong with my calculation for b?

tod.hours * 36000

The default type for such expressions is int. This overflows.

I think you need to be a bit more specific as to what is going on , what maths function are you trying to achieve .

  • as an aside it helps to have a more descriptive title for your thread summering your problem

I’ve not looked at your code , but you seem to mixing variable types in your expressions , problem there ?? ( as per the previous reply)

aarg: tod.hours * 36000

The default type for such expressions is int. This overflows.

Ah, of course. Thank you!

unsigned long b = (tod.hours * 36000UL) + (tod.minutes * 600UL) + (tod.seconds * 10UL) + tod.tenths;