質問編集履歴
2
コードに間違いがありました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,163 +1,167 @@
|
|
1
1
|
Arduino unoで超音波距離センサを用いて観測をしたいです。しかし、下記のコードで試してみたのですがうまくSDカードにデータを記録できませんでした。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
#define echoPin 2 // Echo Pin
|
6
|
-
|
7
|
-
#define trigPin 3 // Trigger Pin
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
double Duration = 0; //受信した間隔
|
12
|
-
|
13
|
-
double Distance = 0; //距離
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
#include <SPI.h>
|
18
|
-
|
19
|
-
#include <SD.h>
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
File myFile;
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
void setup() {
|
28
|
-
|
29
|
-
Serial.begin( 9600 );
|
30
|
-
|
31
|
-
pinMode( 2, INPUT );
|
32
|
-
|
33
|
-
pinMode( 3, OUTPUT );
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
// Open serial communications and wait for port to open:
|
38
|
-
|
39
|
-
Serial.begin(9600);
|
40
|
-
|
41
|
-
while (!Serial) {
|
42
|
-
|
43
|
-
; // wait for serial port to connect. Needed for native USB port only
|
44
|
-
|
45
|
-
}
|
46
2
|
|
47
3
|
|
48
4
|
|
49
5
|
|
50
6
|
|
7
|
+
コード
|
8
|
+
|
51
|
-
|
9
|
+
```#define echoPin 2 // Echo Pin
|
10
|
+
|
11
|
+
#define trigPin 3 // Trigger Pin
|
52
12
|
|
53
13
|
|
54
14
|
|
55
|
-
|
15
|
+
double Duration = 0; //受信した間隔
|
56
16
|
|
57
|
-
|
17
|
+
double Distance = 0; //距離
|
58
|
-
|
59
|
-
return;
|
60
|
-
|
61
|
-
}
|
62
|
-
|
63
|
-
Serial.println("initialization done.");
|
64
18
|
|
65
19
|
|
66
20
|
|
67
|
-
|
21
|
+
#include <SPI.h>
|
68
22
|
|
69
|
-
// so you have to close this one before opening another.
|
70
|
-
|
71
|
-
|
23
|
+
#include <SD.h>
|
72
24
|
|
73
25
|
|
74
26
|
|
75
|
-
// if the file opened okay, write to it:
|
76
|
-
|
77
|
-
i
|
27
|
+
File myFile;
|
78
|
-
|
79
|
-
Serial.print("Writing to test.txt...");
|
80
|
-
|
81
|
-
myFile.println("testing 1, 2, 3.");
|
82
|
-
|
83
|
-
// close the file:
|
84
|
-
|
85
|
-
myFile.close();
|
86
|
-
|
87
|
-
Serial.println("done.");
|
88
|
-
|
89
|
-
} else {
|
90
|
-
|
91
|
-
// if the file didn't open, print an error:
|
92
|
-
|
93
|
-
Serial.println("error opening test.txt");
|
94
|
-
|
95
|
-
}
|
96
28
|
|
97
29
|
|
98
30
|
|
99
|
-
|
31
|
+
void setup() {
|
100
32
|
|
101
|
-
|
33
|
+
Serial.begin( 9600 );
|
102
34
|
|
103
|
-
i
|
35
|
+
pinMode( 2, INPUT );
|
104
36
|
|
105
|
-
|
37
|
+
pinMode( 3, OUTPUT );
|
106
38
|
|
107
39
|
|
108
40
|
|
109
|
-
//
|
41
|
+
// Open serial communications and wait for port to open:
|
110
42
|
|
111
|
-
|
43
|
+
Serial.begin(9600);
|
112
44
|
|
113
|
-
|
45
|
+
while (!Serial) {
|
114
46
|
|
115
|
-
|
47
|
+
; // wait for serial port to connect. Needed for native USB port only
|
116
48
|
|
117
|
-
// close the file:
|
118
|
-
|
119
|
-
myFile.close();
|
120
|
-
|
121
|
-
} else {
|
122
|
-
|
123
|
-
// if the file didn't open, print an error:
|
124
|
-
|
125
|
-
Serial.println("error opening test.txt");
|
126
|
-
|
127
|
-
}
|
49
|
+
}
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
void loop() {
|
132
|
-
|
133
|
-
digitalWrite(3, LOW);
|
134
|
-
|
135
|
-
delayMicroseconds(2);
|
136
|
-
|
137
|
-
digitalWrite( 3, HIGH ); //超音波を出力
|
138
|
-
|
139
|
-
delayMicroseconds( 10 ); //
|
140
|
-
|
141
|
-
digitalWrite( 3, LOW );
|
142
|
-
|
143
|
-
Duration = pulseIn( 2, HIGH ); //センサからの入力
|
144
|
-
|
145
|
-
if (Duration > 0) {
|
146
|
-
|
147
|
-
Duration = Duration / 2; //往復距離を半分にする
|
148
|
-
|
149
|
-
Distance = Duration * 340 * 100 / 1000000; // 音速を340m/sに設定
|
150
|
-
|
151
|
-
Serial.print("Distance:");
|
152
|
-
|
153
|
-
Serial.print(Distance);
|
154
|
-
|
155
|
-
Serial.println(" cm");
|
156
|
-
|
157
|
-
}
|
158
|
-
|
159
|
-
delay(500);
|
160
50
|
|
161
51
|
|
162
52
|
|
53
|
+
|
54
|
+
|
55
|
+
Serial.print("Initializing SD card...");
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
if (!SD.begin(4)) {
|
60
|
+
|
61
|
+
Serial.println("initialization failed!");
|
62
|
+
|
63
|
+
return;
|
64
|
+
|
163
|
-
}
|
65
|
+
}
|
66
|
+
|
67
|
+
Serial.println("initialization done.");
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
// open the file. note that only one file can be open at a time,
|
72
|
+
|
73
|
+
// so you have to close this one before opening another.
|
74
|
+
|
75
|
+
myFile = SD.open("test.txt", FILE_WRITE);
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
// if the file opened okay, write to it:
|
80
|
+
|
81
|
+
if (myFile) {
|
82
|
+
|
83
|
+
Serial.print("Writing to test.txt...");
|
84
|
+
|
85
|
+
myFile.println("testing 1, 2, 3.");
|
86
|
+
|
87
|
+
// close the file:
|
88
|
+
|
89
|
+
myFile.close();
|
90
|
+
|
91
|
+
Serial.println("done.");
|
92
|
+
|
93
|
+
} else {
|
94
|
+
|
95
|
+
// if the file didn't open, print an error:
|
96
|
+
|
97
|
+
Serial.println("error opening test.txt");
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// re-open the file for reading:
|
104
|
+
|
105
|
+
myFile = SD.open("test.txt");
|
106
|
+
|
107
|
+
if (myFile) {
|
108
|
+
|
109
|
+
Serial.println("test.txt:");
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
// read from the file until there's nothing else in it:
|
114
|
+
|
115
|
+
while (myFile.available()) {
|
116
|
+
|
117
|
+
Serial.write(myFile.read());
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
// close the file:
|
122
|
+
|
123
|
+
myFile.close();
|
124
|
+
|
125
|
+
} else {
|
126
|
+
|
127
|
+
// if the file didn't open, print an error:
|
128
|
+
|
129
|
+
Serial.println("error opening test.txt");
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
void loop() {
|
136
|
+
|
137
|
+
digitalWrite(3, LOW);
|
138
|
+
|
139
|
+
delayMicroseconds(2);
|
140
|
+
|
141
|
+
digitalWrite( 3, HIGH ); //超音波を出力
|
142
|
+
|
143
|
+
delayMicroseconds( 10 ); //
|
144
|
+
|
145
|
+
digitalWrite( 3, LOW );
|
146
|
+
|
147
|
+
Duration = pulseIn( 2, HIGH ); //センサからの入力
|
148
|
+
|
149
|
+
if (Duration > 0) {
|
150
|
+
|
151
|
+
Duration = Duration / 2; //往復距離を半分にする
|
152
|
+
|
153
|
+
Distance = Duration * 340 * 100 / 1000000; // 音速を340m/sに設定
|
154
|
+
|
155
|
+
Serial.print("Distance:");
|
156
|
+
|
157
|
+
Serial.print(Distance);
|
158
|
+
|
159
|
+
Serial.println(" cm");
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
delay(500);
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
}
|
1
コードに間違いがありました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,14 +1,4 @@
|
|
1
|
-
Arduino uno
|
1
|
+
Arduino unoで超音波距離センサを用いて観測をしたいです。しかし、下記のコードで試してみたのですがうまくSDカードにデータを記録できませんでした。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
Arduino unoに接続した超音波距離センサで観測したデータをマイクロSDカードに記録したいのですが、ソースコードがわかりません。わかる方がいらっしゃいましたら教えてください。
|
8
|
-
|
9
|
-
ちなみに超音波距離センサのEchoは3.3Vに繋いでいます。
|
10
|
-
|
11
|
-
|
12
2
|
|
13
3
|
|
14
4
|
|
@@ -22,44 +12,152 @@
|
|
22
12
|
|
23
13
|
double Distance = 0; //距離
|
24
14
|
|
15
|
+
|
16
|
+
|
17
|
+
#include <SPI.h>
|
18
|
+
|
19
|
+
#include <SD.h>
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
File myFile;
|
24
|
+
|
25
|
+
|
26
|
+
|
25
27
|
void setup() {
|
26
28
|
|
27
|
-
|
29
|
+
Serial.begin( 9600 );
|
28
30
|
|
29
|
-
|
31
|
+
pinMode( 2, INPUT );
|
30
32
|
|
31
|
-
|
33
|
+
pinMode( 3, OUTPUT );
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
// Open serial communications and wait for port to open:
|
38
|
+
|
39
|
+
Serial.begin(9600);
|
40
|
+
|
41
|
+
while (!Serial) {
|
42
|
+
|
43
|
+
; // wait for serial port to connect. Needed for native USB port only
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
Serial.print("Initializing SD card...");
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
if (!SD.begin(4)) {
|
56
|
+
|
57
|
+
Serial.println("initialization failed!");
|
58
|
+
|
59
|
+
return;
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
Serial.println("initialization done.");
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
// open the file. note that only one file can be open at a time,
|
68
|
+
|
69
|
+
// so you have to close this one before opening another.
|
70
|
+
|
71
|
+
myFile = SD.open("test.txt", FILE_WRITE);
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
// if the file opened okay, write to it:
|
76
|
+
|
77
|
+
if (myFile) {
|
78
|
+
|
79
|
+
Serial.print("Writing to test.txt...");
|
80
|
+
|
81
|
+
myFile.println("testing 1, 2, 3.");
|
82
|
+
|
83
|
+
// close the file:
|
84
|
+
|
85
|
+
myFile.close();
|
86
|
+
|
87
|
+
Serial.println("done.");
|
88
|
+
|
89
|
+
} else {
|
90
|
+
|
91
|
+
// if the file didn't open, print an error:
|
92
|
+
|
93
|
+
Serial.println("error opening test.txt");
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
// re-open the file for reading:
|
100
|
+
|
101
|
+
myFile = SD.open("test.txt");
|
102
|
+
|
103
|
+
if (myFile) {
|
104
|
+
|
105
|
+
Serial.println("test.txt:");
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
// read from the file until there's nothing else in it:
|
110
|
+
|
111
|
+
while (myFile.available()) {
|
112
|
+
|
113
|
+
Serial.write(myFile.read());
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
// close the file:
|
118
|
+
|
119
|
+
myFile.close();
|
120
|
+
|
121
|
+
} else {
|
122
|
+
|
123
|
+
// if the file didn't open, print an error:
|
124
|
+
|
125
|
+
Serial.println("error opening test.txt");
|
126
|
+
|
127
|
+
}
|
32
128
|
|
33
129
|
}
|
34
130
|
|
35
131
|
void loop() {
|
36
132
|
|
37
|
-
|
133
|
+
digitalWrite(3, LOW);
|
38
134
|
|
39
|
-
|
135
|
+
delayMicroseconds(2);
|
40
136
|
|
41
|
-
|
137
|
+
digitalWrite( 3, HIGH ); //超音波を出力
|
42
138
|
|
43
|
-
|
139
|
+
delayMicroseconds( 10 ); //
|
44
140
|
|
45
|
-
|
141
|
+
digitalWrite( 3, LOW );
|
46
142
|
|
47
|
-
|
143
|
+
Duration = pulseIn( 2, HIGH ); //センサからの入力
|
48
144
|
|
49
|
-
|
145
|
+
if (Duration > 0) {
|
50
146
|
|
51
|
-
|
147
|
+
Duration = Duration / 2; //往復距離を半分にする
|
52
148
|
|
53
|
-
|
149
|
+
Distance = Duration * 340 * 100 / 1000000; // 音速を340m/sに設定
|
54
150
|
|
55
|
-
|
151
|
+
Serial.print("Distance:");
|
56
152
|
|
57
|
-
|
153
|
+
Serial.print(Distance);
|
58
154
|
|
59
|
-
|
155
|
+
Serial.println(" cm");
|
60
|
-
|
61
|
-
}
|
62
|
-
|
63
|
-
delay(500);
|
64
156
|
|
65
157
|
}
|
158
|
+
|
159
|
+
delay(500);
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
}
|