Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

code for connecting 3 ultrasonic sensor, 1 buzzer, 1 LED in a Arduino uno

I need a code that connects 3 ultrasonic sensors, 1 buzzer, and 1 LED in a Arduino uno

I tried using this code the original code was only supposed to work with 1 sensor I tried modifying it but nothing seems to work and it keeps buzzing and flickering light randomly. It would be much appreciated if you could help.

// defines pins numbers
const int trigPinA = 7;
const int echoPinA = 2;
const int trigPinB = 3;
const int echoPinB = 4;
const int trigPinC = 5;
const int echoPinC = 6;
const int buzzer = 10;
const int ledPin = 12;

// defines variables
long durationA;
long durationB;
long durationC;
int distanceA;
int distanceB;
int distanceC;
int safetyDistanceA;
int safetyDistanceB;
int safetyDistanceC;


void setup() {
pinMode(trigPinA, OUTPUT); // Sets the trigPin as an Output
pinMode(trigPinB, OUTPUT); // Sets the trigPin as an Output
pinMode(trigPinC, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPinA, INPUT); // Sets the echoPin as an Input
pinMode(echoPinB, INPUT); // Sets the echoPin as an Input
pinMode(echoPinC, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}


void loop() {
// Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPinA, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinA, LOW);

  digitalWrite(trigPinB, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinB, LOW);

  digitalWrite(trigPinC, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinC, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
  durationA = pulseIn(echoPinA, HIGH);
  durationB = pulseIn(echoPinB, HIGH);
  durationC = pulseIn(echoPinC, HIGH);

// Calculating the distance
  distanceA= durationA*0.034/2; 
  distanceB= durationB*0.034/2; 
  distanceC= durationC*0.034/2; 

// DISTANCE 
  safetyDistanceA = distanceA;
  if (safetyDistanceA <= 5){
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, HIGH);
  }
  else{
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, LOW);
  }

 safetyDistanceB = distanceB;
  if (safetyDistanceB <= 5){
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, HIGH);
  }
  else{
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, LOW);
  }
  
  safetyDistanceC = distanceC;
  if (safetyDistanceC <= 5){
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, HIGH);
  }
  else{
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, LOW);
  }
// Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print("Distance: ");
  Serial.print("Distance: ");
  Serial.println(distanceA);
  Serial.println(distanceB);
  Serial.println(distanceC);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

// defines pins numbers
const int trigPinA = 7;
const int echoPinA = 2;
const int trigPinB = 3;
const int echoPinB = 4;
const int trigPinC = 5;
const int echoPinC = 6;
const int buzzer = 10;
const int ledPin = 12;

// defines variables
long durationA;
long durationB;
long durationC;
int distanceA;
int distanceB;
int distanceC;
int safetyDistanceA;
int safetyDistanceB;
int safetyDistanceC;


void setup() {
  pinMode(trigPinA, OUTPUT); // Sets the trigPin as an Output
  pinMode(trigPinB, OUTPUT); // Sets the trigPin as an Output
  pinMode(trigPinC, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPinA, INPUT); // Sets the echoPin as an Input
  pinMode(echoPinB, INPUT); // Sets the echoPin as an Input
  pinMode(echoPinC, INPUT); // Sets the echoPin as an Input
  pinMode(buzzer, OUTPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); // Starts the serial communication
}


void loop() {
  // Sensor A
  digitalWrite(trigPinA, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinA, LOW);
  durationA = pulseIn(echoPinA, HIGH);
  distanceA= durationA*0.034/2; 
  safetyDistanceA = distanceA;
  if (safetyDistanceA <= 5){
    digitalWrite(buzzer, LOW);
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(buzzer, HIGH);
    digitalWrite(ledPin, LOW);
  }

  // Sensor B
  digitalWrite(trigPinB, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinB, LOW);
  durationB = pulseIn(echoPinB, HIGH);
  distanceB= durationB*0.034/2; 
  safetyDistanceB = distanceB;
  if (safetyDistanceB <= 5){
    digitalWrite(buzzer, LOW);
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(buzzer, HIGH);
    digitalWrite(ledPin, LOW);
  }
  
  // Sensor C
  digitalWrite(trigPinC, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinC, LOW);
  durationC = pulseIn(echoPinC, HIGH);
  distanceC= durationC*0.034/2; 
  safetyDistanceC = distanceC;
  if (safetyDistanceC <= 5){
    digitalWrite(buzzer, LOW);
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(buzzer, HIGH);
    digitalWrite(ledPin, LOW);
  }

  // Prints the distance on the Serial Monitor
  Serial.print("Distance A: ");
  Serial.println(distanceA);
  Serial.print("Distance B: ");
  Serial.println(distanceB);
  Serial.print("Distance C: ");
  Serial.println(distanceC);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading