【Arduino】使用激光发射器/接收器来模拟电影里的激光报警器

本次的实验采用的是激光发射和接收两个模块,模拟电影里面的激光报警器,如果想让更好体验到报警效果可以添加一个蜂鸣器来查看效果。

本次完成的内容需要的材料:

 Arduino UNO  X2
Laser Transmitter module  x1
Laser Receiver module     x1

导线(若干)

线路图:

所需代码:

Laser send:

int lasertransmitterPin=8; //definition digital 8 pin to control the laser transmitter module

void setup()

{

    pinMode(lasertransmitterPin,OUTPUT);    //Set the digital 8 port mode, OUTPUT: Output mode

}

void loop()

{  

    digitalWrite(lasertransmitterPin,HIGH); //HIGH is set to about 5V PIN8

  //  delay(5000);                            //Set the delay time, 5000 = 5S

  //digitalWrite(lasertransmitterPin,LOW); //LOW is set to  PIN8

    //  delay(5000);                           //Set the delay time, 5000 = 5S

    

} 

Laser recv:
int laserreceiverPin=8;            //Set the digital 8 to the S pin

int LED=13;

void setup() {

  pinMode( LED,OUTPUT);//initialize the S pin as input

  pinMode( laserreceiverPin,INPUT);//initialize the S pin as input

  Serial.begin(115200 );             //opens serial port, sets data rate to 9600 bps

}



void loop() {

  if(digitalRead(laserreceiverPin)==LOW){

    digitalWrite(LED,HIGH);

    

     Serial.println("No laser received");      //send data to the serial monitor

  }else{

     Serial.println("Received laser");   //send data to the serial monitor

     digitalWrite(LED,LOW);

  }

        delay(500);                          //delay 0.2s

}

评论 1

  • Alex
    Alex @ GroovyCreation

    Great!

  • 跳至工具栏