首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在arduino上实现红外传感器

在arduino上实现红外传感器
EN

Stack Overflow用户
提问于 2021-10-05 08:59:48
回答 1查看 65关注 0票数 0

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

代码是一个光阻传感器的例子,它在每次黑暗时打开LED,并在光传感器检测到它的亮度时将其关闭。

代码语言:javascript
复制
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);
}
}
EN

回答 1

Stack Overflow用户

发布于 2021-10-05 11:53:33

最简单的方法是使用IR phototransistor

在每个digitalWrite()之后不需要延迟,只需将其添加到loop()函数的末尾即可。

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69447581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档