-
Notifications
You must be signed in to change notification settings - Fork 6
/
sname.ino
143 lines (130 loc) · 2.92 KB
/
sname.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
140
141
142
#include <SoftwareSerial.h>
//pin location
int mo1 = 7;
int mo2 = 2;
int mo3 = 3;
int mo4 = 4;
int mo5 = 5; //pwm a
int mo6 = 6; //pwm b
int trig = 8;
int echo = 9;
int led = 12;
int ledhead = 14;
const int voca = 13; //蜂鳴器
int mo5v = 130, mo6v = 100;
char K;
float cm;
SoftwareSerial BTSerial(0, 1);
char val;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(mo1, OUTPUT);
pinMode(mo2, OUTPUT);
pinMode(mo3, OUTPUT);
pinMode(mo4, OUTPUT);
pinMode(mo5, OUTPUT);
pinMode(mo6, OUTPUT);
pinMode(voca, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(led, OUTPUT);
pinMode(ledhead, OUTPUT);
pinMode(echo, INPUT);
digitalWrite(ledhead, HIGH);
BTSerial.begin(9600);
/* tone(voca, 262, 500);
noTone(100);
tone(voca, 294, 500);
noTone(100);
tone(voca, 330, 500);
noTone(100);
tone(voca, 392, 500);
noTone(100);*/
}
void loop() {
// put your main code here, to run repeatedly:
if (BTSerial.available()) {
K = BTSerial.read();
Serial.print(K);
}
digitalWrite(trig, LOW);
delay(5);
digitalWrite(trig, HIGH);
digitalWrite(trig, LOW);
cm = pulseIn(echo, HIGH) / 58.0;
delay(5);
Serial.println(cm);
if (K == '1') { //前進
digitalWrite(mo1, LOW);
digitalWrite(mo2, HIGH);
digitalWrite(mo3, HIGH);
digitalWrite(mo4, LOW);
analogWrite(mo5, mo5v);
analogWrite(mo6, mo6v);
}
if (K == '2') { //左轉
digitalWrite(mo1, LOW);
digitalWrite(mo2, HIGH);
digitalWrite(mo3, HIGH);
digitalWrite(mo4, LOW);
analogWrite(mo5, 140);
analogWrite(mo6, 70);
}
if (K == '3') { //右轉
digitalWrite(mo1, LOW);
digitalWrite(mo2, HIGH);
digitalWrite(mo3, HIGH);
digitalWrite(mo4, LOW);
analogWrite(mo5, 70);
analogWrite(mo6, 140);
}
if (K == '4') { //後退
digitalWrite(mo1, HIGH);
digitalWrite(mo2, LOW);
digitalWrite(mo3, LOW);
digitalWrite(mo4, HIGH);
analogWrite(mo5, mo5v);
analogWrite(mo6, mo6v);
}
if (K == '5') { //空檔
digitalWrite(mo1, HIGH);
digitalWrite(mo2, LOW);
digitalWrite(mo3, HIGH);
digitalWrite(mo4, LOW);
analogWrite(mo5, 0);
analogWrite(mo6, 0);
}
if (K == '6') { //煞車
digitalWrite(mo1, HIGH);
digitalWrite(mo2, HIGH);
digitalWrite(mo3, HIGH);
digitalWrite(mo4, HIGH);
analogWrite(mo5, 255);
analogWrite(mo6, 255);
}
if (K == '8') { //加速前進
digitalWrite(mo1, LOW);
digitalWrite(mo2, HIGH);
digitalWrite(mo3, HIGH);
digitalWrite(mo4, LOW);
analogWrite(mo5, 255);
analogWrite(mo6, 255);
}
if (K == '9') {
tone(voca, 262, 500); //Do
noTone(100);
tone(voca, 294, 500); //Re
noTone(100);
tone(voca, 330, 500); //Mi
noTone(100);
tone(voca, 392, 500); //Sol
noTone(100);
}
if ((cm <= 20.0) && (cm != 0.0)) {
tone(voca, 700, 200);
cm = 0.0;
}
if ((cm <= 15.0) && (cm != 0.0)) {
tone(voca, 700, cm * 20);
}
}