Go Down

Topic: Help with multiple ultrasonic sensors - unusual readings (Read 95 times) previous topic - next topic

dani1999

Hi, I'm Danielle and I've been having a problem with my arduino project.
I'm using multiple ultrasonic sensors which have to detect unequal readings and would make an LED blink once an unequal reading is found. It's mounted up on a glass case which is approximately 9 centimeters deep. As I read the program I've done, it all seems good but as I ran the program and looked it over the serial monitor, the readings aren't accurate because the readings from the three sensors shift from either 9 or 8, such as this:

distance 1 = 9
distance 2 = 8
distance 3 = 9
-
distance 1 = 8
distance 2 = 8
distance 3 = 9

I'm confused if this has something to do with the case, the parts, the sensors or my program. here's the program I've been using.

//Sonar 1
int echoPin1 =2;
int initPin1 =3;
int distance1 =0;

//Sonar 2
int echoPin2 =6;
int initPin2 =7;
int distance2 =0;

//Sonar 3
int echoPin3 =8;
int initPin3 =9;
int distance3 =0;

void setup() {
 
  pinMode(initPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(4, OUTPUT);
  pinMode(initPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(5,OUTPUT);
  pinMode(initPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(10,OUTPUT);
  Serial.begin(9600);
 
}

void loop() {
  //ULTRASONIC SENSOR 1
  distance1 = getDistance(initPin1, echoPin1);
  printDistance(1, distance1);
  delay(150);
  if ((distance1!=distance2)&&(distance2==distance3)){
    digitalWrite(4,HIGH);
    delay(1000);
    digitalWrite(4,LOW);
    delay(1000);
  }
 
  //ULTRASONIC SENSOR 2
  distance2 = getDistance(initPin2, echoPin2);
  printDistance(2, distance2);
  delay(150);
   if (distance1==distance3) {
    digitalWrite(5,HIGH);
    delay(1000);
    digitalWrite(5,LOW);
    delay(1000);
  }
 
  //ULTRASONIC SENSOR 3
  distance3 = getDistance(initPin3, echoPin3);
  printDistance(3, distance3);
  delay(150);
   if ((distance3!=distance2)&&(distance2==distance1)){
    digitalWrite(10,HIGH);
    delay(1000);
    digitalWrite(10,LOW);
    delay(1000);
   }
   if ((distance1==distance2)&&(distance2==distance3)){
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    digitalWrite(10,LOW);
   }
  delay(150);
  }

int getDistance (int initPin, int echoPin){

 digitalWrite(initPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(initPin, LOW);
 unsigned long pulseTime = pulseIn(echoPin, HIGH);
 int distance = pulseTime/58;
 return distance;
}

 void printDistance(int id, int dist){
 
     Serial.print(id);
    if (dist >= 120 || dist <= 0 ){
      Serial.println(" Out of range");
    }else
    for (int i = 0; i <= dist; i++) {
         Serial.print("-");
    }
    Serial.print(dist, DEC);
    Serial.println(" cm");
   
 }

Go Up