如何使用Arduino自动打开/关闭电机



我的项目包括发送短信手动打开或关闭系统,Motor onMotor off,但我想要一个选项,让系统自己运行,而不必自己手动打开或关掉电机。因此CCD_ 3。此自动功能可由Auto off关闭。我不知道如何在Auto on上运行系统,通过将土壤湿度探头从干土切换到湿土,反之亦然,从湿土切换到干土并打开电机来关闭电机。如果SensorValue的条件是>=CCD_ 7我希望它停止并且仅在值<CCD_ 8。

{ 
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 < MaxdrynessSensorValue >= Maxdryness的条件在任何时候都为假,我将尝试更新SensorValue变量。我希望能够在干燥和潮湿的土壤之间切换湿度传感器,并根据需要自动打开或关闭电机。

看起来您的代码中缺少滞后,因此到处都添加了长延迟。最简单的方法之一是添加全局布尔标志";电动机接通";并相应地对其进行维护(打开时将其设置为true,反之亦然(。然后你根据这个标志采取行动——如果电机打开,你只在SensorValue >= Maxdryness时检查,而当它关闭时,你只检查SensorValue < Mindryness。您需要使Mindryness小于Maxdryness,这将产生滞后。这个间隙有多大,取决于你的条件,太大就会溢出,太小就会经常抖动电机。这样您就不需要长时间的延迟,系统仍然可以顺利工作。

为了安全起见,您可能需要测量电机打开的时间,如果在很长一段时间内没有达到浓液位,则关闭系统,以避免出现问题时出现溢流。

最新更新