[SOLVED] delay(1000) = 2 sec

Hi,

I'm having some trouble with the arduino time commands (delay(), millis(), delayMicroseconds(), ...), they all give double the time their supposed to. For example:

delay(1000); // Delays for 2 sec instead of 1 sec.
...
void loop()
{
  dt = millis() - timer;
  if (dt >= 1000) // True for every 2 sec. 
  {
    timer = millis();
    // SOME CODE HERE
  }
}
...
void loop()
{
  blinkALed()
  for (int i=0; i<1000; i++) // Again, loop delays for 2 sec.
  {
    delayMicroseconds(1000);
  }
}

But when I use _delay_ms(1000) it delays for 1 sec.
Some facts:
Arduino 0022
Ubuntu 10.10
Atmega1280 programmed with JTAGICE mkII and avrdude
Ext. crystal 8 MHz
Custom makefile (attached)

I've looked at the code behind the functions but I'm too new to arduino/microchip programming to debug it.
I'd appreciate if someone can shed some light on this.

Thank you...

Makefile (7.25 KB)

I tried Arduino 0021 and it works correctly...

Hi roller

This:

unsigned long dt, timer = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){
  dt = millis() - timer;
  if (dt >= 1000) // True for every 2 sec. 
  {
    timer = millis();
    Serial.println("Mother should I trust the goverment?");
  }
}

is printing out once every second.

I think you have a hardware problem (are you sure about the 8 MHz) or a make file problem.

-Fletcher

This:

crystal 8 MHz

looks like your issue. Your software is probably expecting that the device it is running on is clocked at 16mHz. Wiser heads will likely be able to tell you where to change the config to resolve it.

Haha... 8MHz = 1/2 of 16MHz. Therefore, it expects "n" cycles to pass per second, which the timers (clocked by the CPU clock cycles, not a real time clock inside the chip or something) count on. When 1/2 "n" cycles pass per second - that is, 1/2 of 16MHz = 8MHz - you get twice the delays because it has no perception of time outside of what it's configured to do. Throw an 8MHz clock at a 16MHz program and you'll get double seconds. :wink:

I looked over your makefile and you seem to have everything right... also scanned through the source code of the Arduino and avr-libc libraries, and couldn't find anything that would override F_CPU. Very strange. Only thing I can think of is that maybe some old files weren't being re-compiled when just the makefile was changed... maybe try running "make clean"?

Of course it begs the question, why not just use the Arduino IDE to compile and upload? Using hardware\arduino\boards.txt and programmers.txt, surely you can get the options you're looking for?

A'ha, "make clean" did the trick.

Thanks for the responses.