Interfacing GSM module code optimization

Initiate the modem with a button press. SMS notification is sent upon completion of process. No delays.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 4); // RX, TX

const int buttonPin = 2;     // the number of the pushbutton pin
int redLED = 9; // extra LED for testing
int greenLED = 7; // ON when startup procedure is over // modem is ready to handle text messages

int buttonState = 0;         // variable for reading the pushbutton status

String text = "Module Active"; // type message to be sent
String phone = "+351xxxYYYzzz"; // receiver's number

boolean triggerInitModem = false;
boolean triggerSMS = false;

void setup() {
	pinMode(greenLED, OUTPUT);
	pinMode(redLED, OUTPUT);
	pinMode(buttonPin, INPUT_PULLUP);
	// Open serial communications and wait for port to open:
	Serial.begin(38400);
	while (!Serial) {
		; // wait for serial port to connect. Needed for native USB port only
	}
	Serial.println("Goodnight moon!");
	// set the data rate for the SoftwareSerial port
	mySerial.begin(38400);
	mySerial.println("Hello, world?");
}

void loop() {
	// reads back and displays the response to the AT commands sent through the Serial Monitor
	if (mySerial.available()) {
		Serial.write(mySerial.read());
	}
	if (Serial.available()) {
		mySerial.write(Serial.read());
	}
	
	// read the state of the pushbutton value:
	buttonState = digitalRead(buttonPin);
	// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
	if (buttonState == HIGH) {
		// turn LED on:
		digitalWrite(redLED, HIGH);
	} else {
		// turn LED off:
		digitalWrite(redLED, LOW);
		triggerInitModem = true; // button was pressed, send standard sms
	}
	if (triggerInitModem == true) {
		InitModem();
	}
	if (triggerSMS == true) {
		sms(text, phone);
	}
}
void sms(String text, String phone) {
	static word startTimer, waitTimer;  // waitTimer > 0 causes the timer to run, good up to 65 second intervals
	static byte procState;
	if ( waitTimer > 0 )
	{
		if ( word( millis() ) - startTimer < waitTimer )      return;     // time is not up yet
		else waitTimer = 0; // and on to the switch-case
	}
	switch ( procState )
	{
	case 0 :
		mySerial.println("AT+CMGF=1"); // Set SMS Text Mode
		procState = 1;      //  run case 1 next time
		waitTimer = 2000;
		startTimer = millis();
		break;
	case 1 :
		mySerial.println("AT+CSCS=\"GSM\"");  //
		procState = 2;      //  run case 2 next time
		waitTimer = 2000;
		startTimer = millis();
		break;
	case 2 :
		mySerial.println("AT+CMGS=\"" + phone + "\"");  // send to desired phone
		procState = 3;      //  run case 3 next time
		waitTimer = 2000;
		startTimer = millis();
		break;
	case 3 :
		mySerial.print("\"" + text + "\""); // containing desired text
		procState = 4;      //  run case 4 next time
		waitTimer = 2000;
		startTimer = millis();
		break;
	case 4 :
		mySerial.print((char)26); // signals end of text message
		procState = 0;      //  run case 0 next time
		waitTimer = 2000;
		startTimer = millis();
		triggerSMS = false;
		break;
	}
}

void InitModem() {
	static word startTimer, waitTimer;  // waitTimer > 0 causes the timer to run, good up to 65 second intervals
	static byte procState;
	if ( waitTimer > 0 )
	{
		if ( word( millis() ) - startTimer < waitTimer )      return;     // time is not up yet
		else waitTimer = 0; // and on to the switch-case
	}
	switch ( procState )
	{
	case 0 :
		mySerial.println("AT+CMGD=1,4");        // Delete all messages so there are no conflicts with accidentally unread or stored SMS
		procState = 1;      //  run case 1 next time
		waitTimer = 2000;
		startTimer = millis();
		break;
	case 1 :
		mySerial.println("AT+CMGF=1");          // Set the module message mode, set either at 0 or 1 : 0 = PDU / 1 = Text mode.
		procState = 2;      //  run case 2 next time
		waitTimer = 2000;
		startTimer = millis();
		break;
	case 2 :
		mySerial.println("AT+CSCS=\"GSM\"");      // Set the character sets : “GSM” = default alphabet (GSM03.38.6.2.1)
		procState = 3;      //  run case 3 next time
		waitTimer = 2000;
		startTimer = millis();
		break;
	case 3 :
		mySerial.println("AT+CNMI=2,2,0,0,0");  // Set message indication Format, AT+CNMI=[<mode>[,<mt>[,<bm>[,<ds>[,<bfr>]]]]]
		procState = 0;      //  run case 4 next time
		waitTimer = 2000;
		startTimer = millis();
		digitalWrite(greenLED, HIGH); // signal InitModem finished, modem ready to receive commands
		triggerSMS = true;
		triggerInitModem = false;
		break;
	}
}