Problem with multiplication, addition and a call to millis()

Has anyone seen any type of errors where the arduino (specifically the Yun) returns incorrect values during addition and/or multiplication?

I have the following lines in a sketch:

  long time_to_stop = millis() + (time * 1000);
  Console.print("led_on: "); Console.print(time); Console.print(", "); Console.print(time_to_stop); Console.print(", "); Console.println(millis());

The following is the output I see in the Serial monitor:
led_on: 600, 114192, 104070
led_on: 600, 118223, 108080

These values are wrong. and it appears to me that it is probably the (time * 1000) part of the equation that is returning bad numbers. Has anyone come across anything similar? I'm unable to locate such problems when I search through these forums.

Thanks,
Ray.

Just added this to same sketch at startup:

  Bridge.begin();
  Console.begin();
   
  while (!Console) {
    ; // wait for Console port to connect.
  }
  
  Console.println((600*1000));

The output I see is:

Unable to connect: retrying (1)... connected!
10176

How can 600*1000 be 10176

nvm. I'm an idiot, some kind of casting problem. Apologies for bothering.

Before your wrote Reply #2, I was writing this sketch on my Arduino Uno.

void setup(){
  Serial.begin(9600);
  
  // print 10176, because integer overflow
  Serial.println(600*1000);
  
  // print 600000
  Serial.println(600L*1000L);
}

void loop(){
  // always use unsigned long with millis
  unsigned long time = 6UL;      // 6 seconds
  unsigned long time_to_stop = millis() + (time * 1000UL);
  Serial.print("led_on: "); 
  Serial.print(time); 
  Serial.print(", "); 
  Serial.println(time_to_stop);
  
  delay(500);
}

:slight_smile: I'm new to embedded world. I'm having trouble adjusting to 8 bit life :slight_smile:

I'm an idiot, some kind of casting problem

The compiler should catch that. We certainly can't, seeing only snippets of code.

The Arduino team suppresses warnings, so the compiler-generated warning "You are an idiot" got suppressed. That's my guess as to what happened. 8)