基于Arduino和Mixly(超声波+蜂鸣器)实现距离报警

1.下载安装Mixly  

             http://mixly.org/bnu-maker/mixly-arduino-win

             http://www.yfrobot.com/thread-12034-1-1.html

 2.硬件准备:arduino控制板、USB 数据线、无源蜂鸣器低电平触发MH-FMD、连接线*7、超声波HC-SR04

 3.硬件连接:

蜂鸣器的连接:

超声波的连接:

 基于Arduino和Mixly(超声波+蜂鸣器)实现距离报警_第1张图片

4.Mixly模块:

基于Arduino和Mixly(超声波+蜂鸣器)实现距离报警_第2张图片

代码:

volatile float dist;



float checkdistance_A2_A3() {

  digitalWrite(A2, LOW);

  delayMicroseconds(2);

  digitalWrite(A2, HIGH);

  delayMicroseconds(10);

  digitalWrite(A2, LOW);

  float distance = pulseIn(A3, HIGH) / 58.00;

  delay(10);

  return distance;

}



void setup(){

  dist = 0;

  pinMode(A2, OUTPUT);

  pinMode(A3, INPUT);

  Serial.begin(9600);

  pinMode(10, OUTPUT);

}

//如果距离小于5cm,蜂鸣器报警并重复3次

void loop(){

  dist = checkdistance_A2_A3();

  Serial.print("距离:");

  Serial.println(dist);

  if (dist < 5) {

    for (int i = 1; i <= 3; i = i + (1)) {

      digitalWrite(10,HIGH);

      delay(300);

      digitalWrite(10,LOW);

      delay(300);

    }



  }

}

 

5.观察效果:手放置在传感器,距离小于5cm,蜂鸣器报警3次

6.相关学习链接:

http://www.yfrobot.com/thread-37006-1-1.html

http://www.yfrobot.com/thread-35641-1-1.html

https://gitee.com/liqiujian/BJT/tree/master(源码地址及用到的第三方YFRobot_Mixly_V1.9.1库)

 

你可能感兴趣的:(基于Arduino和Mixly(超声波+蜂鸣器)实现距离报警)