# Timed function

Can someone tell me where I can find an example to follow.

I want to make a pin go high after pressing a button, but after 2 minutes make the pin go low if the button has not been pressed again.

Many thanks

Every time you detect a switch closer you set a pin HIGH, and restart a 2 minute piece of timing code.
When the timing code sees 2 minutes go by, you make the pin LOW.

.

Thanks Larry, I was trying to find code as do I use millis() . I searched for examples but could not find simple examples as I am still new. Regards

ZOR2:
Thanks Larry, I was trying to find code as do I use millis() . I searched for examples but could not find simple examples as I am still new. Regards

Save the millis() value at the time that the start action happens. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly. If not, then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().

If pin12Millis is made equal to millis() every time a switch is closed.
The code in the { } will not run as long as you press the switch before 2 minutes goes by.

//*************************************
if (flag12 == true && millis() - pin12Millis >= 6021000UL)
{
//Turn off pin 12
digitalWrite(12,LOW); //Turn off LED
flag12 = false; //disable timing
}

Note:
flag12 is made true on a switch press.
.

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.

...R

Many thanks for the code Larry, and to UKHeliBob and Robin2. All very helpful, regards

Can someone tell me why this does not work. I am trying to do a repetitive action within a loop which is not working. It starts off okay working until time against interval value is met but then repeats itself rapidly.

``````long previousMillis = 0;
long interval = 10000;
unsigned long currentMillis = 0;

//Save the millis() value at the time that the start action happens.
//Then, each time through loop(), check whether the required wait period has elapsed
//by subtracting the start time from the millis() value now.
//If the period has elapsed then act accordingly.  If not, then go round loop() again

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

void loop()
{
currentMillis = millis(); // Save value of millis

if (currentMillis  > interval)
{
Serial.println("Done");
// Do something here, and reset for next check in loop
currentMillis = 0; // Zero currentMillis to start again
}
}
``````
``````void loop()
{
currentMillis = millis(); // Save value of millis

if (currentMillis  > interval)
{
Serial.println("Done");
// Do something here, and reset for next check in loop
// currentMillis = 0; // Zero currentMillis to start again
interval = interval + 10000; // set time for next check
}
}
``````

[code//Then, each time through loop(), check whether the required wait period has elapsed
//by subtracting the start time from the millis() value now.[/code]Where are you doing this ?

``````   currentMillis = 0; // Zero currentMillis to start again
``````

Why zero it ? The clue is in the name of the variable

Many thanks CrossRoads, just what I wanted.

Thanks UKHeliBob, point taken and understood. Regards both.

ZOR2:
Can someone tell me why this does not work.

You did not study the code in the link I gave you.

And I don't think the code in Reply #8 will work - certainly not if the program runs for a long time and millis() rolls over. Always use subtraction to determine if an interval has elapsed. And always make all values associated with millis() unsigned long

...R

Hello everyone, I’m sorry for cut the conversation… I’m a new beginner Arduino Uno user… I want to make a countdown timer with only 0.1-1sec… and I want some push buttom that have a function for up, down , start, and reset buttom… but I still don’t know how to do it… please help…

@bella_h

.

How the hours tick by trying to do something that should be so simple. Basically I am wanting to disable stepper motor coils when the motor has not run for a time (> 3 mins of being idle)

I have tried examples of time using millis, but it’s just a one shot timer I want which starts when the motor is run in either direction (pan/tilt) and then left.

A number of items are all selected by a radio remote control appearing in the void loop. I have removed all but the two stepper calls. I just want something in those calls that then gets checked at the end of void loop to disable the stepper I am using pin 11 on the Arduino to control the enable pin on the A4988 stepper driver.

Is there a simple way to do it.

``````I am trying to make the enable pin on a A4988 stepper motor driver turn off the coils on the stepper motor if after a time of inactivity of the code moving the motor.

[code]

void loop()
{

if (mySerial.available() > 4)
{
int input = mySerial.parseInt();

if (input == 102) // Pan Right

for (int x = 0; x < 3; x++)
{
digitalWrite(dirPin, LOW);// Stepper direction
digitalWrite(stepPin, HIGH);// Coils drive
delay(7);
digitalWrite(stepPin, LOW);// Coils drive
delay(7);
}

if (input == 103)// Pan Left

for (int x = 0; x < 3; x++)
{
digitalWrite(dirPin, HIGH);// Stepper direction
digitalWrite(stepPin, HIGH);// Coils drive
delay(7);
digitalWrite(stepPin, LOW);// Coils drive
delay(7);
}
}
}
``````

The motor is radio remote controlled. I want to

[/code]

I am trying to make the enable pin on a A4988 stepper motor driver turn off the coils on the stepper motor if after a time of inactivity of the code moving the motor.

Is there a simple way to do it.

millis() is the way to go.

Every time the motor has been run save the time from millis(). Each time through loop() check whether the inactivity period has elapsed by subtracting the time now from the start time. If the inactivity period has passed then act on it, otherwise keep going round loop()

Thanks. So saving millis when the motor is run is done by currentMillis = millis(); // Save value of millis

At the end of the loop checking start time from the millis() value now, does that mean I subtract the millis now from the stored currentMillis to see what value is

Regards

``````currentMillis = millis(); // Save value of millis
``````

Basically OK but it would be better with a more meaningful name for the start time
Perhaps

``````startMillis = millis(); // Save value of millis() at start of timing
``````

does that mean I subtract the millis now from the stored currentMillis to see what value is

No, the other way round. millis() will have increased since the start value was saved so you subtract the start value from the current value and compare it with the required period. Make all timing variables unsigned longs and the comparison will work even when millis() rolls over to zero in 49 and a bit days.

The BlinkWithoutDelay examples shows you the concept.

Still trying to get simple code.

Declare these

unsigned long startMillis;
unsigned long endMillis;

I put this in the event being started

startMillis = millis();

And at the end of the void loop, putting this in

if (startMillis > 0)
{
endMillis = startMillis - millis();
Serial.println(endMillis);
}

Just get long numbers.

I just want the void loop to keep going round until the endMillis reaches 1 minute.

So simple but so far away.

` if (startMillis > 0)`Why ?

I just want the void loop to keep going round until the endMillis reaches 1 minute.

Here is a short program that prints every 6 seconds. It should give you some clues

``````void setup()
{
Serial.begin(115200);
Serial.println("Started timing");
}

void loop()
{
static unsigned long startMillis = millis();
const unsigned long period = 6000;
unsigned long currentMillis = millis();
if (currentMillis - startMillis >= period)
{
Serial.println("Time's up");
startMillis = startMillis + period;
}
}
``````