Go Down

Topic: Bug: Ethernet Library Sample Code WebClientRepeating (Read 1 time) previous topic - next topic

Denbo

I originally posted this in http://arduino.cc/forum/index.php/topic,125510.0.html but since it appears to be a bug I will repost it here as well.

In the WebClientRepeating sample code for the Ethernet library there is a line for determining the posting interval.   The line is

Code: [Select]
const unsigned long postingInterval = 60*1000;  // delay between updates, in milliseconds

The problem is the actual value of the postingInterval is wrong when I print it out (it is 4294961760). It seems to be a casting/overflow problem with the compiler/preprocessor. It looks like it is treating the 60 and 1000 as signed integers.

I broke this out into very short sample code to demonstrate:
Code: [Select]
const unsigned long postingInterval = 60*1000;
const unsigned long postingIntervalB = (long)60*1000;

void setup() {
  Serial.begin(9600);
  Serial.println(postingInterval);
  Serial.println(postingIntervalB); 
}

void loop() {}


When I run this I get back the following values:
Code: [Select]
4294961760
60000


Other Ethernet samples use 10 seconds (10*1000) and do not appear to have this problem.  I assume it is because it is withing the range of an signed integer and that is how the compiler/preprocessor is treating the values. 

Massimo Banzi

I've forwarded your message to the testers to get a diagnose :)

thanks

Federico Vanzati

the problem with this line:
Code: [Select]
const unsigned long postingInterval = 60*1000;  // delay between updates, in millisecond

is that the two numbers (60 and 1000) are by default defined as int constants. For AVRs ints have the size of 16-bit, so the multiplication between two ints with a result that will exceed the maximum dimension for the type, generate an overflow.

The solution should be the variable cast that you propose or assigning the constant with a different type, like:

Code: [Select]
const unsigned long postingInterval = 60L*1000;  // Long typ e constant

or

const unsigned long postingInterval = 60000;  //assigning directly



This example will be fixed in the next IDE release.
Thanks for reporting!

F

Go Up