![](/img/trans.png)
[英]How to control a servo motor using servo library in C++ on Arduino?
[英]How to turn motor on/ff automatically using Arduino
我的項目涉及發送 SMS 以手動打開或關閉系統、 Motor on
、 Motor off
但我想要一個選項讓系統自行運行,而無需自己手動打開或關閉電機。 因此Auto on
。 此自動可以通過Auto off
。 我不知道如何使系統在Auto on
時運行以關閉電機,方法是將我的土壤濕度探頭從干土壤切換到濕土壤,反之亦然,從濕切換到干,然后打開電機。 如果SensorValue
的條件是 >= Maxdryness
我希望它停止並僅在值 < Maxdryness
時打開。
{
auto_flag = 1;
message1 = "System is now automatic";
send_message(message1); // Send a sms back to confirm that the motor is turned auto
if (readSoil() >= Maxdryness)
{
Serial.println("Soil is good.\r");
digitalWrite(wetSoil, HIGH);
digitalWrite (drySoil, LOW);
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
delay(1000);
}
else
{
Serial.println("Low Soil Moisture detected.\r");
digitalWrite(wetSoil, LOW);
digitalWrite (drySoil, HIGH);
digitalWrite(motorPin1,HIGH);
digitalWrite(motorPin2,LOW);
}
delay(1000);
Serial.print("Soil Moisture = ");
Serial.println(readSoil());
delay(1000);//take a reading every second
auto_flag = 0;
}
如果SensorValue < Maxdryness
或SensorValue >= Maxdryness
的條件在任何時候都為假,我正在嘗試更新SensorValue
變量。 我希望能夠在干土和濕土之間切換我的濕度傳感器,並讓它根據需要自動打開或關閉我的電機。
看起來您的代碼中缺少滯后,因此您到處都添加了長時間的延遲。 最簡單的方法之一是添加全局 boolean 標志 - “電機開啟”並相應地保持它(打開時設置為true
,反之亦然)。 然后您根據該標志采取行動 - 如果 motor on 您僅在SensorValue >= Maxdryness
時檢查,而當它關閉時,您僅在SensorValue < Mindryness
時檢查。 您需要使Mindryness
小於Maxdryness
- 這會產生滯后。 這個差距有多大,這取決於你的條件,太大會溢出,太小會經常抖動電機。 這樣您就不需要長時間的延遲,系統仍然可以順利運行。
為了安全起見,您可能需要測量電機開啟的時間並在一段時間內未達到富油水平時關閉系統,以避免出現問題時溢出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.