Just wanting m9 and m10 including servo motor to go low whenever the Ultrasonic detects a high signal , hopefully waiting for any program suggestion . I couldn't have post my program cause i was new to forum

Have another go at describing what you want. Ultrasonic sensors usually detect distances. Motors are stopped or rotating at various speeds and servos go to one position or another.

Describing motors and a servo as “going low” doesn’t really mean anything. Nor does Ultrasonic detecting a “high signal”.

Steve

i mean to say is the top motor driver and servo must stop at which direction the ultrasonic sensor has detected an obstacles .
sorry for if i am too foolish

O.k. please post a clear picture or two of whatever this thing is that you’re talking about.

I guess the servo is doing something like moving the sensor to point in different directions. Is that right? And you have two motor drivers marked “Front” and “Back”. Which of those is the “top motor driver”? Getting the servo to stop when the sensor detects something is easy enough. But what do the motors have to do with it?

Do you have any code yet that does anything with this setup? If so please post it and say what it does. If not then you have some work to do before anyone can really help.

Steve

The servo stops moving when you stop giving it new positions to move to.

A DC motor on an H-Bridge drive stops when the two IN lines to the H-Bridge have the same value or the ENA (Enable) line is set to “Not Enabled”.

Are you sweeping the ultrasonic range finder with the servo, or the motor, or somehow both? Did you want to stop at the the first thing below some distance limit, or complete a scan and move back to the position with the closest thing?

lucky_final_robot.ino (3.8 KB)
just check the motion of the servo and please suggest me , how could i control the position of the servo whenever the ultrasonic sensor detect obstacles less than 10 inch or cm the servo and the m9 and m10 too must go to “wait”

{
ultra();
servo1.write(0);
if(distance<=10)
{
sevo1.write(180);
}
{
servo1.write(90):
}
}
this part need to be modified

Sir
@johnwasser
my ideas was to sweep the servo attach with the ultrasonic sensor and stop the servo wherever the ultrasonic sensor has indicated an obstacles less than 10 inch or cm .
As you already advice me , i will disabled the enabled pin of the top motor driver.

Then your idea will fail. You cannot move the ultrasonic sensor until it has received an echo or has timed out. Then you can reposition the sensor for the next cycle.
Paul

oh your message sound me informative , thank you sir , @Paul_KD7HB
Lucky

Any suggestion for me sir , i will really appreciate it .
thinking you in advance

Forget about the motors for now. Just have a go at writing some code that sweeps the sensor, detects obstacles within 10cm and stops the sweep pointing at the obstacle. Then post that code (as a COMPLETE program) and describe any problems you have with it. That way we have something to help you with.

Steve

type or paste code here[code]
#include <Servo.h>  // https://www.arduino.cc/reference/en/libraries/servo/
#define m1 8
#define m2 9
#define EN1 2
#define m3 10
#define m4 11
#define EN2 3
#define m5 12
#define m6 13
#define EN3 4
#define m7 14
#define m8 15
#define EN4 5
#define m9 16
#define m10 17
#define EN5 6
#define STQ 20
#define Q4 53
#define Q3 52
#define Q2 51
#define Q1 50
Servo servo1;
#define ECHO 21
#define TRIG 22
long duration;
long distance;
void forward()
{
  digitalWrite(m1, HIGH);
  digitalWrite(m2, LOW);
  analogWrite(EN1, 255);
  digitalWrite(m3, HIGH);
  digitalWrite(m4, LOW);
  analogWrite(EN2, 255);
  digitalWrite(m5, HIGH);
  digitalWrite(m6, LOW);
  analogWrite(EN3, 255);
  digitalWrite(m7, HIGH);
  digitalWrite(m8, LOW);
  analogWrite(EN4, 255);
}
void backward()
{
  digitalWrite(m1, LOW);
  digitalWrite(m2, HIGH);
  analogWrite(EN1, 255);
  digitalWrite(m3, LOW);
  digitalWrite(m4, HIGH);
  analogWrite(EN2, 255);
  digitalWrite(m5, LOW);
  digitalWrite(m6, HIGH);
  analogWrite(EN3, 255);
  digitalWrite(m7, LOW);
  digitalWrite(m8, HIGH);
  analogWrite(EN4, 255);
  delay(1000);
}
void right()
{
  digitalWrite(m1, HIGH);
  digitalWrite(m2, LOW);
  analogWrite(EN1, 255);
  digitalWrite(m3, LOW);
  digitalWrite(m4, HIGH);
  analogWrite(EN2, 175);
  digitalWrite(m5, HIGH);
  digitalWrite(m6, LOW);
  analogWrite(EN3, 255);
  digitalWrite(m7, LOW);
  digitalWrite(m8, HIGH);
  analogWrite(EN4, 175);
  delay(1000);
}
void left()
{
  digitalWrite(m1, LOW);
  digitalWrite(m2, HIGH);
  analogWrite(EN1, 175);
  digitalWrite(m3, HIGH);
  digitalWrite(m4, LOW);
  analogWrite(EN2, 255);
  digitalWrite(m5, LOW);
  digitalWrite(m6, HIGH);
  analogWrite(EN3, 175);
  digitalWrite(m7, HIGH);
  digitalWrite(m8, LOW);
  analogWrite(EN4, 255);
  delay(1000);
}
void stop()
{
  digitalWrite(m1, LOW);
  digitalWrite(m2, LOW);
  analogWrite(EN1, 100);
  digitalWrite(m3, LOW);
  digitalWrite(m4, LOW);
  analogWrite(EN2, 100);
  digitalWrite(m5, LOW);
  digitalWrite(m6, LOW);
  analogWrite(EN3, 100);
  digitalWrite(m7, LOW);
  digitalWrite(m8, LOW);
  analogWrite(EN4, 100);
  delay(1000);
}
void clockwise()
{
  digitalWrite(m9, HIGH);
  digitalWrite(m10, LOW);
  analogWrite(EN5, 175);
  delay(1000);
}
void counterclockwise()
{
  digitalWrite(m9, LOW);
  digitalWrite(m10, HIGH);
  analogWrite(EN5, 175);
  delay(1000);
}
void wait()
{
  digitalWrite(m9, LOW);
  digitalWrite(m10, LOW);
  analogWrite(EN5, 100);
  delay(1000);
}
void setup()
{
  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
  pinMode(EN1, OUTPUT);
  pinMode(m3, OUTPUT);
  pinMode(m4, OUTPUT);
  pinMode(EN2, OUTPUT);
  pinMode(m5, OUTPUT);
  pinMode(m6, OUTPUT);
  pinMode(EN3, OUTPUT);
  pinMode(m7, OUTPUT);
  pinMode(m8, OUTPUT);
  pinMode(EN4, OUTPUT);
  pinMode(m9, OUTPUT);
  pinMode(m10, OUTPUT);
  pinMode(EN5, OUTPUT);
  pinMode(Q1, INPUT);
  pinMode(Q2, INPUT);
  pinMode(Q3, INPUT);
  pinMode(Q4, INPUT);
  pinMode(STQ, INPUT);
  pinMode(ECHO, INPUT);
  pinMode(TRIG, OUTPUT);
  servo1.attach(23);
  Serial.begin(9600);
}
void loop()
{
  {

    int temp1 = digitalRead(Q1);
    int temp2 = digitalRead(Q2);
    int temp3 = digitalRead(Q3);
    int temp4 = digitalRead(Q4);
    if (temp1 == 1 && temp2 == 0 && temp3 == 0 && temp4 == 0)
      counterclockwise();
    else if (temp1 == 0 && temp2 == 1 && temp3 == 0 && temp4 == 0)
      forward();
    else if (temp1 == 1 && temp2 == 1 && temp3 == 0 && temp4 == 0)
      clockwise();
    else if (temp1 == 0 && temp2 == 0 && temp3 == 1 && temp4 == 0)
      left();
    else if (temp1 == 1 && temp2 == 0 && temp3 == 1 && temp4 == 0)
      stop();
    else if (temp1 == 0 && temp2 == 1 && temp3 == 1 && temp4 == 0)
      right();
    else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 1)
      backward();
    else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 0)
      wait();
  }
  {
    ultra();
    servo1.write(0);
    if (distance <= 10)
    {
      sevo1.write(180);
    }
    {
      servo1.write(90):
      }
    }
  {
    backward();
    if (distance <= 10)
      delay(1000);
  }
  void ultra()
  {
    digitalWrite(TRIG, LOW);
    delay(2);
    digitalWrite(TRIG, HIGH);
    delay(10);
    digitalWrite(TRIG, LOW);
    duration = pulseIn(ECHO, HIGH);
    distance = duration * 0.034 / 2;
  }
  {
    for (int pos = 0; pos <= 180; pos += 1)
    {
      servo1.write(pos);
      delay(15)
    }
    for (int pos = 180; pos >= 0; pos -= 1)
    {
      servo1.write(pos);
      delay(15);
    }
  }
}
[/code]
this is my whole code 
But still have some error
Hoping you can really help me @ slipstick
Thanking you in advance
Lucky












As the error listing clearly shows you are trying to define the function ultra() inside loop(). You can’t do that. All function definitions must start at the far left so move ultra() down past the end of loop().

Also sort out your {} braces which are scattered almost at random through the program. They MUST be paired, one { matching one }. And check spellings - sevo1 is not the same as servo1. And you can’t just put a : colon where there should be a ; semicolon

All of that was covered in the error listing you got when you validated. I suggest reading those errors yourself and acting on them. It’s much quicker than coming on here hoping someone will do it all for you.

Steve

Sir @slipstick i completed it ,now it is an error free program as i have deleted some command ,but for me it is unsatisfied

#include <Servo.h>  // https://www.arduino.cc/reference/en/libraries/servo/
#define m1 8
#define m2 9
#define EN1 2
#define m3 10
#define m4 11
#define EN2 3
#define m5 12
#define m6 13
#define EN3 4
#define m7 14
#define m8 15
#define EN4 5
#define m9 16
#define m10 17
#define EN5 6
#define STQ 20
#define Q4 53
#define Q3 52
#define Q2 51
#define Q1 50
Servo servo1;
#define ECHO 21
#define TRIG 22
long duration;
long distance;
void forward()
{
  digitalWrite(m1, HIGH);
  digitalWrite(m2, LOW);
  analogWrite(EN1, 255);
  digitalWrite(m3, HIGH);
  digitalWrite(m4, LOW);
  analogWrite(EN2, 255);
  digitalWrite(m5, HIGH);
  digitalWrite(m6, LOW);
  analogWrite(EN3, 255);
  digitalWrite(m7, HIGH);
  digitalWrite(m8, LOW);
  analogWrite(EN4, 255);
}
void backward()
{
  digitalWrite(m1, LOW);
  digitalWrite(m2, HIGH);
  analogWrite(EN1, 255);
  digitalWrite(m3, LOW);
  digitalWrite(m4, HIGH);
  analogWrite(EN2, 255);
  digitalWrite(m5, LOW);
  digitalWrite(m6, HIGH);
  analogWrite(EN3, 255);
  digitalWrite(m7, LOW);
  digitalWrite(m8, HIGH);
  analogWrite(EN4, 255);
  delay(1000);
}
void right()
{
  digitalWrite(m1, HIGH);
  digitalWrite(m2, LOW);
  analogWrite(EN1, 255);
  digitalWrite(m3, LOW);
  digitalWrite(m4, HIGH);
  analogWrite(EN2, 175);
  digitalWrite(m5, HIGH);
  digitalWrite(m6, LOW);
  analogWrite(EN3, 255);
  digitalWrite(m7, LOW);
  digitalWrite(m8, HIGH);
  analogWrite(EN4, 175);
  delay(1000);
}
void left()
{
  digitalWrite(m1, LOW);
  digitalWrite(m2, HIGH);
  analogWrite(EN1, 175);
  digitalWrite(m3, HIGH);
  digitalWrite(m4, LOW);
  analogWrite(EN2, 255);
  digitalWrite(m5, LOW);
  digitalWrite(m6, HIGH);
  analogWrite(EN3, 175);
  digitalWrite(m7, HIGH);
  digitalWrite(m8, LOW);
  analogWrite(EN4, 255);
  delay(1000);
}
void stop()
{
  digitalWrite(m1, LOW);
  digitalWrite(m2, LOW);
  analogWrite(EN1, 100);
  digitalWrite(m3, LOW);
  digitalWrite(m4, LOW);
  analogWrite(EN2, 100);
  digitalWrite(m5, LOW);
  digitalWrite(m6, LOW);
  analogWrite(EN3, 100);
  digitalWrite(m7, LOW);
  digitalWrite(m8, LOW);
  analogWrite(EN4, 100);
  delay(1000);
}
void clockwise()
{
  digitalWrite(m9, HIGH);
  digitalWrite(m10, LOW);
  analogWrite(EN5, 175);
  delay(1000);
}
void counterclockwise()
{
  digitalWrite(m9, LOW);
  digitalWrite(m10, HIGH);
  analogWrite(EN5, 175);
  delay(1000);
}
void wait()
{
  digitalWrite(m9, LOW);
  digitalWrite(m10, LOW);
  analogWrite(EN5, 100);
  delay(1000);
}
void setup()
{
  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
  pinMode(EN1, OUTPUT);
  pinMode(m3, OUTPUT);
  pinMode(m4, OUTPUT);
  pinMode(EN2, OUTPUT);
  pinMode(m5, OUTPUT);
  pinMode(m6, OUTPUT);
  pinMode(EN3, OUTPUT);
  pinMode(m7, OUTPUT);
  pinMode(m8, OUTPUT);
  pinMode(EN4, OUTPUT);
  pinMode(m9, OUTPUT);
  pinMode(m10, OUTPUT);
  pinMode(EN5, OUTPUT);
  pinMode(Q1, INPUT);
  pinMode(Q2, INPUT);
  pinMode(Q3, INPUT);
  pinMode(Q4, INPUT);
  pinMode(STQ, INPUT);
  pinMode(ECHO, INPUT);
  pinMode(TRIG, OUTPUT);
  servo1.attach(23);
  Serial.begin(9600);
}
void loop()
{

    int temp1 = digitalRead(Q1);
    int temp2 = digitalRead(Q2);
    int temp3 = digitalRead(Q3);
    int temp4 = digitalRead(Q4);
    if (temp1 == 1 && temp2 == 0 && temp3 == 0 && temp4 == 0)
      counterclockwise();
    else if (temp1 == 0 && temp2 == 1 && temp3 == 0 && temp4 == 0)
      forward();
    else if (temp1 == 1 && temp2 == 1 && temp3 == 0 && temp4 == 0)
      clockwise();
    else if (temp1 == 0 && temp2 == 0 && temp3 == 1 && temp4 == 0)
      left();
    else if (temp1 == 1 && temp2 == 0 && temp3 == 1 && temp4 == 0)
      stop();
    else if (temp1 == 0 && temp2 == 1 && temp3 == 1 && temp4 == 0)
      right();
    else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 1)
      backward();
    else if (temp1 == 0 && temp2 == 0 && temp3 == 0 && temp4 == 0)
      wait();
  {
   void ultra();
    servo1.write(0);
    if (distance <= 10)
  {
    backward();
    if (distance <= 10)
      delay(1000);
  }
  }
  {
    digitalWrite(TRIG, LOW);
    delay(2);
    digitalWrite(TRIG, HIGH);
    delay(10);
    digitalWrite(TRIG, LOW);
    duration = pulseIn(ECHO, HIGH);
    distance = duration * 0.034 / 2;
  }
  {
    for (int pos = 0; pos <= 180; pos += 1)
      servo1.write(pos);
      delay(15);
    for (int pos = 180; pos >= 0; pos -= 1)
      servo1.write(pos);
      delay(15);
  }
}

here the code
Hoping i will still need you help
With regard Lucky

To do that you will have to measure the distance INSIDE the loop that sweeps the servo. Measuring the distance once and then sweeping the servo back and forth will not do what you say you want to do.

Your previou attempt where you tried to put the distance sensing in a separate function was a good idea. That will make it possible to measure the distance with one function call instead of 7 lines of code. You just have to move the new function OUTSIDE the loop() function.

You now have a function ultra() which does absolutely nothing.

You move the servo to 0 and try to check distance. But at that point you haven’t yet read the sensor so distance isn’t set.

Then you just wag the servo backwards and forwards 180 degrees but you never read the sensor again.Usually the point of sweeping the sensor is to move the servo by a small angle, then read the sensor to see if there is an obstacle. If there is an obstacle stop there. If there isn’t move the servo a little further on and check again.

Steve

Yes brother that was the whole point and my servo and servo to function
@slipstick
with lot of gratitude form Lucky

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