如何从Arduino上的红外传感器接收高或低信号?

时间:2017-12-18 19:14:59

标签: arduino arduino-uno arduino-ide

我正在尝试使用我的Arduino Uno的IR传感器,只想要一个HIGH或LOW信号而不解码使任何IR信号将状态转换为1或0.还有一个运动传感器但该代码已经除去。

int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int relayPin = 4; //PIN FOR RELAY OPERATION
int irPin = 7; //IR Sensor pin
int lightState = 0;
int irVal = 0;

void setup() {
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(inputPin, INPUT); // declare sensor as input
  pinMode(relayPin, OUTPUT);
  pinMode(irPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  irVal = digitalRead(irPin);
  if (irVal == HIGH) {
    lightState = 1;
    Serial.println("IR received");
    while(irVal == HIGH) {
      irVal = digitalRead(irPin);
      if(irVal == HIGH) {
        irVal = LOW;
      } else {
        irVal = HIGH;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您是否试图说输入无法正常工作?也许在设置循环中尝试使用INPUT_PULLUP而不是INPUT。

例如: pinMode(inputPin,INPUT_PULLUP); 您可以在此处找到有关此原则的信息: https://www.arduino.cc/en/Tutorial/InputPullupSerial