-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_project.ino
139 lines (126 loc) · 3.55 KB
/
arduino_project.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
int wheelLeftFront = 9, wheelLeftBack = 10;
int wheelRightFront = 6, wheelRightBack = 5;
byte LED[3] = {7,8,4};
int trig = 2, echo = 3;
int trig2 = 12, echo2 = 11; // 초음파센서 설정
int spdL = 130, spdR = 150;
int sppL = -500, sppR = -500; // 속도
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(trig2, OUTPUT);
pinMode(echo, INPUT);
pinMode(echo2, INPUT);
pinMode(wheelLeftFront, OUTPUT);
pinMode(wheelLeftBack, OUTPUT);
pinMode(wheelRightFront, OUTPUT);
pinMode(wheelRightBack, OUTPUT);
for(int i=0;i<3;i++)
{
pinMode(LED[i], OUTPUT);
}
}
void loop() {
//초음파센서 거리 측정 및 시리얼 모니터 출력
float distance, duration;
digitalWrite(trig, LOW); //초기화
digitalWrite(trig, HIGH);
delay(1);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = ((float)(340 * duration) / 10000) / 2;
Serial.println(distance);
if(distance > 15)
// 장해물이 앞에 없을 때 전진, 초록
{
digitalWrite(LED[0], HIGH);
digitalWrite(LED[1], LOW);
digitalWrite(LED[2], LOW);
analogWrite(wheelLeftFront, spdL);
analogWrite(wheelLeftBack, 0);
analogWrite(wheelRightFront, spdR);
analogWrite(wheelRightBack, 0);
}
else if(distance > 10) //노랑
{
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], HIGH);
digitalWrite(LED[2], LOW);
analogWrite(wheelLeftFront, spdL);
analogWrite(wheelLeftBack, 0);
analogWrite(wheelRightFront, spdR);
analogWrite(wheelRightBack, 0);
}
else if(distance < 10)
{
// 장해물이 15cm 이내에 있을 경우 방향전환, 빨강
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], LOW);
digitalWrite(LED[2], HIGH);
int rd = random(2); // 좌, 우 랜덤 선택 랜덤 값(0,1)
int delayed = random(200,500);
if(rd == 1)
{
// 좌회전
analogWrite(wheelLeftFront, 0);
analogWrite(wheelLeftBack, spdL);
analogWrite(wheelRightFront, spdR);
analogWrite(wheelRightBack, 0);
}
else
{
// 우회전
analogWrite(wheelLeftFront, spdL);
analogWrite(wheelLeftBack, 0);
analogWrite(wheelRightFront, 0);
analogWrite(wheelRightBack, spdR);
}
delay(delayed);
}
float distance1, duration1;
digitalWrite(trig2, LOW); //초기화
digitalWrite(trig2, HIGH);
delay(1);
digitalWrite(trig2, LOW);
duration1 = pulseIn(echo2, HIGH);
distance1 = ((float)(340 * duration1) / 10000) / 2;
Serial.println(distance1);
if(distance1 > 10)
{
if(distance > 10)
{
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], LOW);
digitalWrite(LED[2], HIGH);
analogWrite(wheelLeftFront, sppL);
analogWrite(wheelLeftBack, spdL);
analogWrite(wheelRightFront, sppR);
analogWrite(wheelRightBack, spdL);
delay(1000);
analogWrite(wheelLeftFront, 0);
analogWrite(wheelLeftBack, spdL);
analogWrite(wheelRightFront, 0);
analogWrite(wheelRightBack, spdR);
delay(500);
int rd = random(2); // 좌, 우 랜덤 선택 랜덤 값(0,1)
int delayed = random(200,500);
if(rd == 1)
{
// 좌회전
analogWrite(wheelLeftFront, 0);
analogWrite(wheelLeftBack, spdL);
analogWrite(wheelRightFront, spdR);
analogWrite(wheelRightBack, 0);
}
else
{
// 우회전
analogWrite(wheelLeftFront, spdL);
analogWrite(wheelLeftBack, 0);
analogWrite(wheelRightFront, 0);
analogWrite(wheelRightBack, spdR);
}
delay(delayed);
}
}
}