Connecting two Ultrasonic with servo motor with different angles and leds (red,green))

Dear programmers,

It's my first time posting . I'm a beginner with Arduino and following Mr. Paul McWhorter's online courses.

I'm building a smart home for my cat , I used an Internal and External Ultrasonic sensors to control the direction of the door.

Please ,I want you to clear for me what's wrong/modify in my code to make the process as stable on demand as a single ultrasonic does.

so ,the servo motor keeps working with the 3 angles without stopping.

I do a good job with the individual components coding not with multiple .

best regards


#include<Servo.h>

int servoPin=6;
int redPin=8;
int greenPin=9;
int redPin01=10;
int greenPin01=11;

Servo myServo;

int trigPin=2; //Sensor Trip pin connected to Arduino pin 2
int echoPin=3;  //Sensor Echo pin connected to Arduino pin 3
int trigPin01=4; //Sensor Trip pin connected to Arduino pin 4
int echoPin01=5;  //Sensor Echo pin connected to Arduino pin 5


float pingTime;  //time for ping to travel from sensor to target and return
float pingTime01;  //time for ping to travel from sensor to target and return

float targetDistance; //Distance to Target in centimeter
float targetDistance01; //Distance to Target in centimeter

float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.
float speedOfSound01=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

void setup() {
  
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(trigPin01,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(echoPin01,INPUT);

pinMode(redPin,OUTPUT);
pinMode(redPin01,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(greenPin01,OUTPUT);

myServo.attach(servoPin);
}

void loop() {
  
  myServo.write(90);
   digitalWrite(trigPin, LOW); //
   digitalWrite(trigPin01, LOW); //
   digitalWrite(redPin,HIGH);
   digitalWrite(greenPin,LOW);
   digitalWrite(redPin01,HIGH);
   digitalWrite(greenPin01,LOW);
   delay(3000);
  
  if(targetDistance <= 21){
    myServo.write(180);
   digitalWrite(trigPin, HIGH); //Set trigPin high
   digitalWrite(trigPin01, LOW); //Set trigPin highdigitalWrite(redPin,LOW);   digitalWrite(greenPin,HIGH);
   digitalWrite(redPin,LOW);
   digitalWrite(greenPin,HIGH);
   digitalWrite(redPin01,HIGH);
   digitalWrite(greenPin01,LOW);
   delay(3000);
  }
 

if(targetDistance01 <= 21){
    myServo.write(0);
   digitalWrite(trigPin, LOW); //Set trigPin high
   digitalWrite(trigPin01, HIGH); //Set trigPin highdigitalWrite(redPin,LOW);   digitalWrite(greenPin,HIGH);
   digitalWrite(redPin,HIGH);
   digitalWrite(greenPin,LOW);
   digitalWrite(redPin01,LOW);
   digitalWrite(greenPin01,HIGH);
     delay(3000);

  }
   
 if (targetDistance>=21){
     myServo.write(90);
   digitalWrite(trigPin, LOW); //ping has now been sent
   digitalWrite(trigPin01, LOW); //ping has now been sent
    digitalWrite(redPin,HIGH);
    digitalWrite(greenPin,LOW);
     digitalWrite(redPin01,HIGH);
    digitalWrite(greenPin01,LOW);
       delay(1000);

  }

   if (targetDistance01>=21){
     myServo.write(90);
   digitalWrite(trigPin, LOW); //ping has now been sent
   digitalWrite(trigPin01, LOW); //ping01 has now been sent
    digitalWrite(redPin,HIGH);
    digitalWrite(greenPin,LOW);
     digitalWrite(redPin01,HIGH);
    digitalWrite(greenPin01,LOW);
     delay(1000);
  }
  

{ 
  pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  pingTime01 = pulseIn(echoPin01, HIGH);  //pingTime01 is presented in microceconds

  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)

   pingTime01=pingTime01 /1000000; //convert pingTime01 to seconds by dividing by 1000000 (microseconds in a second)
   pingTime01=pingTime01 /3600; //convert pingtime01 to hourse by dividing by 3600 (seconds in an hour)
 
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
  targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*160934;    //Convert miles to cm by multipling by 160934 (cm per mile)

  targetDistance01= speedOfSound01  * pingTime01;  //This will be in miles, since speed of sound was miles per hour
  targetDistance01=targetDistance01 /2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance01= targetDistance01 *160934;    //Convert miles to cm by multipling by 160934 (cm per mile)
  


  delay(250); //pause to let things settle

  
  
}

  
 
  }```

How is this in the LEDs and Multiplexing category?


That's what all Arduino tutorials suck at.

1 Like

Hi,
A brief analysis of your code can identify some problems:

  1. Open braces on line 100 and close on line 125, for no reason.

  2. Following your code:
    Entering the loop moves the servo to 90;
    testing::
    targetDistance <= 21 if servo moves to 180;
    targetDistance >= 21 if servo moves to 90;
    targetDistance01 <= 21 if servo moves to 0;
    targetDistance01 >= 21 if servo moves to 90;

2.A. If the targetDistance is = 21 it goes to 180 and then to 90;
then it goes to 90, because when it goes back to the beginning of the loop it goes back to 90.

2.B. If targetDistance01 is = 21 it goes to 0 and then to 90;
then it goes to 90, because when it goes back to the beginning of the loop it goes back to 90.

2.C. If targetDistance is < 21 it goes to 180 and then to 0;
then it goes to 90, because when it goes back to the beginning of the loop it goes back to 90.
Your servo always goes back to 90 in the loop() loop.
and so on.
You need to review your code's action logic.

1 Like

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