2ultrasonic_setsors_conflict

trying to make project auto-aim for camera using 2 sensors that look in one direction, and they work when i connect them saparately, but when use them both, one of them returns 0 constantly.

code:

if (mode == 2) {
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = duration * 0.034 / 2;
      Serial.println(distance);
      delay(10);

      // Sets the trigPin on HIGH state for 10 micro seconds
      digitalWrite(TRIGPIN, HIGH);
      delayMicroseconds(10);
      digitalWrite(TRIGPIN, LOW);
      // Reads the echoPin, returns the sound wave travel time in microseconds
      duration1 = pulseIn(ECHOPIN, HIGH);
      // Calculating the distance
      distance1 = duration1 * 0.034 / 2;
      // Prints the distance on the Serial Monitor
      Serial.println(distance1);
      if (butt2 == 1 & butt_flag2 == 0 & cm - timer2 > 100) {
        butt_flag2 = 1;
        mode++;
        if (mode == 4) {
          mode = 1;
        }
        timer2 = cm;
        digitalWrite(17, HIGH);
        delay(500);
        digitalWrite(17, LOW);
        Serial.println(mode);
      }
      if (butt2 == 0 & butt_flag2 == 1) {
        butt_flag2 = 0;
      }
      bool h;
      if (distance1>100){
        h = !h;
      }
      if (distance > 100 & q > 0  ) {
        q--;
        servo.write(q);
        delay(1);
        // if (q == 0) {
        //   w = 1;
        // }
      } else if (distance > 100 & q < 180  ) {
        q++;
        servo.write(q);
        delay(1);
        // if (q == 180) {
        //   w = 0;
        // }
      }
    }

ps
here is only part of the code that works only when I pressed the button

This is the working part .Could you provide rest of the code that not working as you need so we can figure out what is wrong . If you have any problem sharing the code then describe the problem in the way that we can all understand

#include <Servo.h>
#include <SoftwareSerial.h>

///////////////sonar
const int trigPin = 11;
const int echoPin = 2;
// defines variables
long duration;
int distance;

const int TRIGPIN = 9;
const int ECHOPIN = 10;
// defines variables
long duration1;
int distance1;
//////////////

/////////////////software serial
SoftwareSerial mySerial(12, 13);
///////////////////

//////////////buz
int yn = 1;
////////////

/////////////////main button
bool butt_flag1;
bool butt1;
bool led_flag1;
unsigned long timer1;
////////////////////////

/////////////butt2
bool butt_flag2;
bool butt2;
unsigned long timer2;
int mode = 1;
//////////////

////////////rgb
int redValue = random(0, 255);
int greenValue = random(0, 255);
int blueValue = random(0, 255);
//////////////

//////////servo
Servo servo;
int q = 90;
int w;
//////////////

////////////pot
int pot = analogRead(A0);
int speed;
///////////

void setup() {
pinMode(TRIGPIN, OUTPUT); // Sets the trigPin as an Output
pinMode(ECHOPIN, INPUT); // Sets the echoPin as an Input
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT);
Serial.begin(9600);
mySerial.begin(9600);
pinMode(3, INPUT_PULLUP);
pinMode(4, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(16, OUTPUT);
servo.attach(5);
pinMode(6, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(17, OUTPUT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {

unsigned long current_millis = millis();
butt1 = !digitalRead(3);
if (butt1 == 1 & butt_flag1 == 0 & current_millis - timer1 > 80) {
butt_flag1 = 1;
led_flag1 = !led_flag1;
delay(100);
timer1 = current_millis;
}
if (butt1 == 0 & butt_flag1 == 1) {
butt_flag1 = 0;
}

unsigned long cm = millis();
butt2 = !digitalRead(8);
if (butt2 == 1 & butt_flag2 == 0 & cm - timer2 > 100) {

butt_flag2 = 1;
mode++;
if (mode == 4) {
  mode = 1;
}
timer2 = cm;
digitalWrite(17, HIGH);
delay(500);
digitalWrite(17, LOW);
mySerial.println(mode);

}
if (butt2 == 0 & butt_flag2 == 1) {
butt_flag2 = 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
if (led_flag1 == 1) {
speed = map(pot, 0, 1023, 0, 255);
setColor(redValue, greenValue, blueValue);
// analogWrite(11, speed);
// digitalWrite(2, LOW);
if (mode == 1) {
if (digitalRead(6) == LOW & q < 180) {
Serial.println(q);
q++;
delay(10);
servo.write(q);
}
if (digitalRead(7) == LOW & q > 0) {
Serial.println(q);
q--;
delay(10);
servo.write(q);
}
}
if (mode == 2) {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.println(distance);
delay(10);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration1 = pulseIn(ECHOPIN, HIGH);
  // Calculating the distance
  distance1 = duration1 * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  Serial.println(distance1);
  if (butt2 == 1 & butt_flag2 == 0 & cm - timer2 > 100) {
    butt_flag2 = 1;
    mode++;
    if (mode == 4) {
      mode = 1;
    }
    timer2 = cm;
    digitalWrite(17, HIGH);
    delay(500);
    digitalWrite(17, LOW);
    Serial.println(mode);
  }
  if (butt2 == 0 & butt_flag2 == 1) {
    butt_flag2 = 0;
  }
  bool h;
  if (distance1>100){
    h = !h;
  }
  if (distance > 100 & q > 0  ) {
    q--;
    servo.write(q);
    delay(1);
    // if (q == 0) {
    //   w = 1;
    // }
  } else if (distance > 100 & q < 180  ) {
    q++;
    servo.write(q);
    delay(1);
    // if (q == 180) {
    //   w = 0;
    // }
  }
}

if (mode == 3) {

  if (butt2 == 1 & butt_flag2 == 0 & cm - timer2 > 100) {

    butt_flag2 = 1;
    mode++;
    if (mode == 4) {
      mode = 1;
    }
    timer2 = cm;
    digitalWrite(17, HIGH);
    delay(500);
    digitalWrite(17, LOW);
    Serial.println(mode);
  }

  if (butt2 == 0 & butt_flag2 == 1) {
    butt_flag2 = 0;
  }

  if (q > 0 & w == 0) {
    q--;
    servo.write(q);
    delay(10);
    if (q == 0) {
      w = 1;
    }

  } else if (q < 180 & w == 1) {
    q++;
    servo.write(q);
    delay(10);
    if (q == 180) {
      w = 0;
    }
  }
}
// while (yn < 300) {
//   yn++;
//   delay(1);
//   digitalWrite(4, HIGH);
// }
// digitalWrite(4, LOW);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////

} else if (led_flag1 == 0) {
yn = 1;
// digitalWrite(11, LOW);
// digitalWrite(2, LOW);
digitalWrite(14, LOW);
digitalWrite(15, LOW);
digitalWrite(16, LOW);
redValue = random(0, 255);
greenValue = random(0, 255);
blueValue = random(0, 255);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(14, redValue);
analogWrite(15, greenValue);
analogWrite(16, blueValue);
}

Format your code, please.

Have you tried a delay between reading the first sensor distance and starting the pulse for the second sensor?

Can you format the code so it could be little readable and please upload code using <code/> tag .It will help to look up the code .Also describe the pins that you are using give names to pins so we can understand what is the use of that pin .Thank you :grin:

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