5

I do have a small Arduino programming that simply stops after first loop. I might overlook something...but I'm simply clueless about what is happening.

Here is the code

int led = 13;
//int led = 10;
unsigned long windtime = 1000 * 2; // 2 seconds
unsigned long pausetime = 1000 * 60; // 1 minute

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);

  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  Serial.print("Wind");
  digitalWrite(led, HIGH);
  delay(windtime);               

  Serial.print("Pause");
  digitalWrite(led, LOW);    
  delay(pausetime);
}

I used Serial only as debug echo.

Any idea?

4
  • The program you gave looks good...never rule out a hardware bug. Perhaps you have the debugger turned on with a breakpoint?
    – AndyG
    Commented Oct 3, 2013 at 19:26
  • No debugger active. Both my Arduino UNO and Nano simply stops after first blink....
    – Alex
    Commented Oct 3, 2013 at 20:51
  • Have you tried using millis() instead of delay. Also try your program without serial initialization and see if it works.It could be some hardware issue too.
    – praks411
    Commented Oct 4, 2013 at 8:44
  • Check my answer. I made it work even I do not have the ultimate understanding of it.
    – Alex
    Commented Oct 4, 2013 at 11:34

4 Answers 4

6

It seems that you need to explicitly set numeric literals to long (L) and they use them. Otherwise it does not work. If anyone can explain if there is any kind of automatic conversion it will be awesome but until then simply use:

unsigned long seconds = 1000L; // !!! SEE THE CAPITAL "L" USED!!!
unsigned long minutes = seconds * 60;
unsigned long hours = minutes * 60; 

and then simply use delay(millisec) as usual:

delay(5 * minutes);

It worked for me.

3

in your line:

unsigned long pausetime = 1000 * 60; // 1 minute

the Arduino will look at 1000 (integer) and 60 (integer) and so will work out an answer that it will try to slot into... an integer! This means the biggest answer it can give to pausetime is 32,767. Anything bigger than this will wrap round, so 60,000 minus two lots of 32,768 comes out at -5536.

To see it in action add Serial.print(1000 * 60); to the setup and watch in your Tools>Serial Monitor:

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  Serial.print(1000 * 60);
}

To force the Arduino to use your constants as unsigned longs add ul or UL to the end of the number.

0

It could be that the unsigned longs are getting over-written. As a debugging method try a hardcoding constant value like delay(6000) for six seconds. This will prove if your hardware is working.

Finally, not sure if the delay value should be unsigned long, I usually use int and not for 60,000 which is greater than what an int (2 bytes) on the Arduino can store. Remember, embedded systems really are smaller systems.

this example: http://arduino.cc/en/Tutorial/BlinkWithoutDelay implies that the value for delay must be an int.

Hope this helps.

2
  • As I can see from signature [ arduino.cc/en/Reference/delay ] delay(...) accepts unsigned long.
    – Alex
    Commented Oct 4, 2013 at 5:56
  • Check my answer. I made it work even I do not have the ultimate understanding of it.
    – Alex
    Commented Oct 4, 2013 at 11:31
0

instead of using

unsigned long windtime = 1000 * 2; // 2 seconds
unsigned long pausetime = 1000 * 60; // 1 minute

use

unsigned long windtime = 2000; // 2 seconds
unsigned long pausetime = 60000; // 1 minute

and done.

Not the answer you're looking for? Browse other questions tagged or ask your own question.