在arduino上实现红外传感器



是否有办法在Arduino代码中实现红外传感器作为输入?我希望传感器将数据发送到Arduino的值(IR位置的变化),然后使用该值作为软件的输入。

Code是一个光电阻传感器的例子,每当它变暗时打开LED,当光传感器检测到它变亮时将其关闭。

int sensor1Value = 0;
void setup()
{
// declare the ledPins as an OUTPUT:
pinMode(13, OUTPUT);

}
void loop() {
// read the value from the sensor:
sensor1Value = analogRead(A0);
{
if(sensor1Value <200)     // check the value of sensor 
{                          //if the value is less than 200 then turn the leds on
digitalWrite(13, HIGH);
delay(500);
}
else                      // if the value is greater than or equal to 200 then turn leds off
{
digitalWrite(13, LOW);
delay(500);
}
}

最简单的方法是使用红外光电晶体管

在每次digitalWrite()之后不需要延迟,只需在loop()函数的末尾添加它。

void loop() {
// read the value from the sensor:
sensor1Value = analogRead(A0);
if(sensor1Value <200)     // check the value of sensor 
{                          //if the value is less than 200 then turn the leds on
digitalWrite(13, HIGH);

}
else                      // if the value is greater than or equal to 200 then turn leds off
{
digitalWrite(13, LOW);

}
delay(500);
}

最新更新