首页 > 解决方案 > 蜂鸣器 持续蜂鸣 HIGH 然后 LOW

问题描述

在我的项目中,我使用 GSM900A mini、GPS Neo 6m、超声波传感器和蜂鸣器。

问题是我有两个草图,1)**用于 GSM 和 GPS,**2)用于超声波和蜂鸣器,当我分别使用这两个草图时工作正常,但是当我结合蜂鸣器开始像闪烁的 LED HIGH_LOW_HIGH_LOW 连续发出哔哔声时。

草图 1) GPS 和 GSM

#include <SoftwareSerial.h>
#include <TinyGPS.h>

int state = 0;
const int pin = 9;    //Push Button
float gpslat, gpslon;

TinyGPS gps;
SoftwareSerial sgps (7, 6);
SoftwareSerial sgsm (2, 3);

void setup()
{        
    sgsm.begin (9600);    // Setting the baud rate of GSM Module
    sgps.begin (9600);    // Setting the baud rate of GPS Module
}

void loop()
{       
    while (sgps.available ())
    {
        int c = sgps.read ();
        if (gps.encode (c))
        {
            gps.f_get_position (&gpslat, &gpslon);
        }
    }
    if (digitalRead (pin) == HIGH && state == 0)
    {
        sgsm.print ("\r");
        delay (1000);
        sgsm.print ("AT+CMGF=1\r");
        delay (1000);
       //Replace XXXXXXXXXX to 10 digit mobile number &

       sgsm.print ("AT+CMGS=\"+91XXXXXXXXXX\"\r");
       delay (1000);
       //The text of the message to be sent.
       sgsm.print ("I am at : ");
       sgsm.print ("http://maps.google.com/maps?q=loc:");
       sgsm.print (gpslat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : gpslat, 6);    
       sgsm.print (",");
       sgsm.print (gpslon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : gpslon, 6);
       delay (200);
       sgsm.println ((char) 26); // End AT command with a ^Z, ASCII code 26
       delay (200);
       sgsm.println ();
       delay (1000);
       sgsm.write (0x1A);
       delay (1000);
       state = 1;
   }
   if(digitalRead (pin) == LOW)
   {
       state = 0;
   }
}

草图 2) 超声波和蜂鸣器

 // Define pins for ultrasonic and buzzer
int const trigPin = 11;
int const echoPin = 10;
int const buzzPin = 8;

void setup()
{
  pinMode(trigPin, OUTPUT); // trig pin will have pulses output
  pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
  pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}

void loop()
{
  // Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
  int duration, distance;
  // Output pulse with 1ms width on trigPin
  digitalWrite(trigPin, HIGH); 
  delay(1);
  digitalWrite(trigPin, LOW);
  // Measure the pulse input in echo pin
  duration = pulseIn(echoPin, HIGH);
  // Distance is half the duration devided by 29.1 (from datasheet)
  distance = (duration/2) / 29.1;
  // if distance less than 0.5 meter and more than 0 (0 or less means over range) 
    if (distance <= 60 && distance >= 0) {
      // Buzz
      digitalWrite(buzzPin, HIGH);
    } else {
      // Don't buzz
      digitalWrite(buzzPin, LOW);
    }
    // Waiting 60 ms won't hurt any one
    delay(60);
}

和组合代码

#include <SoftwareSerial.h>
#include <TinyGPS.h>

int const trigPin = 11;    //Ultrasonic
int const echoPin = 10;   //Ultrasonic
int const buzzPin = 8;    //Ultrasonic
int state = 0;
const int pin = 9;    //Push Button
float gpslat, gpslon;

TinyGPS gps;
SoftwareSerial sgps (7, 6);
SoftwareSerial sgsm (2, 3); //2,3

void setup()
{
  pinMode (trigPin, OUTPUT);  // trig pin will have pulses output
  pinMode (echoPin, INPUT); // echo pin should be input to get pulse width
  pinMode (buzzPin, OUTPUT);  // buzz pin is output to control buzzering

  sgsm.begin (9600);    // Setting the baud rate of GSM Module
  sgps.begin (9600);    // Setting the baud rate of GPS Module
}

void loop()
{
  // Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
  int duration, distance;
  // Output pulse with 1ms width on trigPin
  digitalWrite (trigPin, HIGH);
  delay (1);
  digitalWrite (trigPin, LOW);
  // Measure the pulse input in echo pin
  duration = pulseIn (echoPin, HIGH);
  // Distance is half the duration devided by 29.1 (from datasheet)
  distance = (duration / 2) / 29.1;
  // if distance less than 0.5 meter and more than 0 (0 or less means over range) 
  if (distance <= 70 && distance >= 0)
  {
    // Buzz
    digitalWrite (buzzPin, HIGH);
  }
  else
  {
    // Don't buzz
    digitalWrite (buzzPin, LOW);
  }
  // Waiting 60 ms won't hurt any one
  delay (60);

  //gsm gps................................................
  while (sgps.available ())
  {
    int c = sgps.read ();
    if (gps.encode (c))
    {
      gps.f_get_position (&gpslat, &gpslon);
    }
  }
  if (digitalRead (pin) == HIGH && state == 0)
  {
    sgsm.print ("\r");
    delay (1000);
    sgsm.print ("AT+CMGF=1\r");
    delay (1000);
    /*Replace XXXXXXXXXX to 10 digit mobile number &
      ZZ to 2 digit country code */
    sgsm.print ("AT+CMGS=\"+919179413150\"\r");
    delay (1000);
    //The text of the message to be sent.
    sgsm.print ("I am at : ");
    sgsm.print ("http://maps.google.com/maps?q=loc:");
    sgsm.print (gpslat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : gpslat, 6);
    //Gsm.print(" Longitude = ");
    sgsm.print (",");
    sgsm.print (gpslon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : gpslon, 6);
    delay (200);
    sgsm.println ((char) 26); // End AT command with a ^Z, ASCII code 26
    delay (200);
    sgsm.println ();
    //sgsm.print("Latitude :");
    //sgsm.println(gpslat, 6);
    //sgsm.print(",");
    //sgsm.print("Longitude:");
    //sgsm.println(gpslon, 6);
    delay (1000);
    sgsm.write (0x1A);
    delay (1000);
    state = 1;
  }
  if(digitalRead (pin) == LOW)
  {
    state = 0;
  }
}

我的 GSM 和 GPS 图图表

我在做什么错?任何形式的提示表示赞赏。

标签: carduinoarduino-unogsmarduino-ultra-sonic

解决方案


推荐阅读