Tôi yêu Arduino gửi vào
- 131812 lượt xem
Với các công việc lập trình cần lưu trữ dữ liệu, bạn cần phải lưu trạng thái của các tín hiệu đầu vào theo thời gian. Hôm nay, mình sẽ hướng dẫn bạn làm điều đó vớ shield datalogger. Bài này chủ yếu ví dụ cho bạn cách làm sao để lưu dữ liệu của cảm biển nhiệt độ, vừa hiển thị lên LCD. Dữ liệu được lưu dưới dạng file excel.
Chuẩn bị
Nối dây
Mình nghĩ khi tìm đến bài này, các bạn đã có một lượng kiến thức nhất định nên mình sẽ không nói lại vì sao nối dây của các thiết bị như quang trở, lcd,...

Còn datalogger shield thì gắn shield lên hoy mà 
Tải về các thư viện
Bài này ta cần các thư viện sau:
- LiquidCrystal (có sẵn)
- SD (có sẵn)
- Wire (có sẵn)
- và Logger Shield (tải về tại đây)
Lập trình
#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
const int chipSelect = 10;
//đối tượng file
File logfile;
// khởi động LCD với các chân tương ứng
LiquidCrystal lcd(7, 8, 9, 4, 2, 6);
int temperaturePin = A0; //chân analog của LM35
int lightPin = A1; //chân analog của quang trở
//chân để test cảm biến ánh sáng
int lcdRed = 3;
int lcdBlue = 5;
RTC_DS1307 RTC; // định nghĩa thư viện thời gian thực - có sẵn trên board datalogger ù
//========================================================
void setup() {
// số hàng và cột của lcd
lcd.begin(16, 2);
Serial.begin(9600); //khởi chạy serial ở baudrate 9600
pinMode(lcdRed, INPUT); //đầu ra của LCD đỏ
pinMode(lcdBlue, INPUT); //đầu ra của LCD xanh
Wire.begin();//khởi chạy thư viện i2c
RTC.begin(); //khởi chạy thư viện đọc tín hiệu đồng hồ
//----------------------------------------
if (! RTC.isrunning()) {
Serial.println("Thoi gian thuc hok chay dc!");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
//-------------------------------
Serial.println("Khoi dong the SD...");
pinMode(SS, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("the co loi hoạc ko duoc cam vao");
while (1) ;//dung chuogn trinh
}
Serial.println("the khoi dong xong.");
//tạo ra các file từ logger00 -> logger99.csv theo mỗi lẩn reset board
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i / 10 + 0;
filename[7] = i % 10 + 0;
if (! SD.exists(filename)) {//nếu fle này chưa được tạo => tạo ra rồi ghi
logfile = SD.open(filename, FILE_WRITE);
break; //dừng vòng lặp tìm file ok
}
}
Serial.print("Luu vao file: ");
Serial.println(filename);
logfile.println(",,,Willy,Reen");
logfile.println("Data Logging File");
logfile.println("Date,Temp(F),Light");
}
//=========================================================
void loop() {
//đọc nhiệt độ
float temperature = getVoltage(temperaturePin);
temperature = ((temperature - .5) * 100) * 1.8 + 32;
//đọc cảm biến ánh sáng
//------------------------
int lightLevel = analogRead(lightPin);
lightLevel = map(lightLevel, 0, 900, 0, 150);
lightLevel = constrain(lightLevel, 0, 150);
// đọc cảm biến nhiệt độ
int tempLevel = analogRead(temperaturePin) * 100;
tempLevel = map(tempLevel, 66, 82, 0, 150);
tempLevel = constrain(tempLevel, 0, 150);
//cập nhập giá trị cảm biến cho độ sáng của led
analogWrite(lcdBlue, lightLevel);
analogWrite(lcdRed, tempLevel);
//-----------------------------------------------------------
String dataString = "";
//date time stuff-------------------------------------------
//-------------------------------------------------------
//logfile.println();
DateTime now = RTC.now();
logfile.print("");
logfile.print(now.year(), DEC);
logfile.print('/');
logfile.print(now.month(), DEC);
logfile.print('/');
logfile.print(now.day(), DEC);
logfile.print(' ');
logfile.print(now.hour(), DEC);
logfile.print(':');
logfile.print(now.minute(), DEC);
logfile.print(':');
logfile.print(now.second(), DEC);
logfile.print("");
logfile.print(",");
//
logfile.print(temperature);
logfile.print(",");
logfile.print(lightLevel);
delay(500);
logfile.println(dataString);
logfile.flush();
lcd.setCursor(0, 0);
lcd.print(temperature);
lcd.setCursor(0, 1);
lcd.print(lightLevel);
lcd.setCursor(8, 0);
lcd.print("Temp");
lcd.setCursor(8, 1);
lcd.print("Light");
delay(500);
}
//==========================================================
float getVoltage(int pin) {
return (analogRead(pin) * .004882814);
}
Kết luận
Chúc các bạn thành công, có thắc mắc bạn cứ bình luận phía dưới nhé!


