How to run two loops simultanously?

this is counting time, two digits for minutes and 1 digit for seconds. first loop is for second digit, rest is for minute which can count until 99. but this code is not working. can you help me please? ( sorry for my bad english)

//SSD is Seven-Segment Display

void setup()
{
  for (int i = 0; i <= 19; i++)
    pinMode(i, OUTPUT); //Set all pins from 0 to 19 as OUTPUT
}
//Aşağıdaki satır, 0'dan 9'a kadar bir SSD'deki rakamlar için tüm ikili sayıları içeren diziyi oluşturma çabalarının ürünüdür :) (ssd =seven segment display)
const int number[11] = {0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010, 0b0000010, 0b1111000, 0b0000000, 0b0010000};


void loop()
{
      digitalWrite(14,LOW); //a
    digitalWrite(15,LOW);//b
    digitalWrite(16,LOW);//c
    digitalWrite(17,LOW);//d
    digitalWrite(18,LOW);//e
    digitalWrite(19,LOW);//f
    digitalWrite(20,HIGH);//g
    

    delay(1000);//1 saniye bekletiyoruz

// 1 olması için sadece b ve c yanık olması gerekir

    digitalWrite(14,HIGH); //a
    digitalWrite(15,LOW);//b
    digitalWrite(16,LOW);//c
    digitalWrite(17,HIGH);//d
    digitalWrite(18,HIGH);//e
    digitalWrite(19,HIGH);//f
    digitalWrite(20,HIGH);//g

    delay(1000); //1 saniye bekletiyoruz


//2 rakamını oluşturabilmemiz için a,b,d,e,g harflerinin yakmamız gerekir 
    digitalWrite(14,LOW); //a
    digitalWrite(15,LOW);//b
    digitalWrite(16,HIGH);//c
    digitalWrite(17,LOW);//d
    digitalWrite(18,LOW);//e
    digitalWrite(19,HIGH);//f
    digitalWrite(20,LOW);//g

    delay(1000); //1 saniye bekletiyoruz



//3 rakamını oluşturabilmemiz için a,b,c,d,g harflerini yakmamız gerekir
    digitalWrite(14,LOW); //a
    digitalWrite(15,LOW);//b
    digitalWrite(16,LOW);//c
    digitalWrite(17,LOW);//d
    digitalWrite(18,HIGH);//e
    digitalWrite(19,HIGH);//f
    digitalWrite(20,LOW);//g

   delay(1000); //1 saniye bekletiyoruz



//4 rakamını oluşturabilmemiz için b,c,f,g harflerini yakmamız gerekir
    digitalWrite(14,HIGH); //a
    digitalWrite(15,LOW);//b.
    digitalWrite(16,LOW);//c.
    digitalWrite(17,HIGH);//d
    digitalWrite(18,HIGH);//e
    digitalWrite(19,LOW);//f.
    digitalWrite(20,LOW);//g.

   delay(1000); //1 saniye bekletiyoruz



// 5 rakamını oluşturabilmemiz için a,c,d,f,g harflerini yakmamız gerekir
    digitalWrite(14,LOW); //a
    digitalWrite(15,HIGH);//b.
    digitalWrite(16,LOW);//c
    digitalWrite(17,LOW);//d
    digitalWrite(18,HIGH);//e.
    digitalWrite(19,LOW);//f
    digitalWrite(20,LOW);//g

    delay(1000); //1 saniye bekletiyoruz



// 6 rakamını oluşturabilmemiz için a,c,d,e,f,g harflerini yakmamız gerekir
    digitalWrite(14,LOW); //a
    digitalWrite(15,HIGH);//b.
    digitalWrite(16,LOW);//c
    digitalWrite(17,LOW);//d
    digitalWrite(18,LOW);//e.
    digitalWrite(19,LOW);//f
    digitalWrite(20,LOW);//g

    delay(1000); //1 saniye bekletiyoruz
}

void loop()
{

  for (int tens = 0; tens < 10; tens++)

  {
    display_tens(tens);
  }
}

void display_tens(const int tens)
{
  int pin1, a, ones;     
  //pin1  1.SSD'nin pinleri ile alakalıdır.

  for (pin1 = 0, a = 0; pin1 < 7; pin1++, a++)
  {
    digitalWrite(pin1, bitRead(number[tens], a));
  }
  for (ones = 0; ones < 10; ones++)
  {
    display_ones(ones);
    delay(60000); 
    //300 ms periyotla sayılar bir bir artar.
  }
}

void display_ones(const int x)
{ int pin2, b;             
//pin2  2.SSD'nin pinleri ile alakalıdır.

  for (pin2 = 7, b = 0; pin2 <= 13; pin2++, b++)
  {
    digitalWrite(pin2, bitRead(number[x], b));

  }

}

Normally you cannot run two loops or two functions "in parallel" or simultaneously- this requires multi-threading. But I believe there is a threading library available that can help with this.

There is only one processor so only one piece of code running at a given time. But because it runs relatively fast, you can mimic doing multiple actions simultaneously for the user

Read about millis and doing multiple things at the same time

For example read this

Use delay(1000) only once in loop(), better not at all. Have second and minute counters and increment them every second. Then update the display. See BlinkWithoutDelay example.

Learn to use arrays, like the number[11] array holding the 7-segment digit codes.

Why one digit for seconds and two digits for minutes? Should it not be the other way around? So you can display from 0:00 till 0:59 9:59?

This does not mean anything; please give a better description of the problem. As I see the problem, you have two loop() functions and that is not allowed so the compiler will throw an error.

If you only want to count the time then simply use the millis() function call. That returns the number of milliseconds since the Arduino was turned on.
Then only when you want your digits to display take that time and construct your values from it.
So to get seconds divide the millis value by 1000.
To get this value from 0 to 59 take the modulus ( the % symbol) 60.
To get minuets divide by 60000 and take the modulus of how ever minuets you want.

how do you want the two loops keeping in sync?

Minutes are an exact multiplier of seconds. This multiplier is 60
So simply counting up seconds and each time the seconds-counter reaches 60
reset to zero and increment the minutes-counter by one.

minimal software RTC every tenth of a second a tenth-second counter increments by one

every time this counter reaches 10 reset to zero and increment seconds-counter by one

everytime the seconds-counter reaches 60 reset to zero and increment minutes-counter by one

every time the minutes-counter reaches 60 reset to zero and increment hours-counter

every time the hours-counter reaches 24 reset to zero

unsigned long RTC_Timer  = 0;
unsigned long MyTestTimer;

int RTC_Hour   = 0;
int RTC_Minute = 0;
int RTC_Second = 0;
int RTC_10nth_Seconds = 0;


boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

void PrintRTC_Data() {
  Serial.print(RTC_Hour);
  Serial.print(":");
  Serial.print(RTC_Minute);
  Serial.print(":");
  Serial.print(RTC_Second);
  Serial.println();
}


void SoftRTC() {
  if ( TimePeriodIsOver(RTC_Timer,100) ) {
    RTC_10nth_Seconds ++;
    if (RTC_10nth_Seconds == 10) {      
      RTC_10nth_Seconds = 0;    
      RTC_Second++;
      
      if (RTC_Second == 60) {
        RTC_Minute++;
        RTC_Second = 0;
      }
    }
    
    if (RTC_Minute == 60) {
      RTC_Hour++;
      RTC_Minute = 0;  
    }
  
    if (RTC_Hour == 24) {
      RTC_Hour = 0;
    }

      
    if ( TimePeriodIsOver(MyTestTimer,1000) ) {
      PrintRTC_Data();
    }  
    
  }
}


void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
}


void loop() {
  SoftRTC();
}

This is the counting part

Then you have that part of code that switches on/off the segments.

Does your exercise forces you to use directly a lot of IO-pins?

or are you allowed to use a 7-segment module with an interface-chip and are you allowed to use a library that has all the detail-code for displaying digits ready to use?

If yes I recommend using such a module

1,20 Euro = 11.32 Türk Lirası

best regards Stefan

I hope that, at the very least, the OP is "allowed" to use arrays and functions instead of repeated digitalWrite() calls within the loop()!

Try this:

void loop{
<code>
otherLoop();
}

void otherLoop(){
<code>
}

Due to the blocking nature of delay() inserting the TO's code inside loop and otherLoop does not work.
the loop that contains the delay(60000) will block for 60 seconds.

The code has to be modified to be non-blocking
best regards Stefan

I did not see those delays.

Yes, in this case using delays would break this thing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.