3 hcsr04 sensor and 3 channel relay

Hi,

I’m trying to make a sculpture to interact with the public. For this I use the Arduino to make this interaction more real as possible.
To make the sculpture react I use the hcsr04 sensor to mesure the distance of reaction.
I was able to make one sensor work with one relay module (use this relay to make 220v motors move), but when I add another one the sensors seem to think theres always an obstacle and the relay module keeps on closing the circuit.
Can you help me to solve this?
I send you the code that I’ve upload to my mega 2560.

All the best and many thanks for all your posts.

Sorry but I couldn't find how to show the code more professionally:

#define tINT 2 // Total number of interrupts
#define trigPin1 3 //Define the HC-SE04 triger on pin 6 on the arduino
#define echoPin1 2 //Define the HC-SE04 echo on pin 5 on the arduino
#define trigPin2 6 //Define the HC-SE04 triger on pin 6 on the arduino
#define echoPin2 5 //Define the HC-SE04 echo on pin 5 on the arduino

#define bulb 9 //Define the relay signal on pin 9 on the arduino
#define bulb1 10 //Define the relay signal on pin 10 on the arduino

void setup()

{

Serial.begin (9600); //Start the serial monitor

pinMode(trigPin1, OUTPUT); //set the trigpin to output
pinMode(echoPin1, INPUT); //set the echopin to input

pinMode(trigPin2, OUTPUT); //set the trigpin to output
pinMode(echoPin2, INPUT); //set the echopin to input

pinMode (bulb, OUTPUT); //set the bulb on pin 9 to output
pinMode (bulb1, OUTPUT); //set the bulb on pin 9 to output

}

void loop()

{

int duration, distance; //Define two intregers duration and distance to be used to save data

digitalWrite(trigPin1, HIGH); //write a digital high to the trigpin to send out the pulse
digitalWrite(trigPin2, HIGH); //write a digital high to the trigpin to send out the pulse

delayMicroseconds(1500); //wait half a millisecond

digitalWrite(trigPin1, LOW); //turn off the trigpin
digitalWrite(trigPin2, LOW); //turn off the trigpin

duration = pulseIn(echoPin1, HIGH); //measure the time using pulsein when the echo receives a signal set it to high
duration = pulseIn(echoPin2, HIGH); //measure the time using pulsein when the echo receives a signal set it to high

distance = (duration/2) / 29.1; //distance is the duration devided by 2 becasue the signal traveled from the trigpin then back to the echo pin, then divide by 29.1 to convert to centimeters

if (distance < 20) //if the distance is less than 20 CM

{

Light(); //execute the Light subroutine below

}

Serial.print(distance); //Dispaly the distance on the serial monitor

Serial.println(" CM"); //in centimeters

delay(500); //delay half a second

}

void Light() //Start the Light subroutine

{ digitalWrite(bulb, HIGH); //turn on the light
digitalWrite(bulb1, HIGH); //turn on the light

delay (3500); //wait 31.5 seconds 31500

digitalWrite(bulb, LOW); //turn off the light
digitalWrite(bulb1, LOW); //turn off the light

}

I suggest that you trigger the sensors one at a time with a delay between them. If triggered too close together, the ranging pulse or echo from one may be sensed by another as an echo. The sensors cannot differentiate between echos. i usually have at least 30 to 50 milliseconds between sensor triggers.

hi

do you mean something like this?

void firstsensor(){ // This function is for first sensor.
int duration1, distance1;
digitalWrite (trigSensor1, HIGH);
delayMicroseconds (10);
digitalWrite (trigSensor1, LOW);
duration1 = pulseIn (echoSensor1, HIGH);
distance1 = (duration1/2) / 29.1;

Yes, then a delay of 30 to 50 milliseconds before triggering the next sensor. The simple way would be to use the delay() function. The problem with delay() is that it blocks, nothing else can happen during a delay(). Using millis() for timing allows you to do other stuff during the waiting time.
Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

thanks!
I'll give it a try...
Hope to to do it. I'm not so good but I've to do it.

We are always willing to help if you get stuck. The more that you try, the easier it will get.

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