我在代码逻辑中遇到问题,我没有找到任何解决方案。
我怀疑是我在Arduino上使用两个红外传感器接口,所以当汽车通过第一传感器和第二传感器时,我发送汽车通过的数据。这很好但是每当car1通过第一个传感器和另一辆车时,比如car2在第二个传感器中,那么旗也变成1,这是真的,但我不想要那个。
如何对此进行编码以使汽车通过两个传感器?如果汽车1在传感器1中,而汽车2在传感器2中,则标志不应为1。
请找到以下代码:
#include<avr/wdt.h>
#define DISTANCE 100
const int trigPin1 = 7;
const int echoPin1 = 6;
const int trigPin2 = 5;
const int echoPin2 = 4;
int MOVE_FLAG = 0;
void setup() {
// initialize serial communication:
Serial.begin(9600);
MOVE_FLAG = 0;
wdt_enable(WDTO_8S);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration1, duration2, inches1, inches2, cm1, cm2;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin1, OUTPUT);
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);
inches1 = microsecondsToInches(duration1);
cm1 = microsecondsToCentimeters(duration1);
delay(10);
pinMode(trigPin2, OUTPUT);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);
// convert the time into a distance
inches2 = microsecondsToInches(duration2);
cm2 = microsecondsToCentimeters(duration2);
if(cm1 <= DISTANCE && cm2 <= DISTANCE && MOVE_FLAG == 0)
{
Serial.println("3");
MOVE_FLAG = 1;
}
if (cm1 > DISTANCE && cm2 > DISTANCE && MOVE_FLAG == 1)
{
MOVE_FLAG = 0;
delay(500);
}
delay(50);
wdt_reset();
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
答案 0 :(得分:0)
两个运动传感器不足以完成您所描述的内容。没有视频或类似的东西,您无法知道Car 1是触发传感器还是Car 2触发了传感器。
我建议你重新思考解决问题的方法。您可能希望查看人们在互联网上创建并发布的其他停车场项目。谷歌搜索&#34; Arduino停车场&#34;或类似的东西。