Skip to content

Commit f67f68b

Browse files
authored
근접센서 코드
1 parent 7a5a154 commit f67f68b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

sketch_dec24a.ino

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <Adafruit_NeoPixel.h>
2+
#define trigPin 13 // trigPin을 13으로 설정합니다.
3+
#define echoPin 12 // echoPin을 12로 설정합니다.
4+
#define PIN 7
5+
#define NUM_LEDS 4
6+
7+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
8+
void setup() {
9+
pinMode(trigPin, OUTPUT); // trigPin 핀을 출력핀으로 설정합니다.
10+
pinMode(echoPin, INPUT); // echoPin 핀을 입력핀으로 설정합니다.
11+
strip.begin();
12+
strip.show();
13+
}
14+
void loop() {
15+
long duration, distance; // 각 변수를 선언합니다.
16+
digitalWrite(trigPin, LOW); // trigPin에 LOW를 출력하고
17+
delayMicroseconds(2); // 2 마이크로초가 지나면
18+
digitalWrite(trigPin, HIGH); // trigPin에 HIGH를 출력합니다.
19+
delayMicroseconds(10); // trigPin을 10마이크로초 동안 기다렸다가
20+
digitalWrite(trigPin, LOW); // trigPin에 LOW를 출력합니다.
21+
duration = pulseIn(echoPin, HIGH); // echoPin핀에서 펄스값을 받아옵니다.
22+
23+
if (distance >= 20 || distance <= 0) //50보다 안되면
24+
{
25+
strip.setPixelColor(0, 0, 0, 0);
26+
strip.setPixelColor(1, 0, 0, 0);
27+
strip.setPixelColor(2, 0, 0, 0);
28+
strip.setPixelColor(3, 0, 0, 0);
29+
}
30+
else
31+
{
32+
RGBW_Random(0,random(1,3));
33+
}
34+
strip.show();
35+
delay(10);
36+
}
37+
38+
void RGBW_Random(int pixel, int Random){
39+
switch(Random){
40+
case 1: strip.setPixelColor(1, 255, 0, 0);
41+
strip.show();
42+
delay(1000);
43+
break;
44+
case 2: strip.setPixelColor(1, 0, 255, 0);
45+
strip.show();
46+
delay(1000);
47+
break;
48+
case 3: strip.setPixelColor(1, 0, 0, 255);
49+
strip.show();
50+
delay(1000);
51+
break;
52+
}
53+
}

0 commit comments

Comments
 (0)