# How to break from a FOR to do another task?

Hi Yall, I have designed a 20 led binary clock. I am writing the code for it now and I have accomplished a for loop to count to 9 for the units of seconds. I do not know how to break from the loop. A brief overview follows,
0
0 0
0 0
0 0

the column on the left represents tens of seconds (1,2,4). The column on the right will be units of seconds(1,2,4,8) with a combination of lights being grounded I will display the current time in seconds. My logic for programming flows like so.

Start counting in units of seconds to 9, If the number is less than 9, Add one.

Is 1 less than 9? YES, NO. IF YES ADD 1

Is 2 less than 9? YES,NO. IF YES ADD 1

is 3 less than 9? YES, NO. IF YES ADD 1

I do this until it I hit 9. Once the code has reached 9, It starts all over again back at 0. I desire for the code to be instructed once it has reached 9 in units of seconds to add 1 to the units of tens of seconds column. After adding a 1 to tens of seconds I want it to continue counting to 9 in units of seconds until it has reached 9 again. After it has reached 9 in units of seconds I want it to turn on the next bit in tens of units( now 2 ) will be displayed, US will continue to count to 9 again and so on. I dont know how to go about this. My code is as follows.

``````int us = 0;
int tus = 0;
int um = 0;
int tm = 0;

void setup ()
{
pinMode (0, OUTPUT);
pinMode (1, OUTPUT);
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
pinMode (5, OUTPUT);
pinMode (6, OUTPUT);
pinMode (7, OUTPUT);
pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);
pinMode (12, OUTPUT);
pinMode (13, OUTPUT);
digitalWrite (0, LOW);
digitalWrite (1, LOW);
digitalWrite (2, LOW);
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
digitalWrite (6, LOW);
digitalWrite (7, LOW);
digitalWrite (8, LOW);
digitalWrite (9, LOW);
digitalWrite (10, LOW);
digitalWrite (11, LOW);
digitalWrite (12, LOW);
digitalWrite (13, LOW);
delay(1000);
digitalWrite (0, HIGH);
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
digitalWrite (4, HIGH);
digitalWrite (5, HIGH);
digitalWrite (6, HIGH);
digitalWrite (7, HIGH);
digitalWrite (8, HIGH);
digitalWrite (9, HIGH);
digitalWrite (10, HIGH);
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
delay(1000);
}

void loop ()
{
for (us=0; us < 9; us++)
{
if (us == 0)
{
digitalWrite(0, HIGH);

digitalWrite(1, HIGH);

digitalWrite(2, HIGH);

digitalWrite(3, HIGH);

delay(1000);
}

else if (us ==  1)
{
digitalWrite(0,LOW);

digitalWrite(1,HIGH);

digitalWrite(2,HIGH);

digitalWrite(3,HIGH);

delay(1000);
}

else if(us == 2)
{
digitalWrite(0,HIGH);

digitalWrite(1,LOW);

digitalWrite(2,HIGH);

digitalWrite(3,HIGH);

delay(1000);
}
else if (us ==  3)
{
digitalWrite(0,LOW);

digitalWrite(1,LOW);

digitalWrite(2,HIGH);

digitalWrite(3,HIGH);

delay(1000);
}

else if (us == 4)
{
digitalWrite(0,HIGH);

digitalWrite(1,HIGH);

digitalWrite(2,LOW);

digitalWrite(3,HIGH);

delay(1000);
}
else if (us ==  5)
{
digitalWrite(0,LOW);

digitalWrite(1,HIGH);

digitalWrite(2,HIGH);

digitalWrite(3,LOW);

delay(1000);
}

else if (us == 6)
{
digitalWrite(0,HIGH);

digitalWrite(1,LOW);

digitalWrite(2,LOW);

digitalWrite(3,HIGH);

delay(1000);
}
else if  (us ==  7)
{
digitalWrite(0,HIGH);

digitalWrite(1,LOW);

digitalWrite(2,LOW);

digitalWrite(3,LOW);

delay(1000);
}

else if (us == 8)
{
digitalWrite(0,HIGH);

digitalWrite(1,HIGH);

digitalWrite(2,HIGH);

digitalWrite(3,LOW);

delay(1000);
}
else if (us == 9)
{
digitalWrite(0, LOW);

digitalWrite(1, HIGH);

digitalWrite(2,HIGH);

digitalWrite(3, LOW);

delay(1000);
}

}
}
``````

Re-organize a little:

``````void loop(){
for (min=0; min<10; min=min+1){
switch(min){
case 0:
digitalWrite() ...
break;
:
:
case 9:
digitalWrite()...
delay(1000);
min=0;
hour=hour+1;
switch(hour){
case 0:
digitalWrite() ...
break;
case 11:
digitalWrite() ...
hour = 0;
break;
} // end hour
} // end min
} // end loop
``````

a break command is what will leave the loop and return shorty after another task is performed?

This feels like an awfully complicated way to solve the problem.

Can't you just maintain an incrementing number of seconds (for example, millis()/1000 or (millis()/1000) %60)) and just use div and mod to calculate the units and tens values? You don't say, but I guess this will be used to display values up to a minute with the three-bit tens field displaying values 0 - 5 and the four-bit units field displaying values 0 - 9. If you have some different scheme in mind it doesn't particularly affect the logic except that you need to implement whatever algorithm you want to determine the units and tens numbers to be displayed.

Having got the number, you can just loop through extracting each bit using bitRead() and use the result to set the corresponding output high or low. If you code this as a function you could easily use the same implementation for both fields i.e. pass in the value and array of pin numbers and get the pins set accordingly.

By the way, I wouldn't put the overall logic in a for loop at all - I'd control it using the non-blocking approach demonstrated in blink without delay, so that your sketch is capable of being extended to do other things at the same time in future.