GR-SAKURA
GR-KURUMI
GR-COTTON
GR-CITRUS
GR-PEACH
GR-KAEDE
GR-ADZUKI
GR-LYCHEE
GR-ROSE
GR-MANGO(*)
SNShield
Web Compiler
IDE for GR
TOPPERS関連
女子美コラボ
その他
※プロデューサミーティング中
作り方使い方資料
イベント関連
作品記事
体験記事
ライブラリ
ツール
その他・過去ファイル
SAKRAボードをやっと手に入れ、XbeeをつないでSerial出力を試して見ました。
次にデーターロガーにするためアナログデータをSDカードに記憶させようとしましたが、標準のライブラリーだとスピードが12msぐらいまでしかあがりません。
(それでもArduinoよりは早いですが)
また、1秒おき位に200msの遅延が発生します。標準のライフラリーだと限界なのでしょうか?
どなたかスピードアップ(5msくらい)の良い方法アドバイス願います。
*GR-SAKURA Sketch Template Version: V1.01*/
#include <rxduino.h>
#include <sdmmc.h>
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
unsigned long val_time;
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
SDMMC mySD;
void setup(){
pinMode(PIN_LED0,OUTPUT);
Serial.begin(9600,SCI_SCI2B); //Press any key to start communication
mySD.begin();
}
void loop(){
File myFile = mySD.open("sample.txt", FILE_WRITE);
if(myFile){
val_time = millis();
myFile.print(val_time);
myFile.print(",");
sensorValue1 = analogRead(analogInPin1);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin3);
myFile.print(sensorValue1);
myFile.print(sensorValue2);
myFile.println(sensorValue3);
myFile.close();
}else{
Serial.println("Fail to access MMC");
digitalWrite(PIN_LED0, 0);
delay(100);
1レコード記録する度にファイルの open()/close() をしてるのが重いみたいなので、書き込みをある程度の量まとめてやればいいと思います。
試しにアナログ入力を1m秒のタイマー割り込みで行い、バッファに1024個貯まる毎に書き込みをしてみたら、かなり余裕がある感じでした。
/*GR-SAKURA Sketch Template Version: V1.01*/ #include <rxduino.h> #include <sdmmc.h> const int analogInPin1 = A1; const int analogInPin2 = A2; const int analogInPin3 = A3; SDMMC mySD; typedef struct { unsigned long val_time; int sensorValue1; int sensorValue2; int sensorValue3; } RecordType; #define BufferRecords 1024 static RecordType buffer[2][BufferRecords]; static volatile int writeBank = 0; static volatile int bufferIndex[2] = {0, 0}; static void timer() { if (bufferIndex[writeBank] < BufferRecords) { RecordType* rp = &buffer[writeBank][bufferIndex[writeBank]]; rp->val_time = millis(); rp->sensorValue1 = analogRead(analogInPin1); rp->sensorValue2 = analogRead(analogInPin2); rp->sensorValue3 = analogRead(analogInPin3); if (++bufferIndex[writeBank] >= BufferRecords) { writeBank = !writeBank; } } } void setup(){ pinMode(PIN_LED0,OUTPUT); pinMode(PIN_SW, INPUT);
Serial.begin(9600,SCI_SCI2B); //Press any key to start communication mySD.begin(); timer_regist_userfunc(timer); } void loop(){ static bool save = true; int readBank = !writeBank; if (!digitalRead(PIN_SW)) { save = false; } if (save && bufferIndex[readBank] >= BufferRecords) { static RecordType temp[BufferRecords]; memcpy(temp, buffer[readBank], sizeof(temp)); bufferIndex[readBank] = 0; File myFile = mySD.open("sample.txt", FILE_WRITE); if(myFile){ for (int i = 0; i < BufferRecords; i++) { myFile.print(temp[i].val_time); myFile.print(","); myFile.print(temp[i].sensorValue1); myFile.print(","); myFile.print(temp[i].sensorValue2); myFile.print(","); myFile.println(temp[i].sensorValue3); } myFile.close(); }else{ Serial.println("Fail to access MMC"); digitalWrite(PIN_LED0, 0); delay(100); } } }