#define Buzzer 2
#define InfraredSensorPin 4
#define LedDisp 13
void setup()
{
pinMode(Buzzer,OUTPUT); //设置输出引脚
Serial.begin(9600);
Serial.println("Start!");
pinMode(InfraredSensorPin,INPUT);
pinMode(LedDisp,OUTPUT);
digitalWrite(LedDisp,LOW);
}
void loop()
{
if(digitalRead(InfraredSensorPin) == LOW)
{digitalWrite(LedDisp,HIGH);
Buzzer_Di();
}
else digitalWrite(LedDisp,LOW);
Serial.print("Infrared Switch Status:");
Serial.println(digitalRead(InfraredSensorPin),BIN);
delay(1000);
}
void Buzzer_Di()
{
digitalWrite(Buzzer,HIGH); //蜂鸣器响
delay(200);
digitalWrite(Buzzer,LOW); //蜂鸣器关闭
delay(200);
}
如果没有障碍就不会亮灯和蜂鸣,串口输出就变成1