SDカードに変化が有った時にPINの状態を、このようなデータを記録していきたいのです。
2014/09/19 20:18:10 0000 0000 0100 0000 0000
接続するピンの数は16系統でデジタル10アナログ6を使っています。
現在「Leonardo」+「Adafruit Data Logger Shield」を使っています。
デジタルピン10, 11, 12, 13はSDカードとして使うそうです。
またSD.hは Adafruit 用のものを使います。
前回、質問させて戴き、教えていただいたとおりにスケッチを書き換えたらコンパイルエラーは発生しなくなりました。
しかしボードに書き込んでも何も反応しません。
スケッチとボードの組み合わせが良くないのか分からないので、スケッチを確認していただけないでしょうか。
よろしくお願い致します。
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h> // Credit: Adafruit
RTC_DS1307 RTC;
#define DPINMAX 16
int Digitalpin [DPINMAX] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 14, 15, 16, 17, 18, 19};
int val = 0;// 入力ピンの状態がこの変数(val)に記憶される
int old_val[DPINMAX] = {};// valの前の値を保存しておく変数
int i;
int ChgFlag;
const int chipSelect = 10;// Adafruit のSDシールドは 10
void setup(){
// see if the card is present and can be initialized:
if (!SD.begin(10, 11, 12, 13)) {//データロガーシールド用スケッチ
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
// Check if the RTC is running.
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running");
}
// This section grabs the current datetime and compares it to
// the compilation time. If necessary, the RTC is updated.
DateTime now = RTC.now();
DateTime compiled = DateTime(__DATE__, __TIME__);
if (now.unixtime() < compiled.unixtime()) {
Serial.println("RTC is older than compile time! Updating");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
Serial.println("Setup complete.");
}
void loop(){
DateTime now = RTC.now();
File dataFile = SD.open("datalog.txt", FILE_WRITE);
{ ChgFlag = 0;/* 変化あったか確認するためのフラグをクリア */
for(i=0;i<DPINMAX;i++) {
val = digitalRead(Digitalpin[i]);
if(val != old_val[i]) {
ChgFlag = 1; /* 前回から変化ありました*/
old_val[i] = val; /* 今回の値に更新します */
}
}
if(ChgFlag != 0) /* 変化あったらからSDに書き出すよ~ */
for(i=0;i<(sizeof(old_val)/sizeof(int));i++) {
dataFile.print(old_val[i]);
if((i % 4) == 0)
dataFile.print(" ");
}
dataFile.println(); /* 最後に改行追加 */
dataFile.close();
}
}