Can I execute 2 loops at the same time?

Hi Everyone!

Right now I am trying to make a project that monitors the changes in temperature while controlling the input supply of a certain device . But the two are not connected to each other meaning they are independent loops in my program. However when I am trying to control the input of a certain device, I could not continue monitoring the temperature and it stopped at the last temperature it got.

Is it possible to still monitor the temperature even during controlling the input of my device?

I am using Arduino UNO and the idea is I will just use on Arduino for this project.

Thank you in advance for answering! :slight_smile:

Yes what you describe is possible if the code is written correctly, but not if you use delay() or while loops. Post the code you're trying to use and I'm sure some will be able to help.

Steve

Welcome to Arduino land.

Not at the same time, but within μS of each other, yes.

Always show us your current compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

Show us a good schematic of your circuit.
Show us a good image of your wiring.

Hi. Thank you for your responses.

So what I’d like to happen is I am implementing this code:

void ACShutterMainLoop() {
  digitalWrite(enterButtonState, LOW);
  digitalWrite(exitButtonState, LOW);
  digitalWrite(setupButtonState, LOW);
 
  int i=1;
  while(i) {
   delay(200);
   enterButtonState = digitalRead(enterButton);
   exitButtonState = digitalRead(exitButton);
  
    if (enterButtonState == HIGH) {
      Serial.print("Enter Button pressed :) \n ");
      int totaltime;
              
            do { 
              digitalWrite(TurnONLED, HIGH);
              delay(TurnOnTime * 1000);
              digitalWrite(TurnONLED, LOW);
              digitalWrite(TurnOFFLED, HIGH);
              delay(TurnOffTime * 1000);
              digitalWrite(TurnOFFLED,LOW);
              totaltime = totaltime + TurnOnTime + TurnOffTime;
              Serial.print("\nTotal Time:");
              Serial.print(totaltime);
              Serial.print("\n");
            } while(totaltime + TurnOnTime + TurnOffTime <= ShutterDuration);
      
      i = 0;
      delay(5000);
    }
    else if (exitButtonState == HIGH) {
      Serial.print("Exit to Temp Monitoring\n");
      i = 0;
      delay(2000);
      TempMonitorLoop(); 
    }
    else {
      idleCounter = idleCounter++;
      Serial.print("Idle Timer: ");
      Serial.print(idleCounter);
      Serial.print("\n");
      delay(1000);
            if (idleCounter == 60){
              Serial.print("Idle Time reached maximum.\n");
              TempMonitorLoop();
            }
            else{
              i = 1;
            }
    }
  }
}

… while this is happening on the background:

void TempMonitorLoop() {
  int b = 1;
  while (b) {
    delay(200);
    TempSensorVal = analogRead(TempSensorPin);
    delay(100);
    temp = ((((TempSensorVal / 1024) * 5) - 0.5) * 100);

    lcd.begin(16, 2);
    lcd.print("Temp is: ");
    lcd.print(temp);

    if (temp <= temparray[0])
    {
      //    lcd.setCursor(0,1);
      //    lcd.print("Cool Temp");
      Serial.print(temp);
      Serial.print(" => Cool Temp\n");
      analogWrite(10, 0);  //analogwrite(PIN,Value)
    }
    else if (temp >= temparray[0] && temp <= temparray[1])
    {
      Serial.print(temp);
      Serial.print("  => Ambient Temp\n");
      analogWrite(10, 50);
    }
    else if (temp >= temparray[1] && temp <= temparray[2])
    {
      Serial.print(temp);
      Serial.print("  => Humid Temp\n");
      analogWrite(10, 100);
    }
    else if (temp >= temparray[2] && temp <= temparray[3])
    {
      Serial.print(temp);
      Serial.print("  => Hot Temp\n");
      analogWrite(10, 180);
    }
    else if (temp >= temparray[3])
    {
      Serial.print(temp);
      Serial.print("  => hot hot Temp\n");
      analogWrite(10, 254);
    }
    setupButtonState = digitalRead(setupButton);
    delay(200);

    if (setupButtonState == HIGH) {
      SetUpLoop();
      b = 0;
    }
    else {
      b = 1;
    }
    delay(5000);
  }
}

Some of your code does not make sense to me. Can you explain what these lines are intended to do?

  digitalWrite(enterButtonState, LOW);
  digitalWrite(exitButtonState, LOW);
  digitalWrite(setupButtonState, LOW);

It would be better to post both your sketches in their entirety.

Please post the code you wrote when you had combined the two parts and discovered there was a problem. There's no real point us looking at fragments of code that aren't what you were running.

But you're definitely going to need to get rid of all those delays. A delay() tells the Arduino to sit there doing nothing at all until the delay has finished whereas what you want in that gap in activity is for it to go off and do another task, like monitoring the temperature.

State machines and timing using millis() are going to be the keys to your success.

Steve