根据第一版式的程序设计基础,做个环境...
断断续续学了好久的,终于集中心思做了个小小小小东西,纪念发贴,大神别喷。一点小曲折,不是插错线,就是写错。过程图
代码
#include <dht11.h>
#include <SD.h>
/*环境监测器
* 使用DHT11和光敏电阻制作
* 相关数据存放于SD卡内
*/
dht11 DHT11; //
#define DHT11_PIN 5//DHT11 引脚
#define LIGHT_PIN A0// 光敏模块引脚
const int chipSelect = 4;// TF卡CS选择引脚
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //初始化串口
pinMode(10,OUTPUT);//将SS引脚设置为输出状态,UNO为10号引脚
Serial.println("Initializing SD card");//初始化SD卡
if(!SD.begin(chipSelect)){
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done");
}
void loop() {
// put your main code here, to run repeatedly:
DHT11.read(DHT11_PIN);
int light = analogRead(LIGHT_PIN);
Serial.println("已打开记录文件,检测数据即将写入……");
File dataFile = SD.open("datalog.txt",FILE_WRITE);
if(dataFile){
Serial.println("从温湿度传感器读取数据……");
dataFile.print(DHT11.humidity);
Serial.print("当前湿度是:");
Serial.print(DHT11.humidity);
Serial.println("%");
dataFile.print(",");
dataFile.print(DHT11.temperature);
Serial.print("当前温度是:");
Serial.print(DHT11.temperature);
Serial.println("℃");
dataFile.print(",");
Serial.println("从光敏电阻读取光照数据……");
dataFile.println(light);
Serial.print("当前光照度是:");
Serial.print(light);
Serial.println("Lux");
dataFile.close();
}
else{
Serial.println("error opening datalog.txt");
}
Serial.println("本次检测结束,请稍等,数据马上来……");
Serial.println();
delay(3000);
}
页:
[1]