I am trying to make an alarm device that Alarm when some one try to enter (going up escalator) to avoid an accident.
Idea is that install 2 sensors and one led Idea is that if S1 trigger 1st and then S2 LED will be lights up.
and vice versa do nothing.
int ledPin1 = 3;
int sensor1 =0;
int sensor2 =0;
int trigPin1 = 6;
int echoPin1 = 7;
int trigPin2 = 8;
int echoPin2 = 9;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(ledPin1, OUTPUT);
}
void loop() {
Serial.println("\n");
int duration1, distance1;
digitalWrite (trigPin1, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin1, LOW);
duration1 = pulseIn (echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
Serial.print("1st Sensor: ");
Serial.print(distance1);
Serial.print("cm ");
if (distance1 <= 80){ // Change the number for long or short distances.
sensor1 =1;
} else {
sensor1 =0;
}
Serial.print(sensor1 );
int duration2, distance2;
digitalWrite (trigPin2, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin2, LOW);
duration2 = pulseIn (echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;
Serial.print(" 2nd Sensor: ");
Serial.print(distance2);
Serial.print("cm ");
if (distance2 <= 80 ) { // Change the number for long or short distances.
sensor2 =1;
}else {
sensor2 =0;
}
Serial.print(sensor2);
// wrong way Control logic
if (sensor1 == (1) && sensor2 == (1)) {
digitalWrite (ledPin1, HIGH);
}
else {
digitalWrite (ledPin1, LOW);
}
if (sensor1 == (0) && sensor2 == (0)) {
//digitalWrite (ledPin1, LOW);
//sensor2 = 0;
}
else {
// digitalWrite (ledPin1, LOW);
}
}
