用Tinkercad学arduino之 人体红外检测报警 蜂鸣器+LED

项目地址: https://www.tinkercad.com/things/dXbkCwjcshu 

 

int ledPin = 5;               
int inputPin = 2;               
int pinSpeaker = 6;        
int pirState = LOW;           
int val = 0;                  

void setup() {
  pinMode(ledPin, OUTPUT);     
  pinMode(inputPin, INPUT);    
  pinMode(pinSpeaker, OUTPUT);
}

void joueSon (long duree, int frequence) {
    duree *= 1000;
    int period = (1.0 / frequence) * 1000000;
    long temps_passe = 0;
    while (temps_passe < duree) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        temps_passe += (period);
    }
}


void loop(){
  val = digitalRead(inputPin); 

  if (val == HIGH) {
    digitalWrite(ledPin, HIGH);  
    joueSon(300, 160);
    delay(150);
    } 
  
  else {
      digitalWrite(ledPin, LOW);
      joueSon(0, 0);
      delay(300);    
   }
}

 

posted @ 2020-12-03 16:54  meetrice  阅读(102)  评论(0编辑  收藏