How to create a delay without pausing the code?

Paul H asked Aug 04,2020
0

I want to make a pin HIGH for a specific duration of time, but I want the code to keep running while the pin is high, how do I do this?


  • Your Answer(4)
Upload photo
You can only upload 5 files in total. Each file cannot exceed 2MB. Supports JPG, JPEG, GIF, PNG, BMP
0 / 10000
  • 0

    //for a little example: long = ref1; if (millis() - ref1 >= 15000) //for 15 seconds... { ref1 = millis() ; digital.Write( pin no, HIGH); } else { whatewer you want} //Is not necessary/optional

    Paul H 2020-08-05 09:14:32Reply

    Ok, thanks for the example.

    Reply
  • 0

    What are you working with? An MCU of some sort, sounds like? If so, you need to use your MCU's interrupt system, in combination with a timer peripheral. You want to set a timer to go off in the future, interrupt the CPU and then you would set the pin to be low from the interrupt handler. Some timer peripherals can do this automatically too. It might help to know what is your exact platform, but most MCUs do have both a timer and an interrupt system.

    Paul H 2020-08-04 11:27:30Reply

    Thanks a lot. I'll try 'millis' method. https://arduinogetstarted.com/faq/how-to-use-millis-instead-of-delay

    Reply
Browse other questions tagged or ask your own question.
List of Questions
pause
Ranking Name Answers
1 PCBWay Team 11
2 Engineer 1
3 Engineer 1
4 Franz Krenn 1