how to run a loop only 1 time every 20 times another loop runs

I have made a motion sensor combined with a distance sensor that transmits to a receiving nano. On the receiver, I want the speaker to only run 1 time every minute, but the distance sensor to continue to run while the speaker waits a minute to run again.

I have included my code.

I hope I explained it good enough for you to understand.

In other words:

if motion detected, play speaker, then pause for at least 30 seconds. Meanwhile continually display the distance via 3 leds.

Hope that helps.

Receiver_Unit_Motion_Dist.ino (3.62 KB)

/*This sketch is the reciever for a motion/distance sensing unit
 * It consists of 2 different units, a transmitter  and a receiver(this sketch)
 * Transmitter Unit
 * HC-SR501 Motion Sensor HC-SR04 Distance Sensor, NRF24, Nano
 * Receiver Unit
 * 8ohm Speaker, LED's, Mega 2560, NRF-24
 * sketch made 2-5-2020 by Troy Dowdle
 * HC-SR501 is attached to pin A0
 * Ultrasonic Sensor HC-SR04 attached to RGGB with red when objects close 
 * Yellow when at a distance and green when nothing is detected
 * 
 */
 
    //Music Library
    #include "pitches.h"
    //NRF-24 Libraries included
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
  
    RF24 radio(7, 8); // CE, CSN  connection from the NRF24
  
    const byte address[6] = "00001";  
  
    // notes in the melody:
    int melody[] = {
    NOTE_C5, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, 0, NOTE_B4, NOTE_C5
    };
  
    // note durations: 4 = quarter note, 8 = eighth note, etc.:
   int noteDurations[] = {
    4, 8, 8, 4, 4, 4, 4, 4
  };
    
    const int redPin=5;  //attach red led
    const int yellowPin=4;  //attach yellow led
    const int greenPin=3;  //attach green led 
    const int greenTrigger=600;  //distance to trigger green led
    const int yellowTrigger=500;  //distance that triggers yellow LED's
    const int redTrigger=300;  //distance that triggers red leds
    int motionVal=0;
    int distVal=0;
    
  void setup() {
    Serial.begin(9600);
    pinMode(redPin, OUTPUT);
    pinMode(yellowPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
  
    radio.begin();
    radio.openReadingPipe(0, address);
    radio.setPALevel(RF24_PA_MAX);
    radio.startListening();
}

void loop() {
 //if(radio.available()){  
 radio.read(&distVal, sizeof(distVal));
 radio.read(&motionVal, sizeof(motionVal));
    Serial.print("Distance Value=  ");
    Serial.println(distVal);
    Serial.print("Motion Sensor Value=  ");
    Serial.println(motionVal);
 //}
 
   //else  return;
        
if (motionVal >=500){
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(9, melody[thisNote], noteDuration); //9 is the speaker pin

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(9);  //9 is the speaker pin
    
  }
  delay(100000);
 }
  
 //Green light on when nothing close to sensor 
if ( distVal >= greenTrigger )   {
        //We now know the val is higher than greenTrigger
  digitalWrite(greenPin, HIGH);
  //keep yellow pin off
  digitalWrite(yellowPin, LOW);
    //Keep the red pin off.
  digitalWrite(redPin, LOW);
 }
      //Yellow Light when something is close
 else if ( distVal >= yellowTrigger && distVal<greenTrigger)    {
        //green and yellow LEDS are both on indicating something is closer to sensor
        digitalWrite(greenPin, HIGH);
        digitalWrite(yellowPin, HIGH);
        digitalWrite(redPin, LOW);}
       
    //All LED's are on cause something is really close to sensor
else  if (distVal>=redTrigger && distVal<yellowTrigger)  { 
           //Turn all pins on.
  digitalWrite(greenPin, HIGH); 
  digitalWrite(yellowPin, HIGH);        
  digitalWrite(redPin, HIGH);
  
  
 }
  delay(10);        // delay in between reads for stability
}

If you want to do anything during your pause you can't use delay, you will need to use millis instead.

See blink without delay or Robin2's thread on doing several things at once