#define LedRed 12
#define LedGreen 13
#define speaker 12
const int pingPin_1_2 = 2; // Trigger Pin of Ultrasonic Sensor
const int echoPin1 = 8; // Echo Pin of Ultrasonic Sensor
const int echoPin2 = 9; // Echo Pin of Ultrasonic Sensor
int delay_time = 10000;
void setup() {
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(echoPin2, INPUT);
pinMode(LedRed , OUTPUT);
pinMode(LedGreen, OUTPUT);
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen , HIGH);
Serial.begin(9600); // Starting Serial Terminal
delay(250);
digitalWrite(LedRed, LOW);
digitalWrite(LedGreen , LOW);
Serial.println("starting ...");
}
void loop(){
long duration, inches, cm, duration2 , cm2, count;
for(int i = 0; i < 3; i++){
// green1 -> uv -> red2
count = 0;
for(int i = 0; i < (delay_time / 1000); i++){
// uv for set1
digitalWrite(2,1); //enables the 1st set of signals
digitalWrite(7,1);
digitalWrite(4,0);
digitalWrite(6,0);
digitalWrite(10, LOW);
delayMicroseconds(2);
digitalWrite(10, HIGH);
delayMicroseconds(10);
digitalWrite(10, LOW);
duration = pulseIn(echoPin1, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print("cm 1 = ");
Serial.print(cm);
Serial.println();
//uv for set2
// digitalWrite(10, LOW);
// delayMicroseconds(2);
// digitalWrite(10, HIGH);
// delayMicroseconds(10);
// digitalWrite(10, LOW);
// duration2 = pulseIn(echoPin2, HIGH);
// cm2 = microsecondsToCentimeters(duration2);
// Serial.print("cm 2 =");
// Serial.print(cm2);
// Serial.println();
if(cm < 200){
count += 1;
Serial.print(count);
Serial.println();
if(count > 1){
digitalWrite(LedRed, HIGH);
digitalWrite(LedGreen, LOW);
tone(speaker, 1000);
}
else{
digitalWrite(LedRed ,LOW);
digitalWrite(LedGreen, HIGH);
noTone(speaker);
}
}else{
count = 0;
digitalWrite(LedRed ,LOW);
digitalWrite(LedGreen, HIGH);
noTone(speaker);
}
delay(1000);
}
// yellow1
digitalWrite(3,1); //enables the yellow lights
digitalWrite(7,1);
digitalWrite(2,0);
delay(1000);
digitalWrite(4,1); //enables the 2nd set of signals
digitalWrite(5,1);
digitalWrite(2,0);
digitalWrite(3,0);
digitalWrite(7,0);
delay(delay_time);
digitalWrite(6,1); //enables the yellow lights
digitalWrite(4,1);
digitalWrite(5,0);
delay(1000);
}
}
long microsecondsToCentimeters(long microseconds) {
return (microseconds / 29 / 2);
}