-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mini_Sumo.ino
80 lines (77 loc) · 1.5 KB
/
Mini_Sumo.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
int duration, sm, tic, trigPin = 6, echoPin = 7, state = 0;
void forward() {
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
digitalWrite(2, HIGH);
}
void left() {
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
digitalWrite(2, LOW);
}
void right() {
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, HIGH);
}
void stop() {
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
}
void led_on() {
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
}
void led_of() {
digitalWrite(9, LOW);
digitalWrite(8, LOW);
}
void plusUltra() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
sm = duration / 58;
}
void setup() {
// Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
led_on();
delay(5000);
}
void loop() {
plusUltra();
// Serial.println(sm);
if (sm > 1 && sm < 40) {
tic ++;
} else {
tic = 0;
}
if (tic >= 2) {
if (state == 0 && millis() > 5050) {
right();
delay(10);
}
forward();
led_on();
delay(300);
state = 1;
} else {
left();
led_of();
}
}