[SD] -- [Arduino]
CS or SDCS -- D10 or D4
MOSI -- D11
MISO -- D12
CLK or SCK -- D13
#include "OneWire.h"
#include "DallasTemperature.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "SD.h"
int row =0;
LiquidCrystal_I2C lcd(0x27, 16,2);
// Arduino數位腳位9接到1-Wire裝置
#define ONE_WIRE_BUS 9
// 運用程式庫建立物件
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
unsigned long time;
void setup(void)
{
Serial.begin(9600);
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
sensors.begin();
//lcd.begin(16,2);
lcd.init();
lcd.backlight();
// initial
Serial.println("CLEARDATA");
Serial.println("LABEL,Time,Timer,Temperature1,Temperature2,");
}
void loop(void)
{
// 要求匯流排上的所有感測器進行溫度轉換
sensors.requestTemperatures();
// 參數0代表匯流排上第0個1-Wire裝置
//Serial.print("DATA,TIME");
Serial.print("DATA,TIME");
Serial.print(",");
Serial.print("TIMER");
Serial.print(",");
Serial.print(sensors.getTempCByIndex(0));
Serial.print(",");
Serial.print(sensors.getTempCByIndex(1));
Serial.println(",");
row ++;
//超過10000行資料後就覆蓋舊資料
/*
if (row > 10000)
{
row=0;
Serial.println("ROW,SET,2");
}
*/
lcd.setCursor(0, 0);
lcd.print("SensorA:");
lcd.print(sensors.getTempCByIndex(0));
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("SensorB:");
lcd.print(sensors.getTempCByIndex(1));
lcd.print("C");
File dataFile = SD.open("Data.TXT",FILE_WRITE);
if (dataFile){
time = millis();
dataFile.print(time);
dataFile.print(",");
dataFile.print(sensors.getTempCByIndex(0));
dataFile.print(",");
dataFile.println(sensors.getTempCByIndex(1));
}
dataFile.close();
delay(100);
}
//http://playground.arduino.cc/Learning/OneWire
//https://github.com/milesburton/Arduino-Temperature-Control-Library
//http://arduino-info.wikispaces.com/file/view/LiquidCrystal_I2C1602V1.zip
沒有留言:
張貼留言