# multiple loops / functions

So, the project i am working on requires me to run 2 loops.

One that keeps track of time, and makes int for ones and tens, lest call it counter();
The counter() is a loop by itself and the delay in it is 1 second.

And the other loop who will (really quickly) read the data from counter() and display them. The second loop which is my void() loop needs to run really fast, with a delay of 5 miliseconds.

Which ever method i try it just runs one loop or the other. If i don't my counter() a loop than it runs once and continues with my loop() loop.

I have minimal programing experience and only did projects like led blinking or moving a simple servo.
All i need is someone to tell me how to run these 2 simultaneously.
I searched the web for the answer but couldn't find anything related to my problem.

To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

Robin2 has a great thread here:

I am sure the code will just confuse you, also i deleted most of if so i am back at the beginning.
The point is i have a counter(); function that basically add +1 to my int Count. The Count is split into First and Second which are basically ones and tens (example: Count 23 = First 3, Second 2 ; Count 83 = First 3, Second 8).
I get First and Seconds (ints) from counter functions but i need to use them in real time. Meaning, my void loop needs to constantly receive values from counter() and use them to do different tasks.

btw What i am trying to do is make my own code to display numbers from 0-99 on 7 segment display. But the circuit im using is different (and complicated). If you wanna analyse the code - HIGH is LOW, LOW is HIGH.

``````int Counter = 0;
int A = 6;
int G = 2;
int C = 4;
int B = 5;
int E = 3;
int F = 7;
int D = 8;

int First;
int Second;

int NumberCounter = 0; // Just ignore it

int Dis1 = 10;
int Dis2 = 11;
int Dis3 = 12;
int Dis4 = 13;

int DelayTime = 1000;

void setup() {

Serial.begin(9600);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(Dis1, OUTPUT);
pinMode(Dis2, OUTPUT);
pinMode(Dis3, OUTPUT);
pinMode(Dis4, OUTPUT);

digitalWrite(Dis1, HIGH);  // meaning they are turned off
digitalWrite(Dis2, HIGH);
digitalWrite(Dis3, HIGH);
digitalWrite(Dis4, HIGH);
}

void loop(){     // loop will display the numbers

}

void counter() {

Counter = Counter + 1;
if(Counter == 100){
Counter = 0;
}
delay(DelayTime);

First = Counter % 10 ;
Second = Counter / 10;

Serial.print("Counter ");
Serial.println(Counter);
Serial.print("First ");
Serial.println(First);
Serial.print("Second ");
Serial.println(Second);
Serial.println(" ");

counter();
}

//these are the numbers

void zero(){
digitalWrite(A, LOW); // 0
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
}
void one(){
digitalWrite(A, HIGH); // 1
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
}
void two(){
digitalWrite(A, LOW); // 2
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
}
void three(){
digitalWrite(A, LOW); // 3
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
}
void four(){
digitalWrite(A, HIGH); // 4
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
void five(){
digitalWrite(A, LOW); // 5
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
void six(){
digitalWrite(A, LOW); // 6
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
void seven(){
digitalWrite(A, LOW); // 7
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
}
void eight(){
digitalWrite(A, LOW); // 8
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
void nine(){
digitalWrite(A, LOW); // 9
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
``````

Alzairus:
The counter() is a loop by itself and the delay in it is 1 second.

The problem is "delay()".

You are NOT ALLOWED to use

• delay() or
• busy waiting loops
in a program that has to do "multitasking".

Here is a small example sketch, that handles "three tasks at the same time".

1st task: oneSecondCounter() ==> count up by one every second
2nd task: blinker() ==> blink an LED in a rhythm of 1 second on and 1 second off
3rd task: counterPrintIfChangedBy3() ==> every time the counter has counted up by three, print to Serial

``````#define LED 13

unsigned long counter;

void oneSecondCounter()
{
static unsigned long lastCountTime;
if (millis()-lastCountTime>=1000)
{
counter++;
lastCountTime+=1000;
}
}

{
digitalWrite(LED, (millis()/1000)%2);
}

void counterPrintIfChangedBy3()
{
static unsigned long lastCounter;
if (counter-lastCounter>=3)
{
Serial.print("Counter changed to: ");
Serial.println(counter);
lastCounter=counter;
}
}

void setup() {
Serial.begin(9600);
pinMode(LED,OUTPUT);
}

void loop() {
oneSecondCounter();
counterPrintIfChangedBy3();
}
``````

You see:

• no "delay()" used anywhere
• no "busy waiting" at any place in the code

Every function must be "cooperative" and not use more time than needed to do the job.

If you are doing it that way, the loop() function and every task is running several thousand times per second.

Thanks a lot for the reply, it really helped.
I new the delay made a problem but had no other idea how to make a counter, thx.