Do some work for 5 seconds and then stop

I work on a Arduino nano. I want to send a signal for 5 seconds and then stop. How do I do this? If I use delay() I assume it doesn't work because with this command I cannot do something in parallel...

Set "ewent_started" = millis() when You issue Your signal. Then perform Your loop() and check if millis()-ewent_started is greater than 5000.

Thank you…

I found this code:

starttime = millis();
endtime = starttime;
While ((endtime - starttime) <=1000) // do this loop for up to 1000mS
{
// code here
loopcount = loopcount+1;
endtime = millis();
}
serial.print (loopcount,DEC);

from this post:

I put the signal where it says // code here. It is a PWM. I run it for 5 seconds, but after that it does not “cut” the signal, the signal still exists… Any help?

Try “<=5000”, not 1000 (one second).
After the last Serial.print issue the code to stop the action.

Railroader:
Try “<=5000”, not 1000 (one second).
After the last Serial.print issue the code to stop the action.

Yes I did that, but still sends the signal… Anything else to check/modify? Thank you…

Your loop() runs over and over, starts the action again. Can that be the explanation?
If You only want one run, one cycle, after powering up the nano, end the sequence with a "while(1);"

Railroader:
Your loop() runs over and over, starts the action again. Can that be the explanation?
If You only want one run, one cycle, after powering up the nano, end the sequence with a "while(1);"

Or run it in setup().

It's not clear if you want a one time action, or a 5 second period of active pwm which can be called multiple times.

The while() loop is blocking, and your sketch is better done with an if() conditional if you want to do other things in loop(). Here is an example where a button press starts the pwm, and the button is responsive while the led is lit. Pressing the button while the led is lit will start another 5 second period.

unsigned long startTime;
boolean timing;
unsigned long timeOut = 5000;
const byte LED = 6;
const byte inputPin = 2;
byte inputState = HIGH;
byte lastInputState = HIGH;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(inputPin, INPUT_PULLUP);
}

void loop()
{
  lastInputState = inputState;
  inputState = digitalRead(inputPin);
  
  //detect button press with INPUT_PULLUP
  if(inputState == LOW && lastInputState == HIGH)
  {
    analogWrite(LED, 25);//10% dutyCycle pwm
    startTime = millis();
    timing = true;
  }

  if (timing == true)
{
  if (millis() - startTime >= timeOut)
    {
      analogWrite (LED, 0);//turn off pwm
      timing = false;
    }
  }
}

Something like this maybe ...

void loop() {
  if (permissionToWork == true) {
     if (millis() - startMillis >= durationMillis) {
        permissionToWork = false;
     }
   }
   if (permissionToWork == true) {
     // do stuff
   }

   // do other stuff that is not time limited

}

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

...R

alex5678:
I work on a Arduino nano. I want to send a signal for 5 seconds and then stop. How do I do this? If I use delay() I assume it doesn’t work because with this command I cannot do something in parallel…

a bit more information is needed.
is the 5 second part, to be run once for the life of the chip, never to be run again ?
or 5 seconds, every time power is regained after a loss of power ?
is the 5 second part to be run after a signal, like turning the light on when you walk in the room or beep for 5 seconds after the toaster is done ?