Hello all. I'm doing a project where I have 2 ultrasonic sensors that will control led lights wherein if 1 sensor first detects a movement it will light up led1 while the other lights up led2 based on the distance of which one detects the nearest object first. For reference I'm trying to imitate this project but from the codes he posted I can only get the distance but cant make the LEDs behave the way they were described.
Edit: Heres a code I've managed to write up by referencing other people's work that had similarities with my project.
int trigPin1 = 4;
int echoPin1 = 3;
int trigPin2 = 5;
int echoPin2 = 6;
int red1 = 13;
int red2 = 8;
int gre2 = 10;
int gre1 = 9;
long duration1, duration2;
int distance1, distance2;
void setup() {
// put your setup code here, to run once:
pinMode (trigPin1, OUTPUT);
pinMode (echoPin1, INPUT);
pinMode (trigPin2, OUTPUT);
pinMode (echoPin2, INPUT);
pinMode (red1, OUTPUT);
pinMode (red2, OUTPUT);
pinMode (gre2, OUTPUT);
pinMode (gre1, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
/*==================================
* Ultrasonic Sensor#1
==================================*/
//clear trigPin1
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
//set TRIGGER_PIN1 to High for 10 microseconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
//read the duration of the sound wave with ECHO_PIN1
duration1 = pulseIn(echoPin1, HIGH);
//calculate distance based on duration of ultrasound from triggerPin to echoPin
distance1 = duration1*0.034/2;
//add thresholds to correct for sensor value errors
if (distance1 <= 100 )
{
digitalWrite ( gre1, HIGH);
digitalWrite ( red2, HIGH);
digitalWrite (gre2, LOW);
digitalWrite (red1, LOW);
} else
{
digitalWrite ( gre1, LOW);
digitalWrite ( red2, LOW);
digitalWrite ( gre2, LOW);
digitalWrite ( red1, LOW);
}
/*==================================
* Ultrasonic Sensor#2
==================================*/
//clear TRIGGER_PIN2 value, setting it to LOW for 2 microseconds
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
//set TRIGGER_PIN2 to High for 10 microseconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
//read the duration of the sound wave with ECHO_PIN1
duration2 = pulseIn(echoPin2, HIGH);
//calculate distance based on duration of ultrasound from triggerPin to echoPin
distance2 = duration2*0.034/2;
//add thresholds to correct for sensor value errors
if (distance2 <= 100 )
{
digitalWrite (gre2, HIGH);
digitalWrite (red1, HIGH);
digitalWrite (gre1, LOW);
digitalWrite (red2, LOW);
}
else
{
digitalWrite ( gre1, LOW);
digitalWrite ( red2, LOW);
digitalWrite ( gre2, LOW);
digitalWrite ( red1, LOW);
}
/*==================================
* Print to Serial Monitor
==================================*/
Serial.print("distance1: ");
Serial.print(distance1);
Serial.println("cm");
Serial.print("distance2: ");
Serial.print(distance2);
Serial.println("cm ");
delay(500);
}
