So, I am trying to controll two 230V lamps (connected to relays) with two ultrasonic sensors seperatly.
#define trigPin A0 //Define the HC-SE04 triger on pin 6 on the arduino
#define echoPin A1 //Define the HC-SE04 echo on pin 5 on the arduino
#define trigPin1 A3 //Define the HC-SE04 triger on pin 6 on the arduino
#define echoPin1 A2
#define bulb 9 //Define the relay signal on pin 9 on the arduino
#define bulb1 3 //Define the relay signal on pin 3 on the arduino
void setup()
{
Serial.begin (9600); //Start the serial monitor
pinMode(trigPin, OUTPUT); //set the trigpin to output
pinMode(echoPin, INPUT); //set the echopin to input
pinMode (bulb, OUTPUT); //set the bulb on pin 9 to output
pinMode (trigPin1, OUTPUT); //set the trigpin to output
pinMode (echoPin1, INPUT); //set the echopin to input
pinMode (bulb1, OUTPUT); //set the bulb on pin 3 to output
}
void loop()
{
int duration, distance; //Define two intregers duration and distance to be used to save data
int duration1, distance1;
digitalWrite(trigPin, HIGH); //write a digital high to the trigpin to send out the pulse
delayMicroseconds(500); //wait half a millisecond
digitalWrite(trigPin, LOW); //turn off the trigpin
digitalWrite(trigPin1, HIGH); //write a digital high to the trigpin to send out the pulse
delayMicroseconds(500); //wait half a millisecond
digitalWrite(trigPin1, LOW); //turn off the trigpin
{
duration = pulseIn(echoPin, HIGH); //measure the time using pulsein when the echo receives a signal set it to high
distance = (duration/2) / 29.1; //cm
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
if (distance < 13) //if the distance is less than 13 CM
{
digitalWrite(bulb, LOW); //turn on the light
}
else
{
digitalWrite(bulb, HIGH); //turn off the light
}
if (distance1 < 13)
{
digitalWrite(bulb1, LOW); //turn on the light
}
else
{
digitalWrite(bulb1, HIGH); //turn off the light
}
}
{
Serial.print(distance1); //Dispaly the distance on the serial monitor
Serial.println(" CM"); //in centimeters
delay(500); //delay half a second
}
}