0% found this document useful (0 votes)
14 views2 pages

Mission 2

The document contains an Arduino sketch that measures distance using an ultrasonic sensor. It controls two LEDs based on the measured distance, providing feedback through the serial monitor. The program categorizes distances into three ranges, triggering different LED states and messages accordingly.

Uploaded by

정원혁
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Mission 2

The document contains an Arduino sketch that measures distance using an ultrasonic sensor. It controls two LEDs based on the measured distance, providing feedback through the serial monitor. The program categorizes distances into three ranges, triggering different LED states and messages accordingly.

Uploaded by

정원혁
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

int trigPin=9;

int echoPin=8;
int LED1=5;
int LED2=6;

void setup() {
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);

// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:
float duration, distance;
digitalWrite(trigPin,HIGH);
delay(10);
digitalWrite(trigPin,LOW);

duration=pulseIn(echoPin,HIGH);
distance=((float)(340*duration/10000)/2);

Serial.print("Duration:");
Serial.print(duration);
Serial.print("|nDistance:");
Serial.print(distance);
Serial.println("cm|n");

if (distance<10) {
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
Serial.println("너무 가까워요!");
}

else if ((distance>=10) && (distance<=30)) {


digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
}

else {
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
Serial.println("가까이 와 주세요!");
}

delay(500);

You might also like