You are on page 1of 3

int rel_ch1 = 9; //output pin for relay board channel-1 IN1;

int rel_ch2 = 10; //output pin for relay board channel-2 IN2;

int trigPin1 = 3; //trigger pin for left sensor;


int echoPin1 = 2; //echo pin for left sensor;

int trigPin2 = 5; //trigger pin for right sensor;


int echoPin2 = 4; //echo pin for right sensor;

int fan1 = 7; //connected to IN3 represent left fan;


int fan2 = 8; //connected to IN4 represent right fan;

void setup() {
// Serial.begin (9600);
pinMode(trigPin1, OUTPUT); //from where we will transmit the ultrasonic wave
(left)
pinMode(echoPin1, INPUT); //from where we will recive the feedback (left)

pinMode(trigPin2, OUTPUT); //from where we will transmit the ultrasonic wave


(right)
pinMode(echoPin2, INPUT); //from where we will recive the feedback (right)

pinMode(rel_ch1, OUTPUT); //from where we will control the left humidifier


(left)
pinMode(rel_ch2, OUTPUT); //from where we will control the right humidifier
(right)

pinMode(fan1, OUTPUT); //for left fan or fan connected to IN3;


pinMode(fan2, OUTPUT); //for left fan or fan connected to IN4;
// put your setup code here, to run once:

void bothsensor(){ // This function is for both sensor.


int duration3, distance3, duration4, distance4;
digitalWrite (trigPin1, LOW); //sets the trigger pin of both sensor on HIGH
delayMicroseconds (2);
digitalWrite (trigPin2, LOW);
delayMicroseconds (2);
digitalWrite (trigPin1, HIGH); //sets the trigger pin of left sensor on HIGH
delayMicroseconds (10); //sets the trigger pin of left sensor on HIGH for
10 micro sec
digitalWrite (trigPin2, HIGH); //sets the trigger pin of left sensor on HIGH
delayMicroseconds (10);
digitalWrite (trigPin1, LOW); //sets the trigger pin of left sensor on LOW
after 10 micro sec
digitalWrite (trigPin2, LOW); //sets the trigger pin of left sensor on LOW
after 10 micro sec
////////////CALCULATION OF DISTANCE/////////////
duration3 = pulseIn (echoPin1, HIGH); //pulseIn is function which calculates the
duration for the echopin1 to go from high to low and stored in duration1 variable;
distance3 = 0.034*(duration3/2); //Do not forget that the measured duration
is proportional to the double of the desired distance this is why we should divide
that duration by two then divide it again by 29.1 which is the speed of our wave;
duration4 = pulseIn (echoPin2, HIGH); //pulseIn is function which calculates the
duration for the echopin1 to go from high to low and stored in duration1 variable;
distance4 = 0.034*(duration4/2);

if (distance3 > 20 && distance4 > 20) { // Change the number for long or short
distances according to your need.
digitalWrite (rel_ch1, HIGH);
digitalWrite (rel_ch2, HIGH);
digitalWrite (fan1, HIGH);
digitalWrite (fan2, HIGH);
} else {
digitalWrite (rel_ch1, LOW);
digitalWrite (rel_ch2, LOW);
digitalWrite (fan1, LOW);
digitalWrite (fan2, LOW);
}
}

void firstsensor(){ // This function is for first sensor. (left)


int duration1, distance1;
digitalWrite (trigPin1, LOW); //sets the trigger pin of left sensor on HIGH
delayMicroseconds (2);
digitalWrite (trigPin1, HIGH); //sets the trigger pin of left sensor on HIGH
delayMicroseconds (10); //sets the trigger pin of left sensor on HIGH for
10 micro sec
digitalWrite (trigPin1, LOW); //sets the trigger pin of left sensor on LOW
after 10 micro sec
////////////CALCULATION OF DISTANCE/////////////
duration1 = pulseIn (echoPin1, HIGH); //pulseIn is function which calculates the
duration for the echopin1 to go from high to low and stored in duration1 variable;
distance1 = 0.034*(duration1/2); //Do not forget that the measured duration
is proportional to the double of the desired distance this is why we should divide
that duration by two then divide it again by 29.1 which is the speed of our wave;

Serial.print("1st Sensor: ");


Serial.print(distance1);
Serial.print("cm ");

if (distance1 > 20) { // Change the number for long or short distances according
to your need.
digitalWrite (rel_ch1, HIGH);
digitalWrite (fan1, HIGH);
} else {
digitalWrite (rel_ch1, LOW);
digitalWrite (fan1, LOW);
}
}

void secondsensor(){ // This function is for second sensor. (right)


int duration2, distance2;
digitalWrite (trigPin2, LOW); //sets the trigger pin of left sensor on HIGH
delayMicroseconds (2);
digitalWrite (trigPin2, HIGH); //sets the trigger pin of right sensor on HIGH
delayMicroseconds (10); //sets the trigger pin of right sensor on HIGH
for 10 micro sec
digitalWrite (trigPin2, LOW); //sets the trigger pin of right sensor on LOW
after 10 micro sec
////////////CALCULATION OF DISTANCE/////////////
duration2 = pulseIn (echoPin2, HIGH); //pulseIn is function which calculates the
duration for the echopin1 to go from high to low and stored in duration2 variable;
distance2 = 0.034*(duration2/2); //Do not forget that the measured duration
is proportional to the double of the desired distance this is why we should divide
that duration by two then divide it again by 29.1 which is the speed of our wave;
Serial.print("2nd Sensor: ");
Serial.print(distance2);
Serial.print("cm ");

if (distance2 > 20) { // Change the number for long or short distances according
to your need.
digitalWrite (rel_ch2, HIGH);
digitalWrite (fan2, HIGH);
} else {
digitalWrite (rel_ch2, LOW);
digitalWrite (fan2, LOW);
}
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println("\n");
bothsensor();
firstsensor();
secondsensor();
delay(100);
}

You might also like