Arduino 发射和接收红外线

//学号后两位26
#define com_hear 2
#define com_sound 3
#define com_speak 4
void setup() {
  pinMode(com_sound, OUTPUT);
  pinMode(com_speak, OUTPUT);
  pinMode(com_hear, INPUT);
  Serial.begin(9600);

}

void loop() {
  int i, j, k;
  int hear_sta[12] = {0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0};
  int speak[12] = {0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1};
  int hear[12] = {0};
  i = 0;
  int judge = 0;
  int count = 0;
  // while(judge != 1)
  // {
  for (k = 0; k < 12; k++)
  {
    if (speak[k] == 0)
    {
      for (i = 0; i < 30; i++)
      {
        digitalWrite(com_speak, HIGH);
        delayMicroseconds(13);
        digitalWrite(com_speak, LOW);
        delayMicroseconds(13);
      }
    }
    else if (speak[k] == 1)
    {
      digitalWrite(com_speak, LOW);
      delayMicroseconds(1100);
    }
  }
  for ( i = 0; i < 1000; i++)
  {
    hear[0] = digitalRead(com_hear);
    delayMicroseconds(550);
    if (hear[0] == 0)
    {
      for (j = 1; j < 12; j++)
      {
        delayMicroseconds(1100);
        hear[j] = digitalRead(com_hear);
      }
      for (j = 0; j < 12; j++)
      {
        if (hear[j] == hear_sta[j])
          count++;
      }
      if (count == 12)
      {
        for (int x = 0; x < 1000; x++)
        {
          digitalWrite(com_sound, HIGH);
          delayMicroseconds(250);
          digitalWrite(com_sound, LOW);
          delayMicroseconds(250);
        }
      }
    }
    delayMicroseconds(100);
    count = 0;
  }


  for (i = 0; i < 12; i++)
    Serial.print(hear[i]);
  Serial.println();

}

你可能感兴趣的:(Arduino 发射和接收红外线)