本文我们将通过Arduino连线E18-D80NK红外开关传感器进行简单测试, E18-D80NK 是一个高灵敏度的红外光线开关传感器,它可以用来检测物体或者作为光电开关使用,测量检测范围从3cm到80cm。
E18-D80NK市面上有两种版本,区别在于线缆颜色不同,一种是棕黑蓝三色线分别对应+5V,OUT和GND;一种是红黄黑三色线分别对应 +5V,OUT和GND 。上图的引脚定义图是前者。
void setup(){ Serial.begin(9600); //Start serial communication boud rate at 9600 pinMode(2,INPUT); //Pin 2 as signal input } void loop(){ while(1){ delay(500); if(digitalRead(2)==LOW){ // If no signal print collision detected Serial.println("Collision Detected."); } else{ // If signal detected print collision detected Serial.println("No Collision Detected."); } }
单纯终端函数
int pinInterrupt = 2; //接中断信号的脚 void onChange() { if ( digitalRead(pinInterrupt) == LOW ) Serial.println("Key Down"); else Serial.println("Key UP"); } void setup() { Serial.begin(9600); //打开串口 pinMode( pinInterrupt, INPUT);//设置管脚为输入 //Enable中断管脚, 中断服务程序为onChange(), 监视引脚变化 attachInterrupt( digitalPinToInterrupt(pinInterrupt), onChange, CHANGE); } void loop() { // 模拟长时间运行的进程或复杂的任务。 for (int i = 0; i < 100; i++) { // 什么都不做,等待10毫秒 delay(10); } }
原文:https://www.cnblogs.com/kekeoutlook/p/12692961.html