include
// 定义光电传感器的输入引脚
const int sensorPin = 2;
// 设置光电传感器的阈值
const int threshold = 500;
void setup()
{
// 初始化串口通信
Serial.begin(9600);
// 设置光电传感器引脚为输入模式
pinMode(sensorPin, INPUT);
}
void loop()
{
// 读取光电传感器的值
int sensorValue = analogRead(sensorPin);
// 判断光电传感器值是否超过阈值
if (sensorValue > threshold)
{
// 如果超过阈值,输出高电平
digitalWrite(13, HIGH);
}
else { // 如果未超过阈值,输出低电平 digitalWrite(13, LOW); }
// 延时100毫秒
delay(100); }
光电传感器(Arduino)
于 2024-06-02 16:41:27 首次发布