上电
按键开始
按键结束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #include "DHT.h" #define DHTPIN 4 // Digital pin connected to the DHT sensor #define DHTTYPE DHT11 // DHT 11 #define LightPIN A0 #define ViocePIN A1 #define BtnPIN 3 DHT dht(DHTPIN, DHTTYPE); / / 顺序 0 温度 0 - 100 1 湿度 0 - 100 2 光照 0 - 1023 3 声音 0 - 1023 String sensorName[ 4 ] = { "TemperatureValue" , "HumidityValue" , "LightValue" , "VioceValue" }; int thresh[ 4 ] = { 30 , 50 , 300 , 80 }; / / 阈值 int btnState = 0 ; int SensorState[ 4 ] = { 0 , 0 , 0 , 0 };; unsigned int timeALL[ 4 ] = { 0 , 0 , 0 , 0 }; unsigned int timeALLOld[ 4 ] = { 0 , 0 , 0 , 0 }; unsigned int timeCurStart[ 4 ] = { 0 , 0 , 0 , 0 }; unsigned int timeCurEnd[ 4 ] = { 0 , 0 , 0 , 0 }; bool timeCostState = 0 ; void setup() { Serial.begin( 9600 ); Serial.println(F( "go go go!" )); dht.begin(); / / time_cur = millis(); / / time_old = millis(); } void loop() { float TemperatureValue = dht.readTemperature(); float HumidityValue = dht.readHumidity(); int LightValue = 1024 - analogRead(LightPIN); int VioceValue = analogRead(ViocePIN); int BtnValue = digitalRead(BtnPIN); if (isnan(HumidityValue) || isnan(TemperatureValue) ) { Serial.println(F( "Failed to read from DHT sensor!" )); HumidityValue = 0 ; TemperatureValue = 0 ; return ; } int curTimeValue[ 4 ] = { int (TemperatureValue), int (HumidityValue),LightValue,VioceValue}; if (BtnValue = = 1 &&btnState = = 0 ){ Serial.println( "按键初次按下,准备开始" ); btnState = 1 ; } else if (BtnValue = = 1 &&btnState = = 1 ) { Serial.println( "按键按下中,等待释放按键开始...." ); } else if (BtnValue = = 0 &&btnState = = 1 ) { Serial.println( "按键初次释放,计时开始" ); btnState = 2 ; timeCostState = 1 ; / / 开始计时 } else if (BtnValue = = 0 &&btnState = = 2 ){ Serial.println( "计时中..." ); } else if (BtnValue = = 1 &&btnState = = 2 ){ Serial.println( "按键再次按下,准备结束" ); btnState = 3 ; } else if (BtnValue = = 1 &&btnState = = 3 ){ Serial.println( "按键再次按下中,等待释放按键结束...." ); } else if (BtnValue = = 0 &&btnState = = 3 ){ Serial.println( "按键再次释放,结束计时" ); btnState = 0 ; timeCostState = 0 ; / / 结束标志位 for ( int i = 0 ;i< = 3 ;i + + ){ timeALLOld[i] = timeALL[i]; SensorState[i] = 0 ; timeALL[i] = 0 ; timeCurStart[i] = 0 ; timeCurEnd[i] = 0 ; } } else if (BtnValue = = 0 &&btnState = = 0 ){ Serial.println( "等待按键开始" ); } unsigned time_cur = millis(); / / time_cost = int ((time_cur - time_old) / 1000 ); if (timeCostState = = 1 ){ for ( int i = 0 ;i< = 3 ;i + + ) { if (curTimeValue[i]>thresh[i]){ if (SensorState[i] = = 0 ){ timeCurStart[i] = millis(); timeCurEnd[i] = millis(); SensorState[i] = 1 ; String msgs = String(sensorName[i]) + " ,begin cout! (" + String(curTimeValue[i]) + ">" + String(thresh[i]) + ")" + " CurAlltime: " + String(timeALL[i]); Serial.println(msgs); } else if (SensorState[i] = = 1 ){ timeCurEnd[i] = millis(); unsigned int timeInterval = int ((timeCurEnd[i] - timeCurStart[i]) / 1000 ); timeALL[i] = timeALL[i] + timeInterval; timeCurStart[i] = millis(); String msgs = String(sensorName[i]) + " ,couting... (" + String(curTimeValue[i]) + ">" + String(thresh[i]) + ")" + " CurAlltime: " + String(timeALL[i]); Serial.println(msgs); } } else { if (SensorState[i] = = 0 ){ timeCurStart[i] = millis(); timeCurEnd[i] = millis(); String msgs = String(sensorName[i]) + " not cout (" + String(curTimeValue[i]) + "<" + String(thresh[i]) + ")" + " CurAlltime: " + String(timeALL[i]); Serial.println(msgs); } else if (SensorState[i] = = 1 ){ timeCurEnd[i] = millis(); unsigned int timeInterval = int ((timeCurEnd[i] - timeCurStart[i]) / 1000 ); timeALL[i] = timeALL[i] + timeInterval; timeCurStart[i] = millis(); SensorState[i] = 0 ; String msgs = String(sensorName[i]) + " ,cout end! (" + String(curTimeValue[i]) + "<" + String(thresh[i]) + ")" + " CurAlltime: " + String(timeALL[i]); Serial.println(msgs); } } } String ShowMsg = String(" ")+" = = = = = = = = = = TOTAL TIME = = = = = = = = \n " + "Light-" + String(timeALL[ 0 ]) + " Humidity-" + String(timeALL[ 1 ]) + " Temperature-" + String(timeALL[ 2 ]) + " Vioce-" + String(timeALL[ 3 ]); Serial.println(ShowMsg); } / / if (timeCostState = = 1 ){ else { / / 没有开始计时 String ShowMsg = String("") + "LightValue:" + String(LightValue) + " " + "Humidity: " + String(HumidityValue) + "% " + "Temperature: " + String(TemperatureValue) + "C。 " + "VioceValue:" + String(VioceValue) + " " + "btnState:" + String(BtnValue) + " " ; Serial.println(ShowMsg); ShowMsg = String(" ")+" = = = = = = = = = = TOTAL TIME = = = = = = = = \n " + "Light-" + String(timeALLOld[ 0 ]) + " Humidity-" + String(timeALLOld[ 1 ]) + " Temperature-" + String(timeALLOld[ 2 ]) + " Vioce-" + String(timeALLOld[ 3 ]); Serial.println(ShowMsg); } delay( 1 * 1000 ); / / 等待一会 延迟 2 秒 } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端