Write a Code for me

Hi,
I am a Beginner in Arduino. I want to make a Project. I wanna control 4 x MG995 Servo Motor with 2 x Ultrasonic Sensor.

I want a Code for following Function.
1) If any object comes in front of any of the Ultrasonic sensor the Motors will rotate at 180 degree.
2) Then they will be stable for 5 second and then comes to its Normal Position.

Can anyone Please make a code for me. I don't know Coding.

Are you going to pay someone or do you want someone to spend hours of their free time to just do this for you?

or can you teach me ?

That's a lot of lessons to teach you and I am terrible teacher.

OK
Np

.

Here is a good place to start learning Arduino


const int TRIG_PIN  = 6;  
const int ECHO_PIN  = 7;  
const int SERVO_PIN = 9; 
const int DISTANCE_THRESHOLD = 50; 

Servo servo; 


float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);       
  pinMode(TRIG_PIN, OUTPUT); 
  pinMode(ECHO_PIN, INPUT);  
  servo.attach(SERVO_PIN);   
  servo.write(0);
}

void loop() {
  
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  
  duration_us = pulseIn(ECHO_PIN, HIGH);
  
  distance_cm = 0.017 * duration_us;

  if(distance_cm < DISTANCE_THRESHOLD)
    servo.write(90); 
  else
    servo.write(0);  


  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}```


Is this a correct code?

Have you tried it?

No. But I had ran it on A online code compiler and tester.

If it works on Wokwi then it should work on a real board

Its Wokwi!

So you have working code for one ultrasonic sensor ond one servo. Now add another ultrasonic sensor and another servo and try to add code to make it work.

it will work on multiple servos and ultrasonic sensors easily.

So exactly what is the problem?

I got my solution :slight_smile:
Thanks for Helping.

:wink:

Have fun

Does Wokwi handle interference between adjacent ultrasonic sensors correctly?

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.