exiting a loop after specified time

ive been poking around with a LED matrix using my Arduino mega 2560, and found that while i can use one loop to draw one character to the matrix by poking the pins with HIGH and LOW, that after a specified amount of time i want that loop to exit so that i am able to draw another character may probably attempt to scroll it later, anyways, whats the easiest or cleanest method to exiting a loop after a specified time? i looked at the doc pages about the 'while' loops but it kind didnt make much sense to me

Use millis() to record the time at which you started the loop. In the loop condition, check if millis() minus that time is greater than the time you're looking for it to last.

so to do that i would need to do something like:

void setup() {
some code in here 
}


Void timer(){
timer=millis();

void loop() {
if timer = millis(1000){
code to next code block
}
else
{
//repeat loop till correct time has hit
}
}

though im 100% sure i have it wrong

okay, so after fiddling and looking at various peices of code i some things half working, the only problem i have is that after i display the first character for 8 seconds, the display goes blank and then i want to display another character, but i cannot have 2 loop statements? how can i bypass this?

Loop is a function not a statement. Look at the while statement for what you want.

Use == in an if or a while

There is no need to have an else if it does nothing, it is optional

im 100% sure i have it wrong

Mee too :wink:
Your code looks Basic inspired…

Every sketch has two methods:
setup() { … }
loop() { … }

Usually, you should use this everlasting loop for everything to be repeated.

…after a specified amount of time i want that loop to exit so that i am able to draw another character may probably attempt to scroll it later, anyways, whats the easiest or cleanest method to exiting a loop after a specified time?

Rather have variables telling you in which state you are, currently.
As a sample:

unsigned long MAXTIME = 60000; // timeout (milliseconds) for RUNNING state


#define NORMAL 0
#define RUNNING 1

byte state;  // or try enum if that's convenient for you 

unsigned long starttime; 

void loop () 
{
   unsigned long now = millis();  // always a good idea to have a consistent value throughout this loop cycle

   boolean startcondition = CheckIfWeStartNow(); 
   if ( startcondition == true )
   { 
      starttime = now;
      state = RUNNING;
   }

   if ((state == RUNNING) && (now - starttime > MAXTIME))
   {
      // time for state expired
      state = NORMAL;
   }

   if (state == NORMAL)
   {
       // do whatever has to be done in this state
   }

   if (state == RUNNING) 
   {
       // do whatever has to be done while timer is running
   }
      
   // etc.   
   
}


boolean CheckIfWeStartNow()
{   
   return false; // just to make code compile ok
}

edit: minor

Grumpy_Mike:
Loop is a function not a statement.
Look at the while statement for what you want.

Use == in an if or a while

There is no need to have an else if it does nothing, it is optional

so it might go like:

#define TIMER_LIM 5000

void setup() {
//Anode pins
pinMode(22, OUTPUT); 
  pinMode(23, OUTPUT);
 pinMode(24, OUTPUT);

//Cathode pins
 pinMode(40, OUTPUT);  
  pinMode(41, OUTPUT);
   pinMode(42, OUTPUT);
start = millis();
}


void loop() {
if (millis() < TIMER_LIM) {
//pin codes to show the letter H on the matrix
}
else
{
//time has elapsed, display second character and alter pin codes
}
}

it compiles fine but i do not think itll work right

michael_x:
Mee too :wink:
Your code looks Basic inspired…

Every sketch has two methods:
setup() { … }
loop() { … }

Usually, you should use this everlasting loop for everything to be repeated.

Rather have variables telling you in which state you are, currently.
As a sample:

unsigned long MAXTIME = 60000; // timeout (milliseconds) for RUNNING state

#define NORMAL 0
#define RUNNING 1

byte state;  // or try enum if that’s convenient for you

unsigned long starttime;

void loop ()
{
  unsigned long now = millis();  // always a good idea to have a consistent value throughout this loop cycle

boolean startcondition = CheckIfWeStartNow();
  if ( startcondition == true )
  {
     starttime = now;
     state = RUNNING;
  }

if ((state == RUNNING) && (now - starttime > MAXTIME))
  {
     // time for state expired
     state = NORMAL;
  }

if (state == NORMAL)
  {
      // do whatever has to be done in this state
  }

if (state == RUNNING)
  {
      // do whatever has to be done while timer is running
  }
     
  // etc.  
 
}

boolean CheckIfWeStartNow()
{  
  return false; // just to make code compile ok
}



<sub>edit: minor</sub>

i used to write software in visual basic S: so anything that is C oriented is quite tough for me to grasp, loop statements ad a few other things i always had trouble wrapping my head around but ill try and wrap my head around this.

i do not think itll work right

Perhaps it works right for 5 seconds after reset ...

Always compare time limits with a difference between the current millis() and a remembered start time.

michael_x: Perhaps it works right for 5 seconds after reset ...

Always compare time limits with a difference between the current millis() and a remembered start time.

uhh, hate to be a pest but how to i compare or check what the current timer is at in an 'if' statement before switching and then moving onto the next code block?

any form of C language has never been a strong point for me S:

perhaps you check and try to understand usage of now, millis() and starttime in my sample ?

michael_x: perhaps you check and try to understand usage of now, millis() and starttime in my sample ?

im understanding how your example checks what state it is in and then reverts to normal operation after the time runs out, if i can read right (and hopefully i dont need glasses) your setting the max time right at the top and checking to see if that value has been hit in code before reverting to normal from running, and for the majority of it, i think i get the involvement of boolean.