user28569: I have been looking at this for hours and can't figure it out... Take the following code I ran on an Arduino Mega:
#define MINUTES 0
#define SECONDS 1
#define MILLIS  2
#define DEFAULT_SELECTED_TURN_DELAY 0, 45, 0

unsigned long calculate_ms(int , int , int );

int selected_turn_delay[3] = { DEFAULT_SELECTED_TURN_DELAY };

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("Values of selected_turn_delay:");
  Serial.print("minutes: ");
  Serial.println(selected_turn_delay[MINUTES]);
  Serial.print("seconds: ");
  Serial.println(selected_turn_delay[SECONDS]);
  Serial.print("milliseconds: ");
  Serial.println(selected_turn_delay[MILLIS]);

  unsigned long result = calculate_ms(selected_turn_delay[MINUTES], selected_turn_delay[SECONDS], selected_turn_delay[MILLIS]);
  unsigned long result1 = calculate_ms(selected_turn_delay[MINUTES], selected_turn_delay[SECONDS], selected_turn_delay[MILLIS]);
  unsigned long result2 = calculate_ms(0,45,0);
  unsigned long result3 = calculate_ms(0,30,0);
  unsigned long result4 = calculate_ms(0,31,0);
  unsigned long result5 = calculate_ms(0,32,0);
  unsigned long result6 = calculate_ms(0,33,0);
  unsigned long result7 = calculate_ms(0,34,0);
}

void loop() {
}

unsigned long calculate_ms(int minutes, int seconds, int milliseconds) {
  Serial.println("------------");
  Serial.println("In calculate_ms");
  Serial.println("------------");
  unsigned long calculated_total_milliseconds = 0;
  unsigned long minute_to_seconds = 0;
  unsigned long seconds_to_ms = 0;

  Serial.println("Input:");
  Serial.print("minutes: ");
  Serial.println(minutes);
  Serial.print("seconds: ");
  Serial.println(seconds);
  Serial.print("milliseconds: ");
  Serial.println(milliseconds);

  calculated_total_milliseconds = (milliseconds + (seconds * 1000) + ((minutes * 60) * 1000));

  Serial.print("Calculates ms:");
  Serial.println(calculated_total_milliseconds);
  return(calculated_total_milliseconds);
}

It gives me the following results:
Values of selected_turn_delay:
minutes: 0
seconds: 45
milliseconds: 0
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:4294946760
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:4294946760
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:4294946760
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 30
milliseconds: 0
Calculates ms:30000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 31
milliseconds: 0
Calculates ms:31000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 32
milliseconds: 0
Calculates ms:32000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 33
milliseconds: 0
Calculates ms:4294934760
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 34
milliseconds: 0
Calculates ms:4294935760

What am I missing why is the calculation for values above 32s out of wack ?
Tried the same code adapted for gcc:
#include <stdio.h>

#define MINUTES 0
#define SECONDS 1
#define MILLIS  2
#define DEFAULT_SELECTED_TURN_DELAY 0, 45, 0

unsigned long calculate_ms(int , int , int );

int selected_turn_delay[3] = { DEFAULT_SELECTED_TURN_DELAY };

int main(void) {
  printf("Values of selected_turn_delay:\n");
  printf("minutes: ");
  printf("%d\n",selected_turn_delay[MINUTES]);
  printf("seconds: ");
  printf("%d\n",selected_turn_delay[SECONDS]);
  printf("milliseconds: ");
  printf("%d\n",selected_turn_delay[MILLIS]);

  unsigned long result = calculate_ms(selected_turn_delay[MINUTES], selected_turn_delay[SECONDS], selected_turn_delay[MILLIS]);
  unsigned long result1 = calculate_ms(selected_turn_delay[MINUTES], selected_turn_delay[SECONDS], selected_turn_delay[MILLIS]);
  unsigned long result2 = calculate_ms(0,45,0);
  unsigned long result3 = calculate_ms(0,30,0);
  unsigned long result4 = calculate_ms(0,31,0);
  unsigned long result5 = calculate_ms(0,32,0);
  unsigned long result6 = calculate_ms(0,33,0);
  unsigned long result7 = calculate_ms(0,34,0);
}


unsigned long calculate_ms(int minutes, int seconds, int milliseconds) {
  printf("------------\n");
  printf("In calculate_ms\n");
  printf("------------\n");
  unsigned long calculated_total_milliseconds = 0;
  unsigned long minute_to_seconds = 0;
  unsigned long seconds_to_ms = 0;

  printf("Input:\n");
  printf("minutes: ");
  printf("%d\n",minutes);
  printf("seconds: ");
  printf("%d\n",seconds);
  printf("milliseconds: ");
  printf("%d\n",milliseconds);

  calculated_total_milliseconds = (milliseconds + (seconds * 1000) + ((minutes * 60) * 1000));

  printf("Calculates ms:");
  printf("%lu\n",calculated_total_milliseconds);
  return(calculated_total_milliseconds);
}

And it works great:
Values of selected_turn_delay:
minutes: 0
seconds: 45
milliseconds: 0
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 30
milliseconds: 0
Calculates ms:30000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 31
milliseconds: 0
Calculates ms:31000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 32
milliseconds: 0
Calculates ms:32000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 33
milliseconds: 0
Calculates ms:33000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 34
milliseconds: 0
Calculates ms:34000

It has to be something obvious, but I can't find it...

user28569: Ok well, 'before bed illumination'...
Changing:
calculated_total_milliseconds = (milliseconds + (seconds * 1000) + ((minutes * 60) * 1000));

for
calculated_total_milliseconds = (milliseconds + (seconds * 1000UL) + ((minutes * 60) * 1000UL));

Make it work:
Values of selected_turn_delay:
minutes: 0
seconds: 45
milliseconds: 0
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 30
milliseconds: 0
Calculates ms:30000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 31
milliseconds: 0
Calculates ms:31000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 32
milliseconds: 0
Calculates ms:32000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 33
milliseconds: 0
Calculates ms:33000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 34
milliseconds: 0
Calculates ms:34000


28569: <p>I have been looking at this for hours and can't figure it out... Take the following code I ran on an Arduino Mega:</p>

<pre><code>#define MINUTES 0
#define SECONDS 1
#define MILLIS  2
#define DEFAULT_SELECTED_TURN_DELAY 0, 45, 0

unsigned long calculate_ms(int , int , int );

int selected_turn_delay[3] = { DEFAULT_SELECTED_TURN_DELAY };

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("Values of selected_turn_delay:");
  Serial.print("minutes: ");
  Serial.println(selected_turn_delay[MINUTES]);
  Serial.print("seconds: ");
  Serial.println(selected_turn_delay[SECONDS]);
  Serial.print("milliseconds: ");
  Serial.println(selected_turn_delay[MILLIS]);

  unsigned long result = calculate_ms(selected_turn_delay[MINUTES], selected_turn_delay[SECONDS], selected_turn_delay[MILLIS]);
  unsigned long result1 = calculate_ms(selected_turn_delay[MINUTES], selected_turn_delay[SECONDS], selected_turn_delay[MILLIS]);
  unsigned long result2 = calculate_ms(0,45,0);
  unsigned long result3 = calculate_ms(0,30,0);
  unsigned long result4 = calculate_ms(0,31,0);
  unsigned long result5 = calculate_ms(0,32,0);
  unsigned long result6 = calculate_ms(0,33,0);
  unsigned long result7 = calculate_ms(0,34,0);
}

void loop() {
}

unsigned long calculate_ms(int minutes, int seconds, int milliseconds) {
  Serial.println("------------");
  Serial.println("In calculate_ms");
  Serial.println("------------");
  unsigned long calculated_total_milliseconds = 0;
  unsigned long minute_to_seconds = 0;
  unsigned long seconds_to_ms = 0;

  Serial.println("Input:");
  Serial.print("minutes: ");
  Serial.println(minutes);
  Serial.print("seconds: ");
  Serial.println(seconds);
  Serial.print("milliseconds: ");
  Serial.println(milliseconds);

  calculated_total_milliseconds = (milliseconds + (seconds * 1000) + ((minutes * 60) * 1000));

  Serial.print("Calculates ms:");
  Serial.println(calculated_total_milliseconds);
  return(calculated_total_milliseconds);
}
</code></pre>

<p>It gives me the following results:</p>

<pre><code>Values of selected_turn_delay:
minutes: 0
seconds: 45
milliseconds: 0
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:4294946760
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:4294946760
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:4294946760
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 30
milliseconds: 0
Calculates ms:30000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 31
milliseconds: 0
Calculates ms:31000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 32
milliseconds: 0
Calculates ms:32000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 33
milliseconds: 0
Calculates ms:4294934760
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 34
milliseconds: 0
Calculates ms:4294935760
</code></pre>

<p>What am I missing why is the calculation for values above 32s out of wack ?</p>

<p>Tried the same code adapted for gcc:</p>

<pre><code>#include &lt;stdio.h&gt;

#define MINUTES 0
#define SECONDS 1
#define MILLIS  2
#define DEFAULT_SELECTED_TURN_DELAY 0, 45, 0

unsigned long calculate_ms(int , int , int );

int selected_turn_delay[3] = { DEFAULT_SELECTED_TURN_DELAY };

int main(void) {
  printf("Values of selected_turn_delay:\n");
  printf("minutes: ");
  printf("%d\n",selected_turn_delay[MINUTES]);
  printf("seconds: ");
  printf("%d\n",selected_turn_delay[SECONDS]);
  printf("milliseconds: ");
  printf("%d\n",selected_turn_delay[MILLIS]);

  unsigned long result = calculate_ms(selected_turn_delay[MINUTES], selected_turn_delay[SECONDS], selected_turn_delay[MILLIS]);
  unsigned long result1 = calculate_ms(selected_turn_delay[MINUTES], selected_turn_delay[SECONDS], selected_turn_delay[MILLIS]);
  unsigned long result2 = calculate_ms(0,45,0);
  unsigned long result3 = calculate_ms(0,30,0);
  unsigned long result4 = calculate_ms(0,31,0);
  unsigned long result5 = calculate_ms(0,32,0);
  unsigned long result6 = calculate_ms(0,33,0);
  unsigned long result7 = calculate_ms(0,34,0);
}


unsigned long calculate_ms(int minutes, int seconds, int milliseconds) {
  printf("------------\n");
  printf("In calculate_ms\n");
  printf("------------\n");
  unsigned long calculated_total_milliseconds = 0;
  unsigned long minute_to_seconds = 0;
  unsigned long seconds_to_ms = 0;

  printf("Input:\n");
  printf("minutes: ");
  printf("%d\n",minutes);
  printf("seconds: ");
  printf("%d\n",seconds);
  printf("milliseconds: ");
  printf("%d\n",milliseconds);

  calculated_total_milliseconds = (milliseconds + (seconds * 1000) + ((minutes * 60) * 1000));

  printf("Calculates ms:");
  printf("%lu\n",calculated_total_milliseconds);
  return(calculated_total_milliseconds);
}
</code></pre>

<p>And it works great:</p>

<pre><code>Values of selected_turn_delay:
minutes: 0
seconds: 45
milliseconds: 0
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 30
milliseconds: 0
Calculates ms:30000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 31
milliseconds: 0
Calculates ms:31000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 32
milliseconds: 0
Calculates ms:32000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 33
milliseconds: 0
Calculates ms:33000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 34
milliseconds: 0
Calculates ms:34000
</code></pre>

<p>It has to be something obvious, but I can't find it...</p>
 28569: <p>Ok well, 'before bed illumination'...</p>

<p>Changing:</p>

<pre><code>calculated_total_milliseconds = (milliseconds + (seconds * 1000) + ((minutes * 60) * 1000));
</code></pre>

<p>for</p>

<pre><code>calculated_total_milliseconds = (milliseconds + (seconds * 1000UL) + ((minutes * 60) * 1000UL));
</code></pre>

<p>Make it work:</p>

<pre><code>Values of selected_turn_delay:
minutes: 0
seconds: 45
milliseconds: 0
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 45
milliseconds: 0
Calculates ms:45000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 30
milliseconds: 0
Calculates ms:30000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 31
milliseconds: 0
Calculates ms:31000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 32
milliseconds: 0
Calculates ms:32000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 33
milliseconds: 0
Calculates ms:33000
------------
In calculate_ms
------------
Input:
minutes: 0
seconds: 34
milliseconds: 0
Calculates ms:34000
</code></pre>
