Having an issue getting the Arduino to send an SMS once button pressed, then resetting ready to send another SMS when a button is pressed again. The code I am using is below! Thanks
*/
#include <MKRGSM.h>
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// this constant won't change:
const int buttonPin = A2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
// connection state
bool connected = false;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("Button has been pressed");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
sendSMS();
} else {
delay (500);
}
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
void sendSMS()
{
Serial.println("SMS Messages Sender");
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (!connected) {
if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
connected = true;
} else {
Serial.println("Not connected");
delay(5000);
}
}
Serial.println("GSM initialized");
const char remoteNum[] = ("xxxxxx");
// SMS text
const char txtMsg[] = ("Gate Opened");
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(remoteNum);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
exit(0);
}