My Arduino stops after 1 minute wait

旧城冷巷雨未停 提交于 2019-12-10 16:32:43

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/19167272/my-arduino-stops-after-1-minute-wait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!