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.
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
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
}
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
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
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