我正在开展一个小型项目,该项目使用多个超声波传感器来检测运动物体并对其进行计数(例如汽车)。我使用switch case但程序并不是所有的情况,因此它没有正常工作,并且计数器也没有增加减少。我测试了每个传感器并意识到它们已经工作但我不知道为什么我不能得到这个代码的结果,如果你帮我提前赞赏...下面是我的代码
const int sensorNum = 2;
uint8_t reading_0;
uint8_t reading_1 ;
int sensing[sensorNum][2];
unsigned long duration = 0; //period between transmit time and receive time in microSeconds
float distance = 0.00; //distance in centimeter
const float fixedDistance = 30.00;
const float vilocity = 0.034; //Vilocity of ultrasonic waves = 340 m/Sec = 0.034 cm/uSec
//const float cmDivisor = 29.4118;
float reading = 0.00;
int carCounter =200;
int PCarCounter=0;
int capacity = 200;
//int activate = LOW;
uint8_t current_ENTERstate = 0;
const uint8_t RESET = 0,CAR_START2EN=1,CAR_ENTERING=2,CAR_EN1=3,CAR_ENTERED=4;
void setup()
{
Serial.begin(9600);
for(int i = 0; i<sensorNum; i++){
sensing[i][1]=LOW;
sensing[i][2]=LOW;
pinMode(i+2,OUTPUT);
digitalWrite(i+2,LOW);
pinMode(i+8,INPUT);
digitalWrite(i+8,LOW);
current_ENTERstate = 0;
}
}
//==============================================================
void loop()
{
for(int l=0; l<2; l++){
reading_0 = calcDistance(2,8);
reading_1 = calcDistance(3,9);
if(reading_0 > 30.0 && reading_0 < 200.0) {
sensing[0][l] = HIGH;
}else if(reading_0 < 30.0 || reading_0 > 200.0) {
sensing[0][l] = LOW;
}
if(reading_1 > 30.0 && reading_1 < 200.0) {
sensing[1][l] = HIGH;
}else if(reading_1 < 30.0 || reading_1 > 200.0){
sensing[1][l] = LOW;
}
}
switch(current_ENTERstate){
case RESET:
if(sensing[0][0]||sensing[0][1]){
current_ENTERstate = CAR_START2EN;
break;
}else{
current_ENTERstate = RESET;
break;
}
break;
case CAR_START2EN:
if( sensing[0][0]||sensing[0][1] && sensing[1][0]||sensing[1][1] ){
current_ENTERstate = CAR_ENTERING;
break;
}else{
current_ENTERstate = RESET;
break;
}
break ;
case CAR_ENTERING:
if( sensing[1][0]||sensing[1][1] ){
current_ENTERstate = CAR_EN1;
break;
}else{
current_ENTERstate = CAR_START2EN;
break;
}
case CAR_EN1:
if( sensing[0][0]==LOW && sensing[0][1]==LOW && sensing[1][0]== LOW && sensing[1][1]==LOW ){
current_ENTERstate = CAR_ENTERED;
break;
}else{
current_ENTERstate = CAR_ENTERING;
break;
}
case CAR_ENTERED:
carCounter++;
break;
default: RESET;
} // switch end
}
//====================================================================================
float calcDistance(int trigger, int echoPin){
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
duration = pulseIn(echoPin,HIGH,40000);
distance = vilocity * duration / 2.0;
//Serial.print(distance);
return distance;
}
答案 0 :(得分:2)
以下是可疑的(感谢@WeatherVane发现它):
if( sensing[0][0]||sensing[0][1] && sensing[1][0]||sensing[1][1] )
因为&&
在 ||
之前被评估了。条件表达式的评估就像它被写了一样:
a = sensing[0][1] && sensing[1][0] ;
if( sensing[0][0] || a || sensing[1][1] )
这似乎不太可能是意图。它应该写成:
if( (sensing[0][0] || sensing[0][1]) &&
(sensing[1][0] || sensing[1][1]) )
(为了清晰起见,添加了换行符。)
关于感应,您应该在读取噪声传感器时添加一些滞后现象。为避免重复大量高维护代码,最好定义一个函数
void distanceHighLow( float reading, int& sensing ) ;
{
static const float HYSTERESIS = 2.0f ;
static const float MIN_DISTANCE = 30.0f ;
static const float MAX_DISTANCE = 200.0f ;
if( reading > MIN_DISTANCE &&
reading < MAX_DISTANCE )
{
sensing = HIGH;
}
else if( reading < (MIN_DISTANCE - HYSTERESIS) ||
reading > MAX_DISTANCE + HYSTERESIS)
{
sensing = LOW;
}
}
然后你调用这个函数:
distanceHighLow( reading_0, sensing[0][l] ) ;
distanceHighLow( reading_1, sensing[1][l] ) ;