There is a mistake in the design implementation of the code probably in if statment - when using the serial monitor it repeatedly shows printf() statements from the code instead of "Sending SMS message".
// Include the GSM library
#include <MKRGSM.h>
#include<math.h>
#include<stdio.h>
int xp = A5;
int yp = A4;
int x,y;
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
void setup() {
x=analogRead(xp);delay(2);
y=analogRead(yp);delay(2);
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("SMS Messages Sender");
pinMode(xp,INPUT);
pinMode(yp,INPUT);
// connection state
boolean connected = false;
// 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(1000);
}
}
Serial.println("GSM initialized");
}
void loop() {
if ((x>=0.60) & (y>=40))
Serial.print("AT+CMGF=1\r");
delay(1000);
Serial.print("AT+CMGS=\"+491781234567\"\r");
char remoteNum[20];
// sms text
Serial.print("your bike is in danger!");
char txtMsg[200];
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");
delay(5000);
if ((x<=0.30) & (y<=25))
Serial.print("AT+CMGF=1\r");
delay(1000);
Serial.print("AT+CMGS=\"+491781234567\"\r");
// sms text
Serial.print("your bike is in danger");
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");
delay(5000);
}