![](/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.