我想打印" Entry"在串行监视器中每次传感器感应到物体,但我的输出是"进入" "条目"等等,直到感觉到下一个条目并且它继续以这种格式。我想打印"条目"只有当感测到物体的条目时才会这样。
`
#define trig1 11
#define echo1 10
#define trig2 9
#define echo2 6
#define ledpin 13
int sensor_1 = 0;
int sensor_2 = 0;
int count = 0;
long duration1, distance1;
long duration2, distance2;
//unsigned long prevmillis=0;
//const long interval=1000;
void setup() {
// put your setup code here, to run once:
pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(trig2, OUTPUT);
pinMode(echo2, INPUT);
pinMode(ledpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
//unsigned long prevmillis=millis();
sensor1(); ///set sensor_1=1
sensor2();///set sensor_2=1
if (sensor_1 == 1)
{ if (sensor_2 == 1) {
Serial.println("Entry");
}
}
}
int sensor1(void)
{
digitalWrite(trig1, LOW);
delayMicroseconds(2);
digitalWrite(trig1, HIGH);
delayMicroseconds(10);
digitalWrite(trig1, LOW);
duration1 = pulseIn(echo1, HIGH);
analogWrite(ledpin, 0);
distance1 = duration1 * .034 / 2;
if (distance1 < 10) {
delay(100);
sensor_1 = 1;
//delay(400);
//count++;
//unsigned long currentmillis=millis();
//Serial.println(currentmillis-prevmillis);
//Serial.println("Sensor1 Sensed Object");
//Serial.println(count);
analogWrite(ledpin, 255);
delay(200);
}
//return sensor_1;
}
int sensor2(void)
{
digitalWrite(trig2, LOW);
delayMicroseconds(2);
digitalWrite(trig2, HIGH);
delayMicroseconds(10);
digitalWrite(trig2, LOW);
duration2 = pulseIn(echo2, HIGH);
analogWrite(ledpin, 0);
distance2 = duration2 * .034 / 2;
if (distance2 < 15) {
delay(100);
sensor_2 = 1;
//delay(400);
//count++;
//unsigned long currentmillis=millis();
//Serial.println(currentmillis-prevmillis);
//Serial.println("Sensor2 Sensed Object");
//Serial.println(count);
analogWrite(ledpin, 255);
delay(100);
}
//return sensor_2;
}
`
答案 0 :(得分:-1)
使用后您无法清除传感器条件值。
if (sensor_1 == 1)
{
if (sensor_2 == 1)
{
Serial.println("Entry");
sensor_1 = 0;
sensor_2 = 0;
}
}
应该做的伎俩。