The easy optimisation is to change int greenLED = 7 etc. to byte greenLED = 7 .
You save one byte each time.
Harder is to get rid of the delays. You have to set a series of state flags and
check / set (if necessary) the state every loop iteration. Here is an example based on your code.
Of course it was much simpler with delays, but these block any other code until they expire.
void sms() {
// called every loop iteration
// parameters smsSend, state, text and phone set as globals
// Initial state is DELETE_ALL_MESSAGES
if ( smsSend == true ) { // if true, we are handling a message send
if ( state == DELETE_ALL_MESSAGES ) {
// set timeout_timer here (if required)
mySerial.print("AT+CMGD=1,4\n\r");
state = WAITING_RESPONSE_DELETE_ALL_MESSAGES ;
}
else if ( state == WAITING_RESPONSE_DELETE_ALL_MESSAGES ) {
if ( test_mySerial_Return() == true ) { // we've got a valid return code from the command
// we've got a valid return code so set up target phone
mySerial.println("AT+CMGS=\"" + phone + "\""); // send to desired phone
state = WAITING_RESPONSE_SETUP_TARGET_PHONE ;
}
}
else if ( state == WAITING_RESPONSE_SETUP_TARGET_PHONE ) {
if ( test_mySerial_Return() == true ) {
// we've got a valid return code so send text
mySerial.print("\"" + text + "\""); // containing desired text
state = WAITING_RESPONSE_SEND_TEXT_TO_TARGET_PHONE ;
}
}
else if ( state == WAITING_RESPONSE_SEND_TEXT_TO_TARGET_PHONE ) {
if ( test_mySerial_Return() == true ) {
// we've got a valid return code so send end of message
mySerial.print((char)26); // signals end of text message
state = WAITING_RESPONSE_SEND_END_OF_MESSAGE ;
}
}
else if ( state == WAITING_RESPONSE_SEND_END_OF_MESSAGE ) {
if ( test_mySerial_Return() == true ) {
// we've got a valid return code so we've successfully sent message
state = MESSAGE_SENT_OK ;
smsSend = false ;
}
}
// test timeout timer here and fail if we're still here after 5 seconds (if required).
}
}