I have a program built that I'd like to run for 3.5 minutes. Is there a way to do this?
thanks and best!
Nick
Yes, check in your code that millis() < 210000L. If it is, do your thing, else do nothing.
I'm a little lost on that. What do I need to do as far as the code to run the program for a set number of seconds? I know that C deals in milliseconds not seconds but what I'm needing to know is the what and where on how to implement it.
thanks!
Nick
millis() returns milliseconds since the sketch started, so your program could look like this:
void setup()
{
while(millis() < 210000L)
{
// Do Nick's thing
}
}
void loop()
{
}
Alternatively, you could do the same in loop. Post the code you have, perhaps it'll make it easier to figure out what would work best
Thank you Bill. Where would that fit into this code?
[code]
#include <Stepper.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
//// Cycle 1 Big to Small (a is biggest; o is smallest)
pos = 80;
myservo.write(pos);
delay(23);
}
[/code]
void loop()
{
//// Cycle 1 Big to Small (a is biggest; o is smallest)
pos = 80;
myservo.write(pos);
delay(23);
}
Difficult to see how you could repeat that for three and a half minutes.
It does one thing.
Takes maybe half a second to a second
AWOL posted the below which may be close to what you want.
I must've been (unusually) intoxicated.
#include <Servo.h>
const int servoPin = 2;
Servo myServo;
void setup ()
{
myServo.write (0);
myServo.attach (servoPin);
delay (1000); // give the servo time to reach 0 "degrees".
myServo.write (45); // send the servo to 45 "degrees"
delay (1000); // wait a second
myServo.write (0); // send it back to zero
}
void loop ()
{
// do nothing for the rest of time.
}
In the immortal words of ex-PFC Wintergreen "Too prolix".
AWOL, yes that would be a boring 3 and a half minute program! I have that as the basic program but with a traverse that gets complicated. So rather than post the slop I have as my code I posted the bones of it.
But I am wanting to find a good way to run that framework for 3.5 minutes then stop. I almost used a 555 timer IC to kill the power to a relay but then I realized that's a bit redundant since I'm working with a microcontroller.
After 3.5 minutes, send a signal to a latching relay to open the power line.
Manually reset it when you want power applied again.
Do you really want to kill the power? Or just have the program go into an idle state until it sees an input pin change state?
Blink without delay variation
setup()
time_check enabled
void loop()
time_check enabled?
yes:
do the servo thing
3.5 min elapsed?
no: do nothing
yes: time_check disabled
no: read restart_switch
restart_switch enabled?
yes: time_check enabled
no: do nothing
back to top of loop
a vary simple way, would be to just add this line in your loop().
while(millis() > 210000){}
it will stop your code until you reset manually. (well unless millis() overflows but i doubt you will use it for that long)
not the best way but probably the simplest. (if you have nothing fancy in mind)
How it works: (assuming your using a factory arduino 16mhz, with no timers changed)
millis(); reads the amount of time from when your program started.
all the below are equal.
210,000 millis
2100 seconds 1000 millis in a second
3.5 minutes 60 seconds in a minute
So ..while.. millis() ..is greater than.. 3.5 minutes.. do..{nothing}..
Millis goes over the set time and your program gets stuck in a while loop doing nothing.
a vary simple way, would be to just add this line in your loop().
while(millis() > 210000){}
Literals (210000) are treated, by the compiler, as integers, unless specifically told otherwise. Since 210000 will not fit in an integer variable without overflowing, this code will NOT produce the expected results,
Using 210000L or 210000UL will tell the compiler to treat the values as signed or unsigned long, rather than integer, resulting in "correct" behavior (though one might as well have used delay()).