非常简单的问题,但我不确定如何将for a for a for a in If i if ime Imption In。上下文:我有一个加湿器,我试图根据房间的湿度自动化自动化。我正在使用Ardiuno,DHT11湿度传感器和伺服器。加湿器旋钮具有三个设置(高低),因此伺服器具有三个位置。我的代码运行,因此伺服器根据湿度级别适当地转动。问题是它很容易波动。为了纠正我想整合一个循环,以便在说60秒的湿度大于55的伺服移动之后。我试图添加一个循环,但似乎无法正常工作。
这只是我所知道的小编程的解决方案。如果有更好的解决方案,甚至是同样可行的替代方案,我很想知道。我目前正在学习机械工程,但我发现这确实需要电子和代码背景。我正在尝试通过一系列项目独立学习,因此我很想学习。希望这有助于解释为什么我要问一个简单的问题。
#include <dht.h>
#include <Servo.h>
Servo myservo;//create servo object to control a servo
dht DHT;
#define DHT11_PIN 7 // pin for humidity sensor ( also measure temp)
void setup() {
myservo.attach(9);//attachs the servo on pin 9 to servo object
myservo.write(0);//statting off position at 0 degrees
delay(1000);//wait for a second
Serial.begin(9600);
}
void loop() {
int chk = DHT.read11(DHT11_PIN); // the follow is just so that I can see the readings come out properly
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
delay(500);
if (DHT.humidity > 55) // here is where my code really begins
{
for (int i=0; i>60; i++); // my goal is to execute the follow code after the statement above has been true for 60 one second iterations
{
myservo.write(0);//goes to off position
delay(1000);//wait for a second
}
} else if (DHT.humidity > 40 ) {
for (int i=0; i>60; i++); // same thing here
myservo.write(90);//goes to low position
delay(1000);//wait for a second
}
else
{
for (int i=0; i>60; i++);
myservo.write(180);//goes to high position
delay(1000);
}
} // end of void loop()
仅解决您的问题,以下行是不正确的:
for (int i=0; i>60; i++);
两件事:1)for循环中的第二个语句描述了其执行的条件。它的写作方式,只有在i> 60(而不是根据评论您想要的)才能执行。2)供应语句之后的分号使下一个块不相关。
将该行更正以下:
for (int i=0; i<60; i++)
有关更多信息,请参见以下信息:https://www.tutorialspoint.com/cprogramming/c_for_loop.htm
检查您的编译器警告和/或设定更高的警告级别可能会很有帮助,以尽早捕获这些类型的事物(当然,这是依赖编译器的)。
我想您尝试在某个时期内尝试潮湿的潮湿级别。
首先,我将转换函数定义为将潮湿级别映射到状态
#define HUMID_OFF 1
#define HUMID_LOW 2
#define HUMID_HIGH 3
byte state_conv (float humid_level){
if (humid_level > 55) return HUMID_OFF ;
else if (humid_level > 40 ) return HUMID_LOW ;
else return HUMID_HIGH ;
}
第二,我将检查状态的更改,并使用Millis()在当前状态稳定时计算时间。如果计数时间长于阈值,则更改实际状态。
/*Global variable*/
byte actual_state;
byte flag_state;
void setup (){
// Do things that necessary
float humid = dht.readHumidity();
/*Initialize value*/
actual_state = state_conv(humid);
flag_state= state_conv(humid);
}
void loop(){
static unsigned long timer = millis();
float humid = dht.readHumidity();
byte crr_state = state_conv(humid);
if (crr_state != actual_state ){// if state is changing
if (flag_state != crr_state){
/if crr_state change form last iteration then reset timer
flag_state = crr_state;/
timer = millis();
}
else if (millis() - timer > 10000){
//if crr_state not change for 10000 ms (10 second)
actual_state = crr_state; // update actual state to crr_state
}
}
// After this use actual_state to control servo
if (actual_state == HUMID_OFF ){
myservo.write(0);//goes to off position
}
else if (actual_state == HUMID_LOW ){
myservo.write(90);//goes to low position
}
else if (actual_state == HUMID_HIGH ){
myservo.write(180);//goes to high position
}
}
DHT.humidity
返回 float
,因此,如果要比较,请首先将其存储在int
中,然后比较。