Hello, can someone review this code and tell me why the Arduino GSM shield take so long to connect to the network. If I connect one or three time today then I can only connect the next day. The time between the connection is extremely long.
/*
Project.
*/
#include <Stepper.h>
#include <GSM.h>
#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
//SR04_Range_Finder and Speaker pins
#define trigPin 9
#define echoPin 8
#define speakerPin 31 //Speaker pin
//Stepper Motor
int motorPin1 = 46; //Green wire
int motorPin2 = 47; //Blue wire
int motorPin3 = 48; //Brown
int motorPin4 = 49; //Gray
//Switch button and led
const int ledPin = 44; //led attached to this pin
const int buttonPin = 42; //the push button attach to this pin
const int readyPin = 40; //Light Led when switch is ready
const int analogPin = 0; //A0 Alcohol sensor pin
int buttonState = LOW; //What's the state of the button, whether push or not
int val, avg,i,caron=0;
int Steps2Take;
long duration, distance;
char remoteNum[13]="+1917xxxxxx"; // telephone number to send sms
char txtMsg1[49]="Hi Mom its Johnny, My Car won't start, fail test";
char txtMsg2[54]="Johnny is having a cardiac problem, Pulse is too high";
#define STEPS 64 // Number of steps per revolution
Stepper small_stepper(STEPS, motorPin1, motorPin3, motorPin2, motorPin4);
// Pulse Sensor VARIABLES
int pulsePin = 1; // Pulse Sensor purple wire connected to analog pin 1
int blinkPin = 4; // pin to blink led at each beat
int fadePin = 5; // pin to do fancy classy fading blink at each beat
int fadeRate = 0; // used to fade LED on with PWM on fadePin
int hazardLedPin = 6; //pin to send hazard warning
int counter_IBI = 0, counter_BPM = 0, Killcnt = 0;
// Pulse - these variables are volatile because they are used during the interrupt service routine!
volatile int BPM; // used to hold the pulse rate
volatile int Signal; // holds the incoming raw data
volatile int IBI = 600; // holds the time between beats, the Inter-Beat Interval
volatile boolean Pulse = false; // true when pulse wave is high, false when it's low
volatile boolean QS = false; // becomes true when Arduoino finds a beat.volatile int speakerPin = 12;
//GSM Variables
int timeTosend=1;
int count=0;
char phone_no[]="xxxxxxxx"; // phone number
void setup(){
//Initialize Pulse sensor pins
pinMode(blinkPin,OUTPUT); // pin that will blink to your heartbeat!
pinMode(fadePin,OUTPUT); // pin that will fade to your heartbeat!
pinMode(speakerPin,OUTPUT); // pin that will fade to your heartbeat!
pinMode(hazardLedPin,OUTPUT); //pin that will blink to hazard situation
Serial.begin(9600); // we agree to talk slow!
delay(2);
interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
small_stepper.setSpeed(200);
//Initialize - set the mode of the push button and ready pins
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(readyPin, OUTPUT);
//Initialize Range finder and Speaker pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(speakerPin, OUTPUT);
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop(){
int i_cnti = 0, i_cntb = 0;
val=0,i=0,avg=0;
if(caron==0){
digitalWrite(readyPin, HIGH); //turn on ready led
//digitalWrite(ledPin, HIGH); //turn on led
for(i=0;i<20;i++){ //read breathalyzer
val=analogRead(analogPin);//Read Alcohol sensor value from analog 0
delay(100);
avg=avg+val; //* time interval input
}
avg=avg/20;
Serial.println(avg,DEC); //Print the value to serial port
buttonState = digitalRead(buttonPin);//read button
if((buttonState == HIGH) && (avg > 300) && (avg < 500) && (Killcnt == 0))//if breath is checked and below alcohol value - start engine
{
caron=1;
digitalWrite(readyPin, LOW); //turn off ready led
digitalWrite(ledPin, HIGH); //turn on blue led to show car running
small_stepper.setSpeed(300);
Steps2Take = 400; //Rotate cw
small_stepper.step(Steps2Take);
delay(20);
small_stepper.setSpeed(300);
Steps2Take = -400; //Rotate ccw
small_stepper.step(Steps2Take);
delay(20);
buttonState = digitalRead(buttonPin);
//digitalWrite(ledPin, LOW); //turn off led
}
else if(avg > 800){
sms.beginSMS(remoteNum);
sms.print(txtMsg1);
sms.endSMS();
Killcnt++;
for(int h = 0; h < 4; ++h){
digitalWrite(hazardLedPin, HIGH);
delay(100); // wait for a second
digitalWrite(hazardLedPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
}
Serial.println(txtMsg1);
}
}
if(caron==1){
//distance sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
// Serial.println(distance); //Print the value to serial port
//Pulse processing
// sendDataToProcessing('S', Signal); // send Processing the raw Pulse Sensor data
if (QS == true){ // Quantified Self flag is true when arduino finds a heartbeat
fadeRate = 255; // Set 'fadeRate' Variable to 255 to fade LED with pulse
sendDataToProcessing('B',BPM); // send heart rate with a 'B' prefix
sendDataToProcessing('Q',IBI); // send time between beats with a 'Q' prefix
// sendDataToProcessing('Q',IBI); // send time between beats with a 'Q' prefix
QS = false; // reset the Quantified Self flag for next time
}
ledFadeToBeat();
//Range finder
//if((distance<40)||(BPM>130))
if(distance<40)
{
for(i=0;i<100;i++){
digitalWrite(speakerPin, HIGH);//speaker beep
delay(1);
digitalWrite(speakerPin, LOW);
delay(1);
}
}
//Alert to put hand on staring wheel
if((IBI > 200) && (IBI < 330)){
counter_IBI++;
if(counter_IBI >= 1000) {
counter_IBI = 0;
for(i=0;i<100;i++){
digitalWrite(speakerPin, HIGH);//speaker beep
delay(2);
digitalWrite(speakerPin, LOW);
delay(2);
digitalWrite(speakerPin, HIGH);//speaker beep
delay(2);
digitalWrite(speakerPin, LOW);
delay(2);
}
}
//Serial.println(counter_IBI);
}
//Serial.println(counter_IBI);
//Alert to activate hazard light and hand on staring wheel and call home
if((BPM > 120) && (BPM < 200) && (IBI > 480) && (IBI < 506)){
sms.beginSMS(remoteNum);
sms.print(txtMsg2);
sms.endSMS();
Serial.println(txtMsg2);
for(int h=0;h<4;++h){
digitalWrite(hazardLedPin, HIGH);
delay(100); // wait for a second
digitalWrite(hazardLedPin, LOW); // turn the LED off by making the voltage LOW
delay(100);
}
for(i=0;i<100;i++){
digitalWrite(speakerPin, HIGH);//speaker beep
delay(2);
digitalWrite(speakerPin, LOW);
delay(2);
digitalWrite(speakerPin, HIGH);//speaker beep
delay(2);
digitalWrite(speakerPin, LOW);
delay(2);
}
}
}//end car on
}//end main
void ledFadeToBeat(){
fadeRate -= 15; // set LED fade value
fadeRate = constrain(fadeRate,0,255); // keep LED fade value from going into negative numbers!
analogWrite(fadePin,fadeRate); // fade LED
}
void sendDataToProcessing(char symbol, int data ){
Serial.print(symbol); // symbol prefix tells Processing what type of data is coming
Serial.println(data); // the data to send culminating in a carriage return
}