本帖最後由 超新手 於 2019-1-14 17:27 編輯 因為你沒按規則合併 你把 DHT 的 loop 程式, 合併到 String ConvertLng() 裡面去了 應該放到 loop 的後半段... stringplace = 0; pos = 0; 這邊才是 loop 迴圈內 } |
超新手 發表於 2019-1-16 11:18 了解 我在研究看看 非常感謝你的幫忙 |
了解我在研究看看 |
所有後來新增的程式都是除錯用 根本沒用處. 本來就要刪 我看不出來為什麼加了那些除錯程式後 就會變正常 |
這樣算沒東西吧? 你之前所謂GPS可以, 有真的顯示 GPS 座標嗎? |
再多印一些資料出來看看 if (GPSModule.find("$GPRMC,")) { Serial.print("#"); String tempMsg = GPSModule.readStringUntil('\n'); Serial.print("*"); Serial.println(tempMsg.length()); for (int i = 0; i < tempMsg.length(); i++) { if (tempMsg.substring(i, i + 1) == ",") { nmea[pos] = tempMsg.substring(stringplace, i); stringplace = i + 1; serial.println(stringplace); serial.println(i); serial.println(pos); pos++; } if (i == tempMsg.length() - 1) { nmea[pos] = tempMsg.substring(stringplace, i); serial.println(stringplace); serial.println(i); serial.println(pos); } } |
本帖最後由 超新手 於 2019-1-15 14:34 編輯 1)改回去GPS 程式, 結果是正常的嗎? 2) 加兩行除錯一下 if (GPSModule.find("$GPRMC,")) { Serial.print("#") String tempMsg = GPSModule.readStringUntil('\n'); Serial.print("*") |
本帖最後由 超新手 於 2019-1-15 13:22 編輯 顯示一次後, 就什麼都沒有顥示嗎? 應該不是"無法從DHT傳感器讀取"吧? |
本帖最後由 James 於 2019-1-15 11:53 編輯 超新手 發表於 2019-1-15 11:38 可以 但是只會顯示一次 delay拿掉結果一樣 |
那可以顯示 濕度/溫度/酒精值嗎? 可能是 DELAY 太久 先拿掉 delay(2000); 和 delay(3000); 看看 |
改完後變成不會顯示GPS了 麻煩再幫忙解惑 感謝~~~~ #include <DHT.h> #include "DHT.h" #include<SoftwareSerial.h> //匯入程式庫 SoftwareSerial GPSModule(10, 11); // TX, RX #define dhtPin 8 #define dhtType DHT11 const int AOUTpin=0;//the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino const int DOUTpin=9;//the DOUT pin of the alcohol sensor goes into digital pin D8 of the arduino const int ledPin=13;//the anode of the LED connects to digital pin D13 of the arduino int limit; int value; int updates; int failedUpdates; int pos; int stringplace = 0; String timeUp; String nmea[15]; String labels[12] {"Time: ", "Status: ", "Latitude: ", "Hemisphere: ", "Longitude: ", "Hemisphere: ", "Speed: ", "Track Angle: ", "Date: "}; DHT dht(dhtPin, dhtType); void setup() { Serial.begin(9600); dht.begin(); pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino pinMode(ledPin, OUTPUT);//sets the pin as an output of the Serial.begin(57600); GPSModule.begin(9600); Serial.begin(115200);//sets the baud rate Serial .println(); Serial .println("感測器啟動"); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); float f = dht.readTemperature(true); if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("無法從DHT傳感器讀取! "); return; } Serial.println(); Serial.print("濕度: "); Serial.print(h); Serial.println("%\t"); Serial.print("攝氏溫度: "); Serial.print(t); Serial.println("*C\t"); Serial.print("華氏溫度: "); Serial.print(f); Serial.println("*F\n"); delay(3000); value= analogRead(AOUTpin);//reads the analaog value from the alcohol sensor's AOUT pin limit= digitalRead(DOUTpin);//reads digital value from the alcohol sensor's DOUT pin Serial.print("酒精值 "); Serial.println(value);//prints the alcohol value Serial.print("Limit: "); Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath) delay(2000); if (limit == HIGH){ digitalWrite(ledPin, HIGH);//if limit has been reached, LED turns on as status indicator } else{ digitalWrite(ledPin, LOW);//if threshold not reached, LED remains off } Serial.flush(); GPSModule.flush(); while (GPSModule.available() > 0) { GPSModule.read(); } if (GPSModule.find("$GPRMC,")) { String tempMsg = GPSModule.readStringUntil('\n'); for (int i = 0; i < tempMsg.length(); i++) { if (tempMsg.substring(i, i + 1) == ",") { nmea[pos] = tempMsg.substring(stringplace, i); stringplace = i + 1; pos++; } if (i == tempMsg.length() - 1) { nmea[pos] = tempMsg.substring(stringplace, i); } } updates++; nmea[2] = ConvertLat(); nmea[4] = ConvertLng(); for (int i = 0; i < 9; i++) { Serial.print(labels[i]); Serial.print(nmea[i]); Serial.println(""); } } else { failedUpdates++; } stringplace = 0; pos = 0; } String ConvertLat() { String posneg = ""; if (nmea[3] == "S") { posneg = "-"; } String latfirst; float latsecond; for (int i = 0; i < nmea[2].length(); i++) { if (nmea[2].substring(i, i + 1) == ".") { latfirst = nmea[2].substring(0, i - 2); latsecond = nmea[2].substring(i - 2).toFloat(); } } latsecond = latsecond / 60; String CalcLat = ""; char charVal[9]; dtostrf(latsecond, 4, 6, charVal); for (int i = 0; i < sizeof(charVal); i++) { CalcLat += charVal[i]; } latfirst += CalcLat.substring(1); latfirst = posneg += latfirst; return latfirst; } String ConvertLng() { String posneg = ""; if (nmea[5] == "W") { posneg = "-"; } String lngfirst; float lngsecond; for (int i = 0; i < nmea[4].length(); i++) { if (nmea[4].substring(i, i + 1) == ".") { lngfirst = nmea[4].substring(0, i - 2); //Serial.println(lngfirst); lngsecond = nmea[4].substring(i - 2).toFloat(); //Serial.println(lngsecond); } } lngsecond = lngsecond / 60; String CalcLng = ""; char charVal[9]; dtostrf(lngsecond, 4, 6, charVal); for (int i = 0; i < sizeof(charVal); i++) { CalcLng += charVal[i]; } lngfirst += CalcLng.substring(1); lngfirst = posneg += lngfirst; return lngfirst; } |
小黑屋|手機版|Archiver|機器人論壇 from 2005.07
GMT+8, 2024-11-21 22:47 , Processed in 0.168088 second(s), 11 queries , Apc On.
Powered by Discuz! X3.2
© 2001-2013 Comsenz Inc.