https://youtu.be/f-yGhTxBH-g
Program:
#define trigPin 10
#define echoPin 13
#define ledred 7
#define ledgreen 6
float duration, distance;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledred, OUTPUT);
pinMode(ledgreen, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0343;
Serial.print("Distance = ");
if (distance >= 400 || distance <= 2) {
Serial.println("Out of range");
Serial.print(distance);
}
else {
Serial.print(distance);
Serial.println(" cm");
delay(500);
if (distance <= 15)
{
digitalWrite(ledgreen, LOW);
digitalWrite(ledred, HIGH);
delay(2000);
digitalWrite(ledred, LOW);
delay(2000);
}
else
{
digitalWrite(ledred, LOW);
digitalWrite(ledgreen, HIGH);
delay(2000);
digitalWrite(ledgreen, LOW);
delay(2000);
}
}
delay(500);
}
No comments:
Post a Comment