system
September 2, 2014, 5:27pm
1
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.
system
September 2, 2014, 5:42pm
2
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
system
September 2, 2014, 5:44pm
3
nvm. I'm an idiot, some kind of casting problem. Apologies for bothering.
Peter_n
September 2, 2014, 5:52pm
4
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);
}
system
September 2, 2014, 6:01pm
5
I'm new to embedded world. I'm having trouble adjusting to 8 bit life
system
September 2, 2014, 9:44pm
6
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)