Two loops possible?

I have a arduino mega connected to a nextium display.

Everything works very fine. I have 2 component: waveform and LED on/off with beats per minute) I noticed that when I alter the number of beats per minute ALL the data is being delayed or speed up. This is because the two components follow eachother up. Is it possible to make them working as parallel instead of follow up? Can i put the led blinking in a second loop as example?

void bt0PopCallback(void *ptr);                               // Release event for dual state button bt0
{
}
  uint32_t alarmTswitch = 0;                                // alarmbutton - Create variable to store value we are going to get
  bt0.getValue(&alarmTswitch);                              // alarmbutton - Read value of dual state button to know the state (0 or 1)

  uint32_t alarmTset = 0;                                   // alarmset temperature- Create variable to store value we are going to get
  n4.getValue(&alarmTset);                                  // alarmset temperature- Read value of dual state button to know the state (0 or 1)

  uint32_t rateSet = 0;                                   // Rate set - Create variable to store value we are going to get
  n5.getValue(&rateSet);                                  // Rate set - Read value of dual state button to know the state (0 or 1)
    
    
      if (temp >= alarmTset && alarmTswitch == 0){                // controleer of temperatuur warmer is dan 26 graden
        digitalWrite(buzzer, HIGH);                         // als de temperatuur warmer is dan 26 graden zet LED aan
                    
    } else {
        digitalWrite(buzzer, LOW);                           // als de temperatuur niet warmer is dan 26 graden zet LED uit

    }

      if (temp >= alarmTset){                // controleer of temperatuur warmer is dan 26 graden
                                            // als de temperatuur warmer is dan 26 graden zet LED aan
        Serial2.print("n3.pco=63488");                      // maak kleur rood
        Serial2.write(0xff);
        Serial2.write(0xff);
        Serial2.write(0xff);

    } else {
                                                    // als de temperatuur niet warmer is dan 26 graden zet LED uit
        Serial2.print("n3.pco=65535");                       // maak kleur wit
        Serial2.write(0xff);
        Serial2.write(0xff);
        Serial2.write(0xff);
    }

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay((60/10*1000)/2);                       // 60 /minute
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay((60/10*1000)/2);                      // 60 /minute



}
    

... and why are there delay()s in there ?
Read up on millis() timing and non-blocking methods.

All these tutorials are relevant to what you are doing:

Using millis for timing
Demonstration for several things at the same time
Finite state machine tutorial

the above doesn't look compilable
below is proper format

void bt0PopCallback(void *ptr)     // Release event for dual state button bt0
{
  uint32_t alarmTswitch = 0;                                // alarmbutton - Create variable to store value we are going to get
  bt0.getValue(&alarmTswitch);
  ...
}

as others have suggested, millis() can be used to check if a time period has expired without blocking

Thanks guys. Your feedback is overwhelming! I will read more about the given suggestions and will let you know what hapenned

Hi guys,

it worked! I just inserted the blinking example without delay. Now it works smoothly. I have another problem. The blinking rate is connected to a editable number field n5 on nextion display. the specific code is:

uint32_t rateSet = 0;                                   
n5.getValue(&rateSet);   
const long interval = (60/rateSet*1000)/2;  

Because of this the led will only blink when I am on the specific page where n5 is displayed. Is it possible to read n5 all the time? So it doesnt matter on which page you are on the nextion.

this is the code in total:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Nextion.h>
 
#define SENSOR_PIN 28
OneWire oneWire(SENSOR_PIN );
DallasTemperature sensors(&oneWire);

NexWaveform s0 = NexWaveform  (3, 2, "s0");
NexNumber n3 = NexNumber      (3, 6, "n3");
NexNumber n4 = NexNumber      (0, 7, "n4");
NexNumber n5 = NexNumber      (3, 7, "n5");
NexDSButton bt0 = NexDSButton (0, 6, "bt0");

int buzzer = 11;

NexTouch *nex_Listen_List[] =
{
&bt0,
&n4,
&n5,
NULL
};



// HEARTRATE constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// HEARTRATE Variables will change:
int ledState = LOW;             // ledState used to set the LED

// HEARTRATE Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated






 
void setup() {
  // HEARTRATE set the digital pin as output:
  pinMode(ledPin, OUTPUT);

  
  pinMode(buzzer,OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  Serial2.begin(9600);
  sensors.begin();
  nexInit();

}
void loop() {
  sensors.requestTemperatures();
  float temp = sensors.getTempCByIndex(0);
  
    
  s0.addValue(0, temp);                                       // weergave temperatuur als wavevorm
  n3.setValue(temp);                                          // weergave temperatuur als nummer

  delay( 1 );
  
void bt0PopCallback(void *ptr);                               // Release event for dual state button bt0

  uint32_t alarmTswitch = 0;                                // alarmbutton - Create variable to store value we are going to get
  bt0.getValue(&alarmTswitch);                              // alarmbutton - Read value of dual state button to know the state (0 or 1)

  uint32_t alarmTset = 0;                                   // alarmset temperature- Create variable to store value we are going to get
  n4.getValue(&alarmTset);                                  // alarmset temperature- Read value of dual state button to know the state (0 or 1)

  uint32_t rateSet = 0;                                   // Rate set - Create variable to store value we are going to get
  n5.getValue(&rateSet);                                  // Rate set - Read value of dual state button to know the state (0 or 1)
    
    
      if (temp >= alarmTset && alarmTswitch == 0){                // controleer of temperatuur warmer is dan 26 graden
        digitalWrite(buzzer, HIGH);                         // als de temperatuur warmer is dan 26 graden zet LED aan
                    
    } else {
        digitalWrite(buzzer, LOW);                           // als de temperatuur niet warmer is dan 26 graden zet LED uit

    }

      if (temp >= alarmTset){                // controleer of temperatuur warmer is dan 26 graden
                                            // als de temperatuur warmer is dan 26 graden zet LED aan
        Serial2.print("n3.pco=63488");                      // maak kleur rood
        Serial2.write(0xff);
        Serial2.write(0xff);
        Serial2.write(0xff);

    } else {
                                                    // als de temperatuur niet warmer is dan 26 graden zet LED uit
        Serial2.print("n3.pco=65535");                       // maak kleur wit
        Serial2.write(0xff);
        Serial2.write(0xff);
        Serial2.write(0xff);
    }



  // HEARTRATE constants will change bij input n5:
  const long interval = (60/rateSet*1000)/2;           // interval at which to blink (milliseconds)

  // HEARTRATE here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);


  }
    


}
    

  

I am not familiar with the way the Nextion official library works, but it does not surprise me that you ask about reading something from the display 'all the time'. You need to save the value you get from n5 in a static or global variable so its value is not lost when you change pages, and use the value of that variable to control the blink.

This:

const long interval = (60/rateSet*1000)/2;  

Can't be right because 'const' means 'constant', i.e., unchanging. I'm surprised the compiler does not throw an error for that.

I think that compiles because interval is defined within the loop(), so it goes out of scope and is recreated anew with each cycle. It is questionable coding style, though: I'd rather make it a global variable defined at top level.

My personal opinion is that you are wasting your time using the official Nextion library. I'm obviously going to suggest you use my methods in Using Nextion displays with Arduino, but in case you prefer a library to my way of doing things I also provide in that tutorial a link to a library by Seithan, which I have read is very good. I can help you with my methods, I have noticed Seithan giving advice about his.