How to simulate millis() starting counting at the start of a function?

Hi there,

I'm working on a timed video game using an lcd, and I was wondering if there was a way to simulate the millis counter starting when a function is called? Right now millis is counting from when the Arduino was booted up (as it should).

Take the millis() time when the function starts and put in a variable.
Any time you want the time since the start, subtract the variable from the current value of millis()

2 Likes

unsigned long

How do i take the millis time? Just millis();?

Yup. Straight into an unsigned long.

Alright, so i wrotetimepassed = millis(); at the start of the function, and then currentTime = millis()-timepassed; right before it checks if current time is less than 20 seconds (the game gives you 20 seconds to complete it). Is this correct?

Here is the rest of the function just in case:

void game(){
  intro(); 
  intro1();
  intro2();
  timepassed = millis();
  gameon = 1;
  while(gameon = 1){
  item_line = random(1, 2 +1);
      item_column = random(left_column_boundary, right_column_boundary +1);
      redraw();
      currentTime = millis()-timepassed;
if ((currentTime <= 20000)) {
          while(!((player_line == item_line)  &&  (player_column == item_column)))
          {
            if(currentTime <= 20000){
            redraw();
            // MOVE UP
            
            if(analogRead(A0+0) > 600){
                if(player_line == bottom_line_boundary){
                    player_line += 1;
                    lcd.clear();
                    redraw();

                }

            }
            // MOVE DOWN
            if(analogRead(A0+0) < 300){
                if(player_line == top_line_boundary){
                    player_line += -1;
                    lcd.clear();
                    redraw();

                }

            }
            // MOVE LEFT
            if(analogRead(A0+1) > 400){
                if(player_column > left_column_boundary){
                    player_column += -1;
                    lcd.clear();
                    redraw();

                }

            }
            // MOVE RIGHT
            if(analogRead(A0+1) < 300){
                if(player_column < right_column_boundary){
                    player_column += 1;
                    lcd.clear();
                    redraw();

                }

            }
            }
            else{
              ending();
              gameon = 0;
              lcd.clear();
              return;
            }

          }
          score += 1;
      }
   
        else{
          ending();
          gameon = 0;
          lcd.clear();
          return;
        }
}
}

Oops

Oh, should i place the millis check under that? Or what do you mean...

I mean "I don't think you want to do that forever. Oops."

What do you mean? I have it loop until the time goes over 20000 milliseconds. Should I change it?

Look carefully at the code I highlighted, and see if you think it looks sensible
Hint: I don't

Sorry but, i dont seem to understand. You're being really vague here. Should I change the value it checks for in the while statement...? or should i ditch using while altogether

If I say that = is not the same as ==, does that help?

(If you want to get ahead in software development, get used to solving problems from vague hints)

While set gameon to 1? Makes no sense!
= is for setting a variable
== is for comparing

Ah yes, there we go, I fixed it! Thanks for noticing that. I also fixed where timepassed updates... but for some reason current time is now equal to 0 constantly...

Heres the updated code

void game(){
  intro(); 
  intro1();
  intro2();
  gameon = 1;
  while(gameon == 1){
    timepassed = millis();
  item_line = random(1, 2 +1);
      item_column = random(left_column_boundary, right_column_boundary +1);
      redraw();
      currentTime = millis()-timepassed;
if ((currentTime <= 20000)) {
          while(!((player_line == item_line)  &&  (player_column == item_column)))
          {
            if(currentTime <= 20000){
            redraw();
            // MOVE UP
            
            if(analogRead(A0+0) > 600){
                if(player_line == bottom_line_boundary){
                    player_line += 1;
                    lcd.clear();
                    redraw();

                }

            }
            // MOVE DOWN
            if(analogRead(A0+0) < 300){
                if(player_line == top_line_boundary){
                    player_line += -1;
                    lcd.clear();
                    redraw();

                }

            }
            // MOVE LEFT
            if(analogRead(A0+1) > 400){
                if(player_column > left_column_boundary){
                    player_column += -1;
                    lcd.clear();
                    redraw();

                }

            }
            // MOVE RIGHT
            if(analogRead(A0+1) < 300){
                if(player_column < right_column_boundary){
                    player_column += 1;
                    lcd.clear();
                    redraw();

                }

            }
            }
            else{
              ending();
              gameon = 0;
              lcd.clear();
              return;
            }

          }
          score += 1;
      }
   
        else{
          ending();
          gameon = 0;
          lcd.clear();
          return;
        }
}
}

The reason for this is that you have made one of the classic C/C++ blunders that everyone makes, repeatedly at first. It tends to be a quicker path to training your eyes to see it if you're helped to find it yourself.

1 Like

Doesn't this need to be before the while loop?

It was there before, but then the screen was updating the time only when you scored, so i double guessed myself. I thought it would fix that problem but now that i've moved it back im not sure whats causing it to only update when you score

Oh, I just figured it out! Just needed to make the redraw and time update more frequently. Thanks for all the help everyone!

This is why answers tend to be vague. You don't learn anything if someone here does all your coding for you.