質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

Q&A

1回答

464閲覧

Arduino unoで超音波距離センサから取得したデータをSDカードに保存したい

kumamusi

総合スコア10

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

0グッド

0クリップ

投稿2018/10/15 08:15

編集2022/01/12 10:55

Arduino unoで超音波距離センサを用いて観測をしたいです。しかし、下記のコードで試してみたのですがうまくSDカードにデータを記録できませんでした。

コード

  #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 );   // 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);   }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

coco_bauer

2018/10/17 00:21

確認ですが、「データをマイクロSDカードに記録」する方法(コード)が判らないので教えてほしい、という質問なのですよね?
kumamusi

2018/10/18 07:19

そうです。お願いします。
coco_bauer

2018/10/18 08:26

「うまくSDカードにデータを記録できませんでした」との事ですが、シリアル通信から何が出力されますか? loop()の中は"delay(1000);"だけにして、setup()の中もSerialの初期化とSDカードの初期化、読み、書きだけにして、動作を追ってみることをお勧めします。
coco_bauer

2018/10/18 08:38

また、SDカードとArduino Unoとの接続はどうなっていますか? Arduino Uno自体にはSDカードスロットがついていないので、何らかの拡張ボード(シールド)か、ブレッドボードを使ってSDカードとつないでいると思うのですが。
kumamusi

2018/10/20 04:13

WIRELESS SD SHIELDで接続しています。
guest

回答1

0

現状のコードはどういう不具合があるのでしょうか。

ソースコードがわからないといわれても説明しようがないですが。

#SDカードの書き込みのコードが見当たりませんが

投稿2018/10/15 09:12

y_waiwai

総合スコア87719

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kumamusi

2018/10/16 07:41

#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 ); // 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); } 貼り付けるコードを間違えていました。すみません。正しいのは上記です。
y_waiwai

2018/10/16 07:50 編集

質問は編集できるので、そっちのほうを修正してください。 ソースコードは、<code>ボタンを押して、’’’の枠の中に貼り付けてください このままでは見づらいでしょ また、聞きたいことは何でしょうか
kumamusi

2018/10/18 07:21

得たデータをSDカードに記録するソースコードを教えていただきたいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問