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?
Instead of using delay(), you can employ millis() in your sketch and check how much time has elapsed since the Arduino last executed some code. Alternatively, you can also utilize a custom counter and increment it in the loop()-method.
As you may have guessed, the minimum delay you can introduce using the delay function is 1 milli-second. What if you want an even shorted delay? Arduino has a delayMicroseconds() function for that, which takes in the value of the delay in microseconds as the argument.
The way the Arduino delay() function works is pretty straight forward. It accepts a single integer as an argument. This number represents the time in milliseconds the program has to wait until moving on to the next line of code. When you do delay(1000) your Arduino stops on that line for 1 second.
Unsigned longs on the arduino can reach from 0 to 4,294,967,295. It is likely that the number being passed to 'delay' is being interpreted as an int. This would mean the delay is limited to a max of 32,767. You should explicitly declare your delay value as an unsigned long like the solution in this post.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With