質問編集履歴
2
コードに間違いがありました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,82 +1,84 @@
|
|
1
1
|
Arduino unoで超音波距離センサを用いて観測をしたいです。しかし、下記のコードで試してみたのですがうまくSDカードにデータを記録できませんでした。
|
2
2
|
|
3
|
-
#define echoPin 2 // Echo Pin
|
4
|
-
#define trigPin 3 // Trigger Pin
|
5
3
|
|
4
|
+
コード
|
6
|
-
|
5
|
+
```#define echoPin 2 // Echo Pin
|
7
|
-
|
6
|
+
#define trigPin 3 // Trigger Pin
|
8
7
|
|
8
|
+
double Duration = 0; //受信した間隔
|
9
|
-
|
9
|
+
double Distance = 0; //距離
|
10
|
-
#include <SD.h>
|
11
10
|
|
11
|
+
#include <SPI.h>
|
12
|
-
|
12
|
+
#include <SD.h>
|
13
13
|
|
14
|
-
void setup() {
|
15
|
-
|
14
|
+
File myFile;
|
16
|
-
pinMode( 2, INPUT );
|
17
|
-
pinMode( 3, OUTPUT );
|
18
15
|
|
19
|
-
|
16
|
+
void setup() {
|
20
|
-
Serial.begin(9600);
|
17
|
+
Serial.begin( 9600 );
|
18
|
+
pinMode( 2, INPUT );
|
21
|
-
|
19
|
+
pinMode( 3, OUTPUT );
|
22
|
-
; // wait for serial port to connect. Needed for native USB port only
|
23
|
-
}
|
24
20
|
|
21
|
+
// Open serial communications and wait for port to open:
|
22
|
+
Serial.begin(9600);
|
23
|
+
while (!Serial) {
|
24
|
+
; // wait for serial port to connect. Needed for native USB port only
|
25
|
+
}
|
25
26
|
|
26
|
-
Serial.print("Initializing SD card...");
|
27
27
|
|
28
|
-
if (!SD.begin(4)) {
|
29
|
-
Serial.
|
28
|
+
Serial.print("Initializing SD card...");
|
30
|
-
return;
|
31
|
-
}
|
32
|
-
Serial.println("initialization done.");
|
33
29
|
|
34
|
-
|
30
|
+
if (!SD.begin(4)) {
|
35
|
-
|
31
|
+
Serial.println("initialization failed!");
|
32
|
+
return;
|
33
|
+
}
|
36
|
-
|
34
|
+
Serial.println("initialization done.");
|
37
35
|
|
38
|
-
//
|
36
|
+
// open the file. note that only one file can be open at a time,
|
39
|
-
if (myFile) {
|
40
|
-
|
37
|
+
// so you have to close this one before opening another.
|
41
|
-
myFile.
|
38
|
+
myFile = SD.open("test.txt", FILE_WRITE);
|
42
|
-
// close the file:
|
43
|
-
myFile.close();
|
44
|
-
Serial.println("done.");
|
45
|
-
} else {
|
46
|
-
// if the file didn't open, print an error:
|
47
|
-
Serial.println("error opening test.txt");
|
48
|
-
}
|
49
39
|
|
50
|
-
//
|
40
|
+
// if the file opened okay, write to it:
|
51
|
-
myFile = SD.open("test.txt");
|
52
|
-
if (myFile) {
|
41
|
+
if (myFile) {
|
42
|
+
Serial.print("Writing to test.txt...");
|
43
|
+
myFile.println("testing 1, 2, 3.");
|
44
|
+
// close the file:
|
45
|
+
myFile.close();
|
53
|
-
Serial.println("
|
46
|
+
Serial.println("done.");
|
47
|
+
} else {
|
48
|
+
// if the file didn't open, print an error:
|
49
|
+
Serial.println("error opening test.txt");
|
50
|
+
}
|
54
51
|
|
55
|
-
// read from the file until there's nothing else in it:
|
56
|
-
while (myFile.available()) {
|
57
|
-
Serial.write(myFile.read());
|
58
|
-
}
|
59
|
-
//
|
52
|
+
// re-open the file for reading:
|
60
|
-
myFile.close();
|
61
|
-
} else {
|
62
|
-
// if the file didn't open, print an error:
|
63
|
-
|
53
|
+
myFile = SD.open("test.txt");
|
64
|
-
}
|
65
|
-
}
|
66
|
-
|
54
|
+
if (myFile) {
|
67
|
-
digitalWrite(3, LOW);
|
68
|
-
delayMicroseconds(2);
|
69
|
-
digitalWrite( 3, HIGH ); //超音波を出力
|
70
|
-
delayMicroseconds( 10 ); //
|
71
|
-
digitalWrite( 3, LOW );
|
72
|
-
Duration = pulseIn( 2, HIGH ); //センサからの入力
|
73
|
-
if (Duration > 0) {
|
74
|
-
Duration = Duration / 2; //往復距離を半分にする
|
75
|
-
Distance = Duration * 340 * 100 / 1000000; // 音速を340m/sに設定
|
76
|
-
Serial.
|
55
|
+
Serial.println("test.txt:");
|
77
|
-
Serial.print(Distance);
|
78
|
-
Serial.println(" cm");
|
79
|
-
}
|
80
|
-
delay(500);
|
81
56
|
|
57
|
+
// read from the file until there's nothing else in it:
|
58
|
+
while (myFile.available()) {
|
59
|
+
Serial.write(myFile.read());
|
82
|
-
}
|
60
|
+
}
|
61
|
+
// close the file:
|
62
|
+
myFile.close();
|
63
|
+
} else {
|
64
|
+
// if the file didn't open, print an error:
|
65
|
+
Serial.println("error opening test.txt");
|
66
|
+
}
|
67
|
+
}
|
68
|
+
void loop() {
|
69
|
+
digitalWrite(3, LOW);
|
70
|
+
delayMicroseconds(2);
|
71
|
+
digitalWrite( 3, HIGH ); //超音波を出力
|
72
|
+
delayMicroseconds( 10 ); //
|
73
|
+
digitalWrite( 3, LOW );
|
74
|
+
Duration = pulseIn( 2, HIGH ); //センサからの入力
|
75
|
+
if (Duration > 0) {
|
76
|
+
Duration = Duration / 2; //往復距離を半分にする
|
77
|
+
Distance = Duration * 340 * 100 / 1000000; // 音速を340m/sに設定
|
78
|
+
Serial.print("Distance:");
|
79
|
+
Serial.print(Distance);
|
80
|
+
Serial.println(" cm");
|
81
|
+
}
|
82
|
+
delay(500);
|
83
|
+
|
84
|
+
}
|
1
コードに間違いがありました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,33 +1,82 @@
|
|
1
|
-
Arduino uno
|
1
|
+
Arduino unoで超音波距離センサを用いて観測をしたいです。しかし、下記のコードで試してみたのですがうまくSDカードにデータを記録できませんでした。
|
2
2
|
|
3
|
-
|
4
|
-
Arduino unoに接続した超音波距離センサで観測したデータをマイクロSDカードに記録したいのですが、ソースコードがわかりません。わかる方がいらっしゃいましたら教えてください。
|
5
|
-
ちなみに超音波距離センサのEchoは3.3Vに繋いでいます。
|
6
|
-
|
7
|
-
|
8
3
|
#define echoPin 2 // Echo Pin
|
9
4
|
#define trigPin 3 // Trigger Pin
|
10
5
|
|
11
6
|
double Duration = 0; //受信した間隔
|
12
7
|
double Distance = 0; //距離
|
8
|
+
|
9
|
+
#include <SPI.h>
|
10
|
+
#include <SD.h>
|
11
|
+
|
12
|
+
File myFile;
|
13
|
+
|
13
14
|
void setup() {
|
14
|
-
|
15
|
+
Serial.begin( 9600 );
|
15
|
-
|
16
|
+
pinMode( 2, INPUT );
|
16
|
-
|
17
|
+
pinMode( 3, OUTPUT );
|
18
|
+
|
19
|
+
// Open serial communications and wait for port to open:
|
20
|
+
Serial.begin(9600);
|
21
|
+
while (!Serial) {
|
22
|
+
; // wait for serial port to connect. Needed for native USB port only
|
17
23
|
}
|
24
|
+
|
25
|
+
|
26
|
+
Serial.print("Initializing SD card...");
|
27
|
+
|
28
|
+
if (!SD.begin(4)) {
|
29
|
+
Serial.println("initialization failed!");
|
30
|
+
return;
|
31
|
+
}
|
32
|
+
Serial.println("initialization done.");
|
33
|
+
|
34
|
+
// open the file. note that only one file can be open at a time,
|
35
|
+
// so you have to close this one before opening another.
|
36
|
+
myFile = SD.open("test.txt", FILE_WRITE);
|
37
|
+
|
38
|
+
// if the file opened okay, write to it:
|
39
|
+
if (myFile) {
|
40
|
+
Serial.print("Writing to test.txt...");
|
41
|
+
myFile.println("testing 1, 2, 3.");
|
42
|
+
// close the file:
|
43
|
+
myFile.close();
|
44
|
+
Serial.println("done.");
|
45
|
+
} else {
|
46
|
+
// if the file didn't open, print an error:
|
47
|
+
Serial.println("error opening test.txt");
|
48
|
+
}
|
49
|
+
|
50
|
+
// re-open the file for reading:
|
51
|
+
myFile = SD.open("test.txt");
|
52
|
+
if (myFile) {
|
53
|
+
Serial.println("test.txt:");
|
54
|
+
|
55
|
+
// read from the file until there's nothing else in it:
|
56
|
+
while (myFile.available()) {
|
57
|
+
Serial.write(myFile.read());
|
58
|
+
}
|
59
|
+
// close the file:
|
60
|
+
myFile.close();
|
61
|
+
} else {
|
62
|
+
// if the file didn't open, print an error:
|
63
|
+
Serial.println("error opening test.txt");
|
64
|
+
}
|
65
|
+
}
|
18
66
|
void loop() {
|
19
|
-
|
67
|
+
digitalWrite(3, LOW);
|
20
|
-
|
68
|
+
delayMicroseconds(2);
|
21
|
-
|
69
|
+
digitalWrite( 3, HIGH ); //超音波を出力
|
22
|
-
|
70
|
+
delayMicroseconds( 10 ); //
|
23
|
-
|
71
|
+
digitalWrite( 3, LOW );
|
24
|
-
|
72
|
+
Duration = pulseIn( 2, HIGH ); //センサからの入力
|
25
|
-
|
73
|
+
if (Duration > 0) {
|
26
|
-
|
74
|
+
Duration = Duration / 2; //往復距離を半分にする
|
27
|
-
|
75
|
+
Distance = Duration * 340 * 100 / 1000000; // 音速を340m/sに設定
|
28
|
-
|
76
|
+
Serial.print("Distance:");
|
29
|
-
|
77
|
+
Serial.print(Distance);
|
30
|
-
|
78
|
+
Serial.println(" cm");
|
31
|
-
|
79
|
+
}
|
32
|
-
|
80
|
+
delay(500);
|
81
|
+
|
33
82
|
}
|