const int trigPin = 9;
const int echoPin = 10;
long duration;
int dCm, dInch;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
for(int i = 1 ; i<=6;i++)
{
pinMode(i,OUTPUT);
}
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
dCm= duration*0.034/2;
dInch = duration*0.0133/2;
if(dCm<2){
digitalWrite(6,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
}
else if(dCm>2&&dCm < 5 ){
digitalWrite(6,HIGH);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
}
else if(dCm > 5 && dCm <10){
digitalWrite(6,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
}
else if(dCm > 10 && dCm <15){
digitalWrite(6,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
}
else if(dCm > 15 && dCm <20){
digitalWrite(6,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
}else if(dCm>20){
digitalWrite(6,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
}
Serial.print("Distance: "); // Prints string "Distance"
Serial.print(dCm); // Prints the distance value from the sensor
Serial.println(" cm");
delay(10);
Serial.print("Distance: ");
Serial.print(dInch);
Serial.println(" inch");
delay(10);
}
Comments