How to access variable in a loop as it changes?

I am currently working on a 4 digit 7 segment display that displays the seconds in real time. To make the number on the display change as time goes on I am using a for loop that increases a variable by one for every second. I want to use this variable in real time, but the scope seems to be wrong so I cannot access the variable. I have tried to intialize a variable outside of the for loop scope, but when I try to use that variable it only gives me the last number as the loop ends, not the variable as it changes in the loop. Here is my code.

int sec;
int x = 0;

for(sec = 0; sec<10; sec++)
{
delay(1000);
x = sec;
}

I want to display “x” on the display in real time as it changes due to the loop, how can I do this?

Thanks in advance.

I have tried to intialize a variable outside of the for loop scope, but when I try to use that variable it only gives me the last number as the loop ends, not the variable as it changes in the loop. Here is my code.

There is nothing wrong with that code. The problem, therefore, is in the code you didn't post.

[codevoid loop()
{
  int sec;
  for(sec = 0; sec<10; sec++)
  {
    delay(1000);
  }
  
  if(sec==10)
  {
    sec = 0;
  }
  
  int tenth;
  for(tenth = 0; tenth<10; tenth++)
  {
    delay(1000);
  }
  
  if(tenth==10)
  {
    tenth = 0;
  }
  
  for(int x = 0; x<=100; x++)
  {
    digitalWrite(dig1,HIGH);
    digitalWrite(dig2,HIGH);
    digitalWrite(dig3,HIGH);
    digitalWrite(dig4,LOW);
    pickNumber(sec);
    delay(5);
    
    digitalWrite(dig1,HIGH);
    digitalWrite(dig2,HIGH);
    digitalWrite(dig3,LOW);
    digitalWrite(dig4,HIGH);
    pickNumber(tenth);
    delay(5);
  
}
]

That is the rest of my loop. Basically it quickly refreshes the 3 and 4 digit of the display to show a timer, ( 0, 1 , …10, etc). I want the display to show the seconds by using the variables in the For loops above ( they change every second). The problem I have run into is that I can only get the final number from the For loops ( 10 in this case) but I want to use the variable as it changes throughout the loop( in the loop it goes 1, 2, 3…just like seconds) not just the final product of the loop. Hopefully I am explaining this well enough. Please help lol :slight_smile:

Hopefully I am explaining this well enough.

No, I’m afraid not. For one thing, all those delays have got to go.

You can update your clock/display in the for loop(s).

  int sec;
  for(sec = 0; sec<10; sec++)
  {
    delay(1000);
  }
  
  if(sec==10)

When the if test occurs, there is no way for sec to be anything other than 10. So, the if test is silly.

  int tenth;
  for(tenth = 0; tenth<10; tenth++)
  {
    delay(1000);
  }
  
  if(tenth==10)

The same issue occurs here.

You should look at the blink without delay example to see how to do something at a specific time with needing to delay().

Imagine YOU are changing cards on a rack, where the cards have numbers 0 to 9 on them, and there are places for two cards. You are to update the value displayed once an hour. You could put up a set of cards, and turn on a one hour timer, and stand and stare at it until the time was up, when you’d change the cards, and start the timing process over again.

Or, you could put up a set of cards, and record what time it was. Periodically, you’d see if it was time to change the cards. If so, you’d change them. If not, you’d go back to watching the ball game and drinking beer (or whatever).

The Arduino is good at watching ball games and drinking beer. No, wait, I mean that the Arduino is good at checking periodically (that would be every pass through loop()) to see if it is time to do something (now minus then is greater than or equal the time between events) and doing it, or not, as appropriate.

if(sec==10)

At this point, what other value could "sec" have?

When you are in a for loop, only the code in that for loop is executed, until the exit condition has been met. Nothing outside that for loop gets executed during that time.

Alright thanks everyone for all your help! I guess ill have to tweak my code quite a bit.

jraskell: When you are in a for loop, only the code in that for loop is executed, until the exit condition has been met. Nothing outside that for loop gets executed during that time.

Mostly true, unless you are using interrupts. If you are using interrupts, the interrupt could happen while you are executing inside of the loop, or not.

The Arduino is good at watching ball games and drinking beer.

That's classic!