|
发表于 2021-3-14 20:29:22
|
显示全部楼层
void read_dual() {
lox1.rangingTest(&measure1, false); // pass in 'true' to get debug data printout! 放到打印反而工作的更久
lox2.rangingTest(&measure2, false); // pass in 'true' to get debug data printout!
// 读取距离判断有无人
if (measure1.RangeStatus != 4) { // if not out of range
int distance_A = measure1.RangeMilliMeter;
// Serial.print("A_status:");Serial.print(measure1.RangeStatus);
// Serial.print(" ");Serial.print("A:");Serial.println(distance_A);
if (distance_A <= Trigger_Distance && distance_A != 0) {
motion_a = 1;
}
}
else {
motion_a = 0;
}
if (measure2.RangeStatus != 4) { // if not out of range
int distance_B = measure2.RangeMilliMeter;
// Serial.print("B_status:");Serial.print(measure2.RangeStatus);
// Serial.print(" ");Serial.print("B:");Serial.println(distance_B);
if (distance_B <= Trigger_Distance && distance_B != 0) {
motion_b = 1;
}
}
else {
motion_b = 0;
}
//判断触发方向 // 记录触发并根据序列判断人数
if (motion_a == 0) {
flag_a = 1;
}
if (motion_a == 1 && flag_a == 1) {//0变1
up_a = true;flag_a = 0;
Serial.print(millis());Serial.println(":a is up");
maichong_buff += "A1";jilu += "A1";
// Serial.print(millis());Serial.println(jilu); //调试用
}
if (motion_b == 0) {
flag_b = 1;
}
if (motion_b == 1 && flag_b == 1) {
up_b = true;flag_b = 0;
Serial.print(millis()); Serial.println(":b is up");
maichong_buff += "B1";jilu += "B1";
// Serial.print(millis());Serial.println(jilu);//调试用
}
if (motion_a == 1) {
flag_c = 1;
}
if (motion_a == 0 && flag_c == 1) {
down_a = true;flag_c = 0;
Serial.print(millis());Serial.println(":a is down");
maichong_buff += "A0";jilu += "A0";
// Serial.print(millis());Serial.println(jilu);//调试用
String a = jilu.substring(jilu.length() - 4);
String b = jilu.substring(0, 2);
if (a == "B0A0" && b == "B1") { //人出来了
if (renshu > 0) {
renshu--;
}
action(); //显示,推送,beep等
jilu = ""; maichong_buff = ""; //人出来后需清空序列,以供下次判断
}
// if((a=="B0A0"||a=="A1A0")&&b=="A1") {jilu="";}
if (a == "B0A0" && b == "A1") { //人又退回去了没进去,可能在传感器下停留或来回移动一段时间
jilu = ""; maichong_buff = ""; //只清空记录,不清空buff,。
}
if (a == "A1A0" && jilu.length() == 4) { // 人又退回去了没进去,或者是干扰,需单独判断
jilu = "";
}
}
if (motion_b == 1) {
flag_d = 1;
}
if (motion_b == 0 && flag_d == 1) {//低电平,需要对是否进入情况进行判断
down_b = true;flag_d = 0;
Serial.print(millis());Serial.println(":b is down");
maichong_buff += "B0";jilu += "B0";
// Serial.print(millis());Serial.println(jilu);//调试用
String a = jilu.substring(jilu.length() - 4);
String b = jilu.substring(0, 2);
if (a == "A0B0" && b == "A1") { //人进去了
renshu++;
action();
jilu = ""; maichong_buff = "";
}
// if((a=="A0B0"||a=="B1B0")&&b=="B1") {jilu="";}
if (a == "A0B0" && b == "B1") { //人退回去了没出来,可能在传感器下停留或来回移动一段时间
jilu = ""; maichong_buff = "";
}
if (a == "B1B0" && jilu.length() == 4) { //人没出来,或者是干扰
jilu = "";
} //其他情况则继续记录
}
down_a = false; //一次循环完后把状态值恢复,以免影响后边
down_b = false;
up_a = false;
up_b = false;
}
核心代码给你,其他的你自己补把 |
|