将3个独立运行的DataLogger代码合并为一个时功能异常,寻求解决方案
合并3份DataLogger代码的修复方案
现有代码核心问题
- 结构错误:运动检测逻辑完全游离在
setup和自定义函数之外,不属于任何函数作用域,直接编译失败 - 引脚配置缺失:LED、运动传感器未设置输入输出模式,硬件无法正常工作
- 引脚冲突:温度、光敏、运动三个传感器全部定义为读取A0,数据互相干扰无法正常读数
- 语法错误:
loop、measure函数大括号不闭合,字符串内强制换行导致CSV行断裂,编译无法通过 - 逻辑错误:SD卡文件仅在
setup阶段打开一次,异常断电极易丢失数据;多处delay堆叠导致采样时序完全混乱 - 表头不匹配:仅写入亮度表头,后续写入温度、运动数据时CSV结构混乱无法正常读取
修复后完整可运行代码
#include <SPI.h> #include <SD.h> #include "RTClib.h" RTC_DS1307 rtc; // 引脚定义 三个传感器分开引脚避免冲突 const int pinLight = A0; // 光敏传感器 const int pinTemp = A2; // 温度传感器 const int pinMotion = A1; // 运动传感器 const int pinLed = 13; const int chipSelect = 10; int motionState = LOW; const char* filename = "DataLogger.csv"; void setup() { Serial.begin(9600); #ifndef ESP8266 while (!Serial); #endif // 初始化RTC if (!rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); abort(); } if (!rtc.isrunning()) { Serial.println("RTC is NOT running, let's set the time!"); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } // 引脚模式配置 pinMode(chipSelect, OUTPUT); pinMode(pinLed, OUTPUT); pinMode(pinMotion, INPUT); // 初始化SD卡 if (!SD.begin(chipSelect)) { Serial.println("Error : Push the reset button"); for (;;); } // 写入CSV表头 File file = SD.open(filename, FILE_WRITE); if (file) { if (file.size() == 0) { file.println("类型,数值,时间,日期"); } file.close(); } } void loop() { DateTime now = rtc.now(); // 1. 采集并记录亮度 readAndLogLight(now); // 2. 采集并记录温度 readAndLogTemp(now); // 3. 检测并记录运动 checkMotion(now); // 每秒采样一次 delay(1000); } // 读取亮度并写入SD卡 void readAndLogLight(DateTime time) { int lightValue = analogRead(pinLight); String logStr = String("亮度,") + String(lightValue) + "," + time.timestamp(DateTime::TIMESTAMP_TIME) + "," + time.timestamp(DateTime::TIMESTAMP_DATE); Serial.println(logStr); File file = SD.open(filename, FILE_WRITE); if (file) { file.println(logStr); file.close(); } } // 读取温度并写入SD卡 void readAndLogTemp(DateTime time) { int sensorInput = analogRead(pinTemp); double temp = (double)sensorInput / 1024 * 5; temp = (temp - 0.5) * 100; String logStr = String("温度,") + String(temp, 2) + "," + time.timestamp(DateTime::TIMESTAMP_TIME) + "," + time.timestamp(DateTime::TIMESTAMP_DATE); Serial.println(logStr); File file = SD.open(filename, FILE_WRITE); if (file) { file.println(logStr); file.close(); } } // 检测运动并写入SD卡 void checkMotion(DateTime time) { int val = digitalRead(pinMotion); if (val == HIGH) { digitalWrite(pinLed, HIGH); if (motionState == LOW) { String logStr = String("运动,检测到,") + time.timestamp(DateTime::TIMESTAMP_TIME) + "," + time.timestamp(DateTime::TIMESTAMP_DATE); Serial.println(logStr); File file = SD.open(filename, FILE_WRITE); if (file) { file.println(logStr); file.close(); } motionState = HIGH; } } else { digitalWrite(pinLed, LOW); if (motionState == HIGH){ String logStr = String("运动,停止,") + time.timestamp(DateTime::TIMESTAMP_TIME) + "," + time.timestamp(DateTime::TIMESTAMP_DATE); Serial.println(logStr); motionState = LOW; } } }
硬件接线注意事项
- 光敏传感器输出接A0
- 温度传感器输出接A2
- 人体运动传感器输出接A1
- 所有传感器共地共3.3V/5V(根据传感器参数选择供电电压)
内容的提问来源于stack exchange,提问作者Nick Sharman
相关产品推荐
相关产品推荐

