ATmega328を用いてアナログ入力とSDカードへの書き込みのためのスケッチ(プログラム)です。 正確な値を得るために、アナログ入力の値を100回読み取り、 その平均値を求めています。「現在の時間(ミリ秒単位),読み取り電圧×100(小数点以下切り捨て)」を 記入します。SD cardへの書き込みではfloat形式はうまくいかなかったため 全て文字に変換しています。SDカードへアクセスしている間は PIN9がHIGHになりますので、ここにLEDを接続して確認用ランプとします。
//Save analog input(temperature) to a SD card by Toda Yorozu Kenkyjyo(TYK)
#include < SD.h > //SDカードライブラリーを読み込む
const int chipSelect = 8; //chipselect 端子番号(Sparkfun microSD shield では8を使用
unsigned long sensorValue = 0; //original analog read value (10 bit),10bitのアナログ読み取り値
unsigned long AnalogInV=0; //analog in voltage, アナログ電圧の読み取り値(2.75V=275)
unsigned long AveN=100; // average times for analog read, アナログ入力のための平均化回数
unsigned long i=0; // repetition, 繰り返し回数
unsigned long timeMS=0; // time in ms unit, ミリ秒
float aveAin=0; // average analog read,平均化されたアナログ入力値
String dataString = ""; // data in the string mode,データ送信用の文字列
void setup(){
analogReference(DEFAULT); //use Vcc voltage(3.3 V) as the analog input reference, アナログ入力の最大を3.3 Vとする。
Serial.begin(9600); // serial communication rate, シリアル通信のレート
Serial.print("Initializing SD card..."); //check the Serial communication
pinMode(chipSelect, OUTPUT); //Define chipselect terminal 8 as output, チップセレクトに使う8番端子はoutputとする。
if (!SD.begin(chipSelect)) { // check the SD card is available or not, SDカードが利用可能などうか確認
Serial.println("Card failed, or not present"); // in the case of SD card error, SDカード読み取りエラーの時のメッセージ
}else{
Serial.println("Card initialized."); //in the case of SD card is available, SDカードが読み取れた時のメッセージ
}
}
// main program, 主プログラム
void loop() {
sensorValue=0; // initialize the sensorValue,センサー読み取り値を0に戻す(和を取るため)
for (i=0; i< AveN; i++){ // Repeat AveN times, AveN回繰り返す。
sensorValue+=analogRead(0); // sum of analogRead(Ain terminal=0) for AveN times, 読み取り値をAveN回足す
}
timeMS=millis(); //time (msec) after the program was initiated, プログラム開始後の経過時間(ミリ秒)
aveAin= sensorValue/AveN; // average of sensor read, センサー読み取り値の平均値
AnalogInV=(long) (aveAin*330/1024); //calculate AnalogInVoltage, Analog read reference =3.3 V and 10 bit,AnalogInVの計算。3300 mVを10 bitに分割している。
dataString=String(timeMS,DEC); //make a data to send in string mode, string conversion cannot treat float numbers, 送信用の文字列データを作成
dataString += ","; // add comma, コンマを加える
dataString += String(AnalogInV,DEC); // add the AnalogInV data, AnalogInVの数字を加える。
PrintToFile(dataString); // output data to the SD cardm through the subroutine, SDカードにデータ出力するサブルーチンを呼ぶ。
delay(1000); //delay 1 sec, 1秒待つ。
}
// Subroutine for writing data in SD card, SDカードへのデータ書き込みのためのサブルーチン
void PrintToFile(String dataIn){
digitalWrite(9,HIGH); delay(20); //check data writing
File dataFile = SD.open("datalog.txt", FILE_WRITE); // define the filename, ファイル名を定義。
if (dataFile) { //if the file in the SD card was open to wrihte, SDカードの対象ファイルを開くことができれば
dataFile.println(dataIn); // write data into the file, データの記入
dataFile.close(); // close the file, ファイルを閉じる
Serial.println(dataIn); // print to the serial port too,シリアルポートにも出力して確認。
}else { // if the file isn't open, pop up an error message, ファイルが開けないときのエラーメッセージ
Serial.println("error opening file");
}
digitalWrite(9,LOW); //check data writing
}
ここで公開するアイデア/装置は安全性を保障しておりません。 用途に応じた設計を行い、十分な安全検査を行ってからご利用ください。 本サイトの情報の営利目的での利用はご遠慮ください。 本サイトの内容の無断転載を禁じます。© 2011 TYK