Can I make multiple void loops with Arduino Uno?

I have a problem. I want to make smart trash bin with a buzzer, 2 Sensor Ultrasonic HC-SR04 and servo. I want to run them at the same time but in different loops.

This is my code.

#define trigPin 10
#define echoPin 9
#define buzzPin 2
#define trigPin2 5
#define echoPin2 6
#define servoPin 7
#define led 3
long duration2, dist, average;
long aver[3];

#include <Servo.h> //servo library
Servo servo;

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(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
Serial.begin(9600);
servo.attach(servoPin);
servo.write(0); //close cap on power on
delay(100);
servo.detach();

}

void measure() {
digitalWrite(10, HIGH);
digitalWrite(trigPin2, LOW);
delayMicroseconds(10);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin2, LOW);
pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);
dist = (duration2 / 2) / 29.1; //obtain distance
}

void loop() {

openclose();
bunyi();
}
void openclose() {
for (int i = 0; i <= 2; i++) { //average distance
measure();
aver = dist;

  • delay(10); //delay between measurements*
  • }*
  • dist = (aver[0] + aver[1] + aver[2]) / 3;*
  • if ( dist < 30 ) {*
  • //Change distance as per your need*
  • servo.attach(servoPin);*
  • delay(1);*
  • servo.write(0);*
  • delay(3000);*
  • servo.write(150);*
  • delay(1000);*
  • servo.detach();*
  • }*
  • Serial.print(dist);*
    }
    void bunyi() {
  • // 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) / 20.1;*
  • // if distance les s than 0.5 meter and more than 0 (0 or less means over range)*
  • if (distance <= 10 && distance >= 5) {*
  • // Buzz*
  • digitalWrite(buzzPin, HIGH);*
  • } else {*
  • // Don't buzz*
  • digitalWrite(buzzPin, LOW);*
  • }*
  • // Waiting 60 ms won't hurt any one*
  • delay(60);*
    }

So what does the improperly posted code do? How is that different from what you want it to do?

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

void loop_buzzer() {
   // put your  code here
}

void loop_ultrasonic_1() {
   // put your  code here
}

void loop_ultrasonic_2() {
   // put your  code here
}

void loop_servo() {
   // put your  code here
}

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  loop_buzzer();
  loop_ultrasonic_1();
  loop_ultrasonic_2();
  loop_servo();
}

Note that: each loop is run in sequence. They CANNOT run at the same time with Arduino.

You SHOULD NOT use the delay() function in each loop. Instead, use timestamp, see Blink Without Delay example

arduino programming is single thread so "no" is the official answer. however these little boards are capable of very complex timing through multiple timers. Unfortunately, your code structure isn't going to work for what you are trying to do.

since these boards are single thread, anytime you use the delay function it freezes the entire sketch. so you can't use delay anytime you are trying to do more than one thing.

to accomplish what you are trying to do you will need to use millis() timers.
if you are unfamiliar here is the most basic example:

if you change your coding approach to this for your timing needs then the sky is the limit. you can only do so much with delay.

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

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R