如何让我的SensorValue值自动更新?我正在尝试使SensorValue变量在任何时候SensorValue < Maxdryness或SensorValue >= Maxdryness的条件为false时自动更新。我希望我的湿度传感器能够在干燥和湿润的土壤之间切换,并根据需要自动打开或关闭电机。
if(incomingData.indexOf("Auto on")>=0) // if received command is to turn system automatic
{
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;
}我的项目涉及发送短信手动打开或关闭系统,Motor on,Motor off,但我想要一个选项,使系统自动运行,而不必手动打开或关闭自己的电机。因此就有了Auto on。此自动功能可由Auto off关闭。我不知道如何让系统在Auto on模式下运行,通过将土壤湿度探头从干土壤切换到湿土壤来关闭电机,反之亦然。如果SensorValue的条件是>= Maxdryness,我希望它停止并仅在值为<>= Maxdryness时才打开。
发布于 2020-12-09 07:33:51
如果SensorValue是单个引脚上的传感器,请在每个循环中或定期读取该引脚!
从你的代码看,它更有可能是一个模拟引脚。文档中(我很久没有读过了)似乎也不需要在setup()块中声明模拟可读的管脚(而对于数字管脚是这样做的),所以获取结果应该和将调用analogRead()的返回值赋给SensorValue一样简单
void loop() {
...
SensorValue = analogRead(PIN_NUMBER);
...
}您可能想要做一些消息,以线性化对数输出强制值进入某个范围(0-100而不是0-1023),等等,但这将取决于未显示的代码和传感器引脚表示的值。
如果传感器是更高级的东西,比如使用SPI bus,您需要做一些额外的工作,但设置将是相似的。无论是谁生产的组件,都应该提供一些文档。如果您需要其他参考,Arduino还提供了一些通用Barometric Pressure Sensor的示例。
https://stackoverflow.com/questions/65208419
复制相似问题