Is it possible to store a value of time while you're inside a loop? Entertain the condition:

if (condition && value > 0)
timeTrans = millis() + 2000;
timeTrans - millis() = value;

basically i want it to begin counting down from 2 to 0 no matter what time it is at, but the problem is if it's inside the loop tineTrans will update as millis counts up, and value will always be 2000 and will never count down to 0. And the condition stated in the if function cannot be called in void setup(), so there's no hope in that direction. I'd post my code but it's a little long, if it will help find a solution i will post it and point around to what i'm working on.

Also, the condition is met randomly. I do not know when, or at what time/millis() it will be met at.

I also tried using for loops as well but it counts wayyyy too fast for it to be viable, and obviously i cant delay it while its inside a void loop(). Is there any other way i can go about with this?

I've been looking at the beginners guides and here's where i'm at right now, this is about as clear and concise as this question is going to get

is there any way to make "time-2000" a constant variable in this example?

unsigned long time, timeNow, timePast, timeStart, value;
unsigned long period;

void setup() {
  timeStart = 0;


void loop() {
  time = millis();
  if (time > 3000) //Using time bigger than 3 sec as a condition substitute to trigger my event
  period = time-2000; //Since in my example, we'll know at what time the condition was triggered (once it's triggered), I set the "period" to that.
    if ((time - timeStart) > period) //aaaaannndd the attempt
      timePast = period;

  Serial.print("Time: "); //debugging
  Serial.print(" please work ");

that "time-2000" just needs to be turned into a constant somehow. After that, i'll be golden. Anybody know a way around this?

  • 2
    this is about as clear and concise as this question is going to get” Then it's hopeless. You must make the question clearer if you want to have any chance of getting a good answer. Start by telling us what you are trying to accomplish. – Edgar Bonet Apr 8 '18 at 12:59

Browse other questions tagged or ask your own question.