Raspberry Pi ultrasonic ranging + buzzer (c language)

  We have explained in detail the distance measurement of the ultrasonic module controlled by the Raspberry Pi ( http://www.cnblogs.com/yuemo/p/8888342.html ) and the sound of the ultrasonic control buzzer module ( http://www.cnblogs .com/yuemo/p/8906111.html ).

  Today we will take a look at how to apply both modules to make an anti-theft device.

  Let's take a look at the connection method of each pin (requires seven female-to-female DuPont wires):

  Ultrasonic Module:

    1.Vcc: connect to 5V power supply (connect to pin 1)

    2.Trig: output port (connect to pin 16)

    3.Echo: input port (connect to port 18)

    4.Gnd: ground terminal (connected to port 6)

  Buzzer module

    1.GND ground pin (connect to pin 14 of Raspberry Pi)

    2. I/O input and output pins (connect to Raspberry Pi No. 11 pin - GPIO.0)

    3. VCC is connected to the power pin (connected to pin 4 of the Raspberry Pi)


 

  Next, attach the c language code:

#include <wiringPi.h>
#include <stdio.h>
#include <sys/time.h>
#define Trig 4
#define Echo 5
#define VOICE 0

void ultraInit(void){
  pinMode(Echo,INPUT);
  pinMode(Trig,OUTPUT);
  pinMode(VOICE,OUTPUT);
}

float disMeasure(void){
  struct timeval tv1;
  /*
    struct timeval{
      time_t tv_sec;
      suseconds_t tv_usec;
    }
   */

  struct timeval tv2;
  long start=0,stop=0;

  digitalWrite(VDICE,LOW);
  digitalWrite(Trig,LOW);
  delayMicroseconds(2);

  digitalWrite(Trig,HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig,LOW);

  while(!(digitalRead(Echo) == 1));
  gettimeofday(&tv1,NULL);

  while(!(digitalRead(Echo) == 0));
  gettimeofday(&tv2,NULL);

  start = tv1.tv_sec * 1000000 + tv1.tv_usec;
  stop = tv2.tv_sec * 1000000 + tv2.tv_usec;

  return (float)(stop - start) / 1000000*34000 / 2;
}

void doVoice(void){
  for(int i=1;i<=6;i++){
    digitalWrite(VOICE,HIGH);
    delay(500);
    digitalWrite(VOICE,LOW);
    delay(1000);
  }
}

int main(){
  float dis;
  float olddis=0;
 
  if(wiringPiSetup() == -1){
    printf("setup wiringPi failed!");
    return 1;
  }

  ultraInit();

  while(1){
    dis = disMeasure();
    printf("disMeasure = %0.2f cm\n",dis);
     if(olddis==0){
       olddis=dis;
       continue;
     }else{
       int s=dis-olddis;
       if(s<-30||s>30){
     doVoice();
       }
       olddis=dis;
     }
    delay(1000);
  }
  return 0;
}

  The idea of ​​our program is to use ultrasonic ranging, record the distance of the last ultrasonic ranging, and compare it with the distance of this ultrasonic ranging. If the difference is more than 30cm, the buzzer will alarm. Both thinking and programming are relatively simple.

  If you find that some functions are unclear in the process of reading the program, please move to the blog: https://www.cnblogs.com/lulipro/p/5992172.html

 


    This experiment is the first attempt in a new field, and I have never been exposed to Raspberry Pi-based program development before. I encountered many problems at the beginning:
    1. The Raspberry Pi system was programmed incorrectly, and it took a long time at that time.
    2. The choice of experimental equipment, for me to choose OCD is really a terrible thing, I bought a lot of useless things, including but not limited to 3.5-inch screen, Raspberry Pi pin expansion board, 170-hole bread Board (too small), joystick module, 4*4 membrane keyboard, temperature sensor, one relay, electric fan. Before I started, I had a big heart and wanted to do something amazing, but at the end I didn’t have enough time to react. Let’s start with something simple.
    3. Male-to-male, female-to-female, 40 male-to-female Dupont lines each... I don't need so many at all, QWQ, uncomfortable
    4. The code and tutorials in the c language are really hard to find. I finally found a blog that introduces wiringPi, and it is a treasure. Now the Raspberry Pi textbooks on the market are basically based on python, and python is a very easy to use. language, but the teacher asks me to use the C language and I have no choice.
    5. github is useful, but you need to know how to use it. Learning to port code reasonably and gracefully is a required course for a programmer.

  Author's contact information: [email protected] Any questions you don't understand in the blog are welcome.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568872&siteId=291194637
Recommended