|
Post by 邱老师 on Jan 7, 2021 14:58:58 GMT
判断人是否离开
int sensor_pin = 2;
int c = 0;
void setup() {
Serial.begin(9600);
pinMode(sensor_pin,INPUT);
Serial.flush();
}
void loop() {
int val = digitalRead(sensor_pin);
if (val == 1){
delay(50);
if (val == 1){
c = 1; //反转c值
Serial.println("have people");
delay(5000);
delay(5000); //延时10s
}
}
else if (c==1 && val==0){
c = 0;
Serial.println("people left"); //当人离开的时候打印
}
}
|
|
|
Post by 邱老师 on Jan 7, 2021 15:00:07 GMT
当没有监测到有人的时候,串口打印输出“NONE”,当有人靠近的时候,串口打印“have people”,当人离开的时候,串口打印“people left”。
int sensor_pin = 2;
int c = 0;
void setup() {
Serial.begin(9600);
pinMode(sensor_pin,INPUT);
Serial.flush();
}
void loop() {
int val = digitalRead(sensor_pin);
if (val == 1){
delay(50);
if (val == 1){
c = 1; //反转c值
Serial.println("have people"); //延时10s
}
}
else if (c==1 && val==0){
c = 0;
Serial.println("people left"); //当人离开的时候打印
}
else {
Serial.println("NONE");
}
delay(5000);
delay(5000);
}
|
|