How to make 2 loops run at same time??.

i’m making distance sensing experiment
if any object comes in the distance less than 50 cm buzzer will buzz
if any object is more than than the distance of 16 cm motor will start
i did this coding in two different loops .
but they are not working . please someone help me fastt

int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
int const motorPin = 5;
int const pottrigPin = 4;
int const potechoPin = 3;
void setup()
{
  pinMode(trigPin, OUTPUT); // trig pin will have pulses output
  pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
  pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
  pinMode(motorPin, OUTPUT); // this is for motor to vibrate
  pinMode(pottrigPin, OUTPUT); // this is another pin for 3rd sensor for pothole detection this sends waves
  pinMode(potechoPin, INPUT); // this is another pin for 3rd sensor for pothole detection this receives waves
}

void normal()
{
  // Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
  int duration, distance;
  // Output pulse with 1ms width on trigPin
  digitalWrite(trigPin, HIGH);
  delay(1);
  digitalWrite(trigPin, LOW);  // Measure the pulse input in echo pin
  duration = pulseIn(echoPin, HIGH);  // Distance is half the duration devided by 29.1 (from datasheet)
  distance = (duration / 2) / 29.1; // if distance less than 0.5 meter and more than 0 (0 or less means over range)
  if (distance <= 50 && distance >= 0) {      // Buzz
    digitalWrite(buzzPin, HIGH);
  } else {      // Don't buzz
    digitalWrite(buzzPin, LOW);
  }

  delay(60);  // Waiting 60 ms won't create too much problem
}

void pothole() {
  int potduration, potdistance;
  digitalWrite(pottrigPin, HIGH);
  delay(1);
  digitalWrite(pottrigPin, LOW);
  potduration = pulseIn(potechoPin, HIGH);
  potdistance = (potduration / 2) / 29.1;
  if (potdistance >= 16 && potdistance <= 0) {
    digitalWrite(motorPin, HIGH);
  } else {
    digitalWrite(motorPin, LOW);
  }
  delay(60);
}

void loop() {
  normal();
  pothole();
}

"but they are not working "
What’s not working?

to cross roads

two loops are not working at same time..

sorry i've confused you

The processor is only single threaded, it can only do one thing at a time. For example, while it is delaying(1) or (60) in one function, it can't run the other.

I suggest you pull the delay(60) back into loop(), or better yet use Blink Without Delay (File:Examples:Digital I think) and then not have delay at all.

Maybe every 30mS, do one action, then 30mS later do the other action.

to crossroads
sir i’m really new to this arduino programming

can u please modify my code or tell me how to modify it…
i just the blinkwithoutdelay example (as u suggested).

but i didn’t understand anything from it…

Keep working and experimenting and researching until you understand every bit of the BlinkWithoutDelay sketch. This is an essential concept for you to understand if you are to have success with Arduino. Once you understand BlinkWithoutDelay break your sketch into two parts and get each one working perfectly without using delay, they you will be ready to combine them. If you run into problems along the way come back with specific questions and code and we'll try to help.

If we write the code for you then you won't learn anything.

thank you pert

you've ignited the excitement and enthusiasm in me

thank u i've learnt it

and i made my code

thanks to Crossroads also

The demo Several Things at a Time is an extended example of BWoD. It may help with understanding the technique.

...R

do you know if there is any way to fix my code.

I have a mp3 player and lights that play with it but the mp3 player wont play until the lights are done blinking even though it is higher in the code

jingle_bellslights.ino (5.92 KB)

coder1212: do you know if there is any way to fix my code.

I have a mp3 player and lights that play with it but the mp3 player wont play until the lights are done blinking even though it is higher in the code

You should not use delay(). Study File > Examples > 02.Digital > BlinkWithoutDelay and the associated tutorial: https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay to understand how to write non-blocking code.

coder1212: the mp3 player wont play until the lights are done blinking even though it is higher in the code

My guess would be that the first play command to the MP3 player is failing for some reason. It might be that the play command is sent too soon after the "select the TF card" command. To test that, you could increase the delay after sending the "select the TF card" command.