Multiple Distance Sensors - ROLL EMPTY ALARM MANUFACTURING

Hi, Very new to this so any help is much appreciated.

My application is a distance sensor to stop a machine when a roll of material is empty.
See attached video iCloud
and WORKING code, I have this working well with one distance sensor.
The machine holds 4 rolls so I need 4 sensors and for the buzzer to sound if any of the sensors do not detect material.

I have started / finished my code but doesn't work! see code file NOT WORKING.

Also: If I have only 3 rolls i.e. 3 sensors. Can I just unplug a sensor? or will that break the code.

Thank you in advance for any help.

roll alam 4 sensor version NOT WORKING.txt (3.54 KB)

working roll alam WORKING.txt (1.24 KB)

Too much code

but doesn't work!

You seem to have copy/pasted the code for a single sensor multiple times including the declaration of variables which will produce errors like

redeclaration of 'int duration'

At the very least remove all but the first declarations of such variables. You can shorten you code in a number of ways but get the verbose version working first

int const trigPin1 = 2;
int const echoPin1 = 3;

int const trigPin2 = 4;
int const echoPin2 = 5;

int const trigPin3 = 6;
int const echoPin3 = 7;

int const trigPin4 = 8;
int const echoPin4 = 9;

int const buzzPin = 10;

void setup() {
pinMode(trigPin1, OUTPUT); // trig pin will have pulses output
pinMode(echoPin1, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering

}

void loop() {

int duration, distance;
digitalWrite(trigPin1, HIGH);
delay(1);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 15.1;
if (distance <= 50 && distance >= 0) { // if distance less than 0.5 meter and more than 0 (0 or less means over range)
digitalWrite(buzzPin, LOW); // Don't buzz
} else {
digitalWrite(buzzPin, HIGH); // Buzz
}

delay(60);

}

Image of circuit

Please use the code button </> when posting code

so it 
looks like this

See How to get the best out of the Forum

...R

@battie1980 - where is the code for the 3 other sensors ?

Do I add them all to the void setup?

Sorry if I sound like a dummy this is my first project!

int const trigPin1 = 2;
int const echoPin1 = 3;

int const trigPin2 = 4;
int const echoPin2 = 5;

int const trigPin3 = 6;
int const echoPin3 = 7;

int const trigPin4 = 8;
int const echoPin4 = 9;

int const buzzPin = 10;


void setup() {
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT); 

pinMode(trigPin2, OUTPUT); 
pinMode(echoPin2, INPUT); 

pinMode(trigPin3, OUTPUT); 
pinMode(echoPin3, INPUT); 

pinMode(trigPin3, OUTPUT); 
pinMode(echoPin3, INPUT); 

pinMode(buzzPin, OUTPUT); 

}

void loop() {

int duration, distance;
digitalWrite(trigPin1, HIGH);
delay(1);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin4, HIGH);
distance = (duration/2) / 15.1;
if (distance <= 50 && distance >= 0) {  // if distance less than 0.5 meter and more than 0 (0 or less means over range)
digitalWrite(buzzPin, LOW); // Don't buzz
} else {
digitalWrite(buzzPin, HIGH); // Buzz
}

delay(60);

Your code from post#3 errors because echoPin has not been declared, but echoPin1 has. Fix that first then you can add reading the other 3 distances as you do for #1. Do that first by copying the code for #1 3 times and changing the pin numbers. Post the code here and we can tackle improving it by using arrays and a for loop to reduce the size of the code

Your code posted in code tags is incomplete

int const trigPin1 = 2;
int const echoPin1 = 3;

int const trigPin2 = 4;
int const echoPin2 = 5;

int const trigPin3 = 6;
int const echoPin3 = 7;

int const trigPin4 = 8;
int const echoPin4 = 9;

int const buzzPin = 10;


void setup() {

pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT); 

pinMode(trigPin2, OUTPUT); 
pinMode(echoPin2, INPUT); 

pinMode(trigPin3, OUTPUT); 
pinMode(echoPin3, INPUT); 

pinMode(trigPin3, OUTPUT); 
pinMode(echoPin3, INPUT); 

pinMode(buzzPin, OUTPUT); 

}

void loop() {

int duration, distance;

digitalWrite(trigPin1, HIGH);
delay(1);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);

digitalWrite(trigPin2, HIGH);
delay(1);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin2, HIGH);

digitalWrite(trigPin3, HIGH);
delay(1);
digitalWrite(trigPin3, LOW);
duration = pulseIn(echoPin3, HIGH);

digitalWrite(trigPin4, HIGH);
delay(1);
digitalWrite(trigPin4, LOW);
duration = pulseIn(echoPin4, HIGH);


distance = (duration/2) / 15.1;
if (distance <= 50 && distance >= 0) {  // if distance less than 0.5 meter and more than 0 (0 or less means over range)
digitalWrite(buzzPin, LOW); // Don't buzz
} else {
digitalWrite(buzzPin, HIGH); // Buzz
}

delay(60);

}

It compiles but each each of these

 duration = pulseIn(echoPin1, HIGH);
  duration = pulseIn(echoPin2, HIGH);
  duration = pulseIn(echoPin3, HIGH);
  duration = pulseIn(echoPin4, HIGH);

overwrites the value of duration. So when you finally do this

  distance = (duration / 2) / 15.1;

you are only using the value of duration from echoPin4. If you need separate distances to be saved then you need separate variables for each of them

It is time for you to explain exactly what you are trying to do
Should the buzzer sound if any of the distances are less than 50 or should the buzzer sound if all of the distances are less than 50, or maybe something else

UKHeliBob:
It compiles but each each of these

 duration = pulseIn(echoPin1, HIGH);
  duration = pulseIn(echoPin2, HIGH);
  duration = pulseIn(echoPin3, HIGH);
  duration = pulseIn(echoPin4, HIGH);

overwrites the value of duration. So when you finally do this

  distance = (duration / 2) / 15.1;

you are only using the value of duration from echoPin4. If you need separate distances to be saved then you need separate variables for each of them

It is time for you to explain exactly what you are trying to do
Should the buzzer sound if any of the distances are less than 50 or should the buzzer sound if all of the distances are less than 50, or maybe something else

The buzzer should sound if any of the sensors are less than 50. Each sensor will be mounted above a roll of material when any of the 4 rolls of material are empty I want the machine to stop / sound buzzer.

The easy way would be to calculate the value of distance each time you read the duration and sound the buzzer if the distance is less than 50 rather than calculating the distance just once

Thanks again for your help think I am starting to understand the code a little better! How's this?

int const trigPin1 = 2;
int const echoPin1 = 3;

int const trigPin2 = 4;
int const echoPin2 = 5;

int const trigPin3 = 6;
int const echoPin3 = 7;

int const trigPin4 = 8;
int const echoPin4 = 9;

int const buzzPin = 10;


void setup() {

pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT); 

pinMode(trigPin2, OUTPUT); 
pinMode(echoPin2, INPUT); 

pinMode(trigPin3, OUTPUT); 
pinMode(echoPin3, INPUT); 

pinMode(trigPin3, OUTPUT); 
pinMode(echoPin3, INPUT); 

pinMode(buzzPin, OUTPUT); 

}

void loop() {

int duration, distance;

digitalWrite(trigPin1, HIGH);
delay(1);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance = (duration/2) / 15.1;
if (distance <= 50 && distance >= 0) {

digitalWrite(trigPin2, HIGH);
delay(1);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin2, HIGH);
distance = (duration/2) / 15.1;
if (distance <= 50 && distance >= 0) {

digitalWrite(trigPin3, HIGH);
delay(1);
digitalWrite(trigPin3, LOW);
duration = pulseIn(echoPin3, HIGH);
distance = (duration/2) / 15.1;
if (distance <= 50 && distance >= 0) {

digitalWrite(trigPin4, HIGH);
delay(1);
digitalWrite(trigPin4, LOW);
duration = pulseIn(echoPin4, HIGH);
distance = (duration/2) / 15.1;
if (distance <= 50 && distance >= 0) {



digitalWrite(buzzPin, LOW); // Don't buzz
} else {
digitalWrite(buzzPin, HIGH); // Buzz
}

delay(60);

}

Try using the auto format tool in the IDE, and see if your scheme looks like you expect.

I wouldn't have thought each ping would be dependent upon the previous one, but I'm not sure what you had in mind.

Your code is incomplete. Possible a copy/paste error but maybe wrong in the IDE

Auto Format your code in the IDE. Does the code end with a } on the last line ? If no, then your code is wrong. Does it compile ?

Hold on !
You didn't close the braces for the distance checks, so you have the second if inside the first and the third inside the second and so on

Auto Format is your friend in showing up such things but it will not fix them

When you have got this verbose version working we can move on to the clever stuff

Code after AUTO FORMAT

int const trigPin1 = 2;
int const echoPin1 = 3;

int const trigPin2 = 4;
int const echoPin2 = 5;

int const trigPin3 = 6;
int const echoPin3 = 7;

int const trigPin4 = 8;
int const echoPin4 = 9;

int const buzzPin = 10;


void setup() {

  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

  pinMode(buzzPin, OUTPUT);

}

void loop() {

  int duration, distance;

  digitalWrite(trigPin1, HIGH);
  delay(1);
  digitalWrite(trigPin1, LOW);
  duration = pulseIn(echoPin1, HIGH);
  distance = (duration / 2) / 15.1;
  if (distance <= 50 && distance >= 0)

    digitalWrite(trigPin2, HIGH);
  delay(1);
  digitalWrite(trigPin2, LOW);
  duration = pulseIn(echoPin2, HIGH);
  distance = (duration / 2) / 15.1;
  if (distance <= 50 && distance >= 0)

    digitalWrite(trigPin3, HIGH);
  delay(1);
  digitalWrite(trigPin3, LOW);
  duration = pulseIn(echoPin3, HIGH);
  distance = (duration / 2) / 15.1;
  if (distance <= 50 && distance >= 0)

    digitalWrite(trigPin4, HIGH);
  delay(1);
  digitalWrite(trigPin4, LOW);
  duration = pulseIn(echoPin4, HIGH);
  distance = (duration / 2) / 15.1;
  if (distance <= 50 && distance >= 0)
  {



    digitalWrite(buzzPin, LOW); // Don't buzz
  } else {
    digitalWrite(buzzPin, HIGH); // Buzz
  }

  delay(60);

}

That's not the same as the code in reply #13.

That's not going to work at all - you're not triggering the sensors correctly.

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