-1

The problem is that sometime I don't even need to press the button and it sent message and keep on sending continuously.

What I want to do is that once the button is pressed, sent 1 message.

I cannot figure out why it is happening. Please help me to figure out.

 #define CUSTOM_SETTINGS
    #define INCLUDE_GPS_SHIELD
    #define INCLUDE_SMS_SHIELD
    #define INCLUDE_VIBRATION_SHIELD

/* Include 1Sheeld library.*/
#include <OneSheeld.h>
#include <string.h>
#define SW 8


/* Define a boolean flag. */

float lat;
float lon;
String Message;
String Message1;
String Message2;
char latitude[10];
char longitude[10];


void setup() 
{
  pinMode(SW, INPUT);
  /* Start communication.*/
  Serial.begin(9600);
  OneSheeld.begin();

}

void Emergency() {

      /* Send SMS. */
      if(digitalRead(SW) == 1)
      {  
        lat = GPS.getLatitude();
        lon = GPS.getLongitude();
        Message1 = String(lat,8);
        Message2 = String(lon,8);

        Message = "I need Help! Google Maps - https://www.google.co.in/maps/place/" + Message1 + "," + Message2;
        Serial.println(Message);

        SMS.send("0637200721",Message);
        //Vibration.start(1000,2000);

        delay(3000);
      }
}

void loop()
{ 
  Emergency();

  delay(1000);
}

This is how I connect the button. The Grey cable is for Ground, The red is for 5V and the orange is to pin8. Between Red and Grey there is resistor.

  • I don't have a coding answer for you, but your problem is almost certainly contact bounce. There are de-bounce settings, but these tend to be one-way. A better approach is to detect a press, delay for a short time then confirm the button is still pressed. – Milliways Apr 28 '18 at 6:16
  • @Milliways I am quite new to this field. So can you advice me more how to figure it out. Thanks – Htoo Apr 28 '18 at 6:28
  • There are plenty of references e.g. playground.arduino.cc/Learning/SoftwareDebounce – Milliways Apr 28 '18 at 7:00
  • 1
    I am trying to puzzle out the wiring of your button. It looks like you have +5V -> Resistor -> GND and then also GPIO -> GND, and alongside that Nothing -> Button -> GND. Learn how to wire a button! – Majenko Apr 28 '18 at 11:13
-1

The problem is because I didn't put the following lines and I changed to use physical push button to 1Sheeld provided button from mobile. The follwing is the working code.

#define CUSTOM_SETTINGS
#define INCLUDE_GPS_SHIELD
#define INCLUDE_SMS_SHIELD
#define INCLUDE_VIBRATION_SHIELD
#define INCLUDE_PUSH_BUTTON_SHIELD

/* Include 1Sheeld library.*/
#include <OneSheeld.h>
#include <string.h>


/* Define a boolean flag. */
boolean isMessageSent = false;
float lat;
float lon;
String Message;
String Message1;
String Message2;
char latitude[10];
char longitude[10];


void setup() 
{
  /* Start communication.*/
  Serial.begin(9600);
  OneSheeld.begin();
  PushButton.setOnButtonStatusChange(&Emergency);
}

void loop()
{ 

}


void Emergency(byte isButtonPressed) {
 if(isButtonPressed)
      {  
        if(!isMessageSent)
        lat = GPS.getLatitude();
        lon = GPS.getLongitude();
        Message1 = String(lat,8);
        Message2 = String(lon,8);

        Message = "I need Help! Google Maps - https://www.google.co.in/maps/place/" + Message1 + "," + Message2;
        Serial.println(Message);

        SMS.send("0637200721",Message);
        isMessageSent = true; // This line 
      }
      else{
        isMessageSent = false; // This line
      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.