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
}