kumamusi score 10
2018/10/18 16:19 投稿
Arduino unoで超音波距離センサから取得したデータをSDカードに保存したい |
Arduino unoで超音波距離センサを用いて観測をしたいです。しかし、下記のコードで試してみたのですがうまくSDカードにデータを記録できませんでした。 |
double Duration = 0; //受信した間隔 |
double Distance = 0; //距離 |
コード |
```#define echoPin 2 // Echo Pin |
#define trigPin 3 // Trigger Pin |
double Duration = 0; //受信した間隔 |
double Distance = 0; //距離 |
File myFile; |
#include <SPI.h> |
#include <SD.h> |
void setup() { |
Serial.begin( 9600 ); |
pinMode( 2, INPUT ); |
pinMode( 3, OUTPUT ); |
File myFile; |
// Open serial communications and wait for port to open: |
Serial.begin(9600); |
while (!Serial) { |
; // wait for serial port to connect. Needed for native USB port only |
} |
void setup() { |
Serial.begin( 9600 ); |
pinMode( 2, INPUT ); |
pinMode( 3, OUTPUT ); |
// Open serial communications and wait for port to open: |
Serial.begin(9600); |
while (!Serial) { |
; // wait for serial port to connect. Needed for native USB port only |
} |
Serial.print("Initializing SD card..."); |
if (!SD.begin(4)) { |
Serial.println("initialization failed!"); |
return; |
} |
Serial.println("initialization done."); |
// open the file. note that only one file can be open at a time, |
// so you have to close this one before opening another. |
myFile = SD.open("test.txt", FILE_WRITE); |
// if the file opened okay, write to it: |
if (myFile) { |
Serial.print("Writing to test.txt..."); |
myFile.println("testing 1, 2, 3."); |
// close the file: |
myFile.close(); |
Serial.println("done."); |
} else { |
// if the file didn't open, print an error: |
Serial.println("error opening test.txt"); |
} |
// re-open the file for reading: |
myFile = SD.open("test.txt"); |
if (myFile) { |
Serial.println("test.txt:"); |
// read from the file until there's nothing else in it: |
while (myFile.available()) { |
Serial.write(myFile.read()); |
} |
// close the file: |
myFile.close(); |
} else { |
// if the file didn't open, print an error: |
Serial.println("error opening test.txt"); |
} |
} |
void loop() { |
digitalWrite(3, LOW); |
delayMicroseconds(2); |
digitalWrite( 3, HIGH ); //超音波を出力 |
delayMicroseconds( 10 ); // |
digitalWrite( 3, LOW ); |
Duration = pulseIn( 2, HIGH ); //センサからの入力 |
if (Duration > 0) { |
Duration = Duration / 2; //往復距離を半分にする |
Distance = Duration * 340 * 100 / 1000000; // 音速を340m/sに設定 |
Serial.print("Distance:"); |
Serial.print(Distance); |
Serial.println(" cm"); |
} |
delay(500); |
} |
kumamusi score 10
2018/10/18 16:15 投稿
Arduino unoで超音波距離センサから取得したデータをSDカードに保存したい |
Arduino unoに超音波距離センサを繋げ、それで得たデータをマイクロSDカードに記録したい。 |
Arduino unoに接続した超音波距離センサで観測したデータをマイクロSDカードに記録したいのですが、ソースコードがわかりません。わかる方がいらっしゃいましたら教えてください。 |
ちなみに超音波距離センサのEchoは3.3Vに繋いでいます。 |
Arduino unoで超音波距離センサを用いて観測をしたいです。しかし、下記のコードで試してみたのですがうまくSDカードにデータを記録できませんでした。 |
#define echoPin 2 // Echo Pin |
#define trigPin 3 // Trigger Pin |
double Duration = 0; //受信した間隔 |
double Distance = 0; //距離 |
#include <SPI.h> |
#include <SD.h> |
File myFile; |
void setup() { |
Serial.begin( 9600 ); |
pinMode( 2, INPUT ); |
pinMode( 3, OUTPUT ); |
Serial.begin( 9600 ); |
pinMode( 2, INPUT ); |
pinMode( 3, OUTPUT ); |
// Open serial communications and wait for port to open: |
Serial.begin(9600); |
while (!Serial) { |
; // wait for serial port to connect. Needed for native USB port only |
} |
Serial.print("Initializing SD card..."); |
if (!SD.begin(4)) { |
Serial.println("initialization failed!"); |
return; |
} |
Serial.println("initialization done."); |
// open the file. note that only one file can be open at a time, |
// so you have to close this one before opening another. |
myFile = SD.open("test.txt", FILE_WRITE); |
// if the file opened okay, write to it: |
if (myFile) { |
Serial.print("Writing to test.txt..."); |
myFile.println("testing 1, 2, 3."); |
// close the file: |
myFile.close(); |
Serial.println("done."); |
} else { |
// if the file didn't open, print an error: |
Serial.println("error opening test.txt"); |
} |
// re-open the file for reading: |
myFile = SD.open("test.txt"); |
if (myFile) { |
Serial.println("test.txt:"); |
// read from the file until there's nothing else in it: |
while (myFile.available()) { |
Serial.write(myFile.read()); |
} |
// close the file: |
myFile.close(); |
} else { |
// if the file didn't open, print an error: |
Serial.println("error opening test.txt"); |
} |
} |
void loop() { |
digitalWrite(3, LOW); |
delayMicroseconds(2); |
digitalWrite( 3, HIGH ); //超音波を出力 |
delayMicroseconds( 10 ); // |
digitalWrite( 3, LOW ); |
Duration = pulseIn( 2, HIGH ); //センサからの入力 |
if (Duration > 0) { |
Duration = Duration / 2; //往復距離を半分にする |
Distance = Duration * 340 * 100 / 1000000; // 音速を340m/sに設定 |
Serial.print("Distance:"); |
Serial.print(Distance); |
Serial.println(" cm"); |
} |
delay(500); |
} |
digitalWrite(3, LOW); |
delayMicroseconds(2); |
digitalWrite( 3, HIGH ); //超音波を出力 |
delayMicroseconds( 10 ); // |
digitalWrite( 3, LOW ); |
Duration = pulseIn( 2, HIGH ); //センサからの入力 |
if (Duration > 0) { |
Duration = Duration / 2; //往復距離を半分にする |
Distance = Duration * 340 * 100 / 1000000; // 音速を340m/sに設定 |
Serial.print("Distance:"); |
Serial.print(Distance); |
Serial.println(" cm"); |
} |
delay(500); |
} |