How can I make this code shorter? pls.

I made the project “arduino remotely controlled sockets”, and i just have a little problem, out of space, and I dont know if theres any more errors. im counting upon the community’s help. Thanks!

#include <GSM.h>

int AtransistorPin = 13;
int BtransistorPin = 12;
int CtransistorPin = 11;
int DtransistorPin = 10;
#define PINNUMBER ""
char senderNumber[20];
GSM gsmAccess;
GSM_SMS sms;
char remoteNumber[20] = "+639107993724";
char txtMsgA[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-ACTIVE S2-ACTIVE S3-ACTIVE S4-ACTIVE   All Sockets are active";
char txtMsgB[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-INACTIVE S2-INACTIVE S3-INACTIVE S4-INACTIVE   All Sockets are  inactive";
char txtMsgC[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-INACTIVE S2-ACTIVE S3-ACTIVE S4-ACTIVE";
char txtMsgD[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-ACTIVE S2-INACTIVE S3-INACTIVE S4-INACTIVE";
char txtMsgE[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-INACTIVE S2-INACTIVE S3-ACTIVE S4-ACTIVE";
char txtMsgF[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-ACTIVE S2-ACTIVE S3-INACTIVE S4-INACTIVE";
char txtMsgG[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-INACTIVE S2-ACTIVE S3-INACTIVE S4-ACTIVE";
char txtMsgH[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-ACTIVE S2-INACTIVE S3-ACTIVE S4-INACTIVE";
char txtMsgI[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-INACTIVE S2-ACTIVE S3-ACTIVE S4-INACTIVE";
char txtMsgJ[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-ACTIVE S2-INACTIVE S3-INACTIVE S4-ACTIVE";
char txtMsgK[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-ACTIVE S2-ACTIVE S3-ACTIVE S4-INACTIVE";
char txtMsgL[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-INACTIVE S2-INACTIVE S3-INACTIVE S4-ACTIVE";
char txtMsgM[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-INACTIVE S2-INACTIVE S3-ACTIVE S4-INACTIVE";
char txtMsgN[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-ACTIVE S2-ACTIVE S3-INACTIVE S4-ACTIVE";
char txtMsgO[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-INACTIVE S2-ACTIVE S3-INACTIVE S4-INACTIVE";
char txtMsgP[200] = "Arduino Remotely controlled Sockets. Status Report as of now: S1-ACTIVE S2-INACTIVE S3-ACTIVE S4-ACTIVE";

void setup() {

  boolean notConnected = true;

  // Start GSM connection
  while (notConnected) {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
      notConnected = false;
    } else {
    }
  }
  //sets transistorPin as an output, turns off transistor as setup
  pinMode(AtransistorPin, OUTPUT);
  pinMode(BtransistorPin, OUTPUT);
  pinMode(CtransistorPin, OUTPUT);
  pinMode(DtransistorPin, OUTPUT);
  digitalWrite (AtransistorPin, LOW);
  digitalWrite (BtransistorPin, LOW);
  digitalWrite (CtransistorPin, LOW);
  digitalWrite (DtransistorPin, LOW);
}


void loop() {
  char c;

  if (sms.available()) {
    sms.remoteNumber(senderNumber, 20);
    
    if (sms.peek() == '#') {
      sms.flush();
    }
    char c;
    String received = "";

    { while (c = sms.read())
        received += c;

      Serial.print(c);
      if (received.compareTo("on") == 0) {
        digitalWrite(AtransistorPin, HIGH);
        digitalWrite(BtransistorPin, HIGH);
        digitalWrite(CtransistorPin, HIGH);
        digitalWrite(DtransistorPin, HIGH);
      }
      if (received.compareTo("off") == 0) {
        digitalWrite(AtransistorPin, LOW);
        digitalWrite(BtransistorPin, LOW);
        digitalWrite(CtransistorPin, LOW);
        digitalWrite(DtransistorPin, LOW);
      }
      if (received.compareTo("on 1") == 0) {
        digitalWrite(AtransistorPin, HIGH);
      }
      if (received.compareTo("on 2") == 0) {
        digitalWrite(BtransistorPin, HIGH);
      }
      if (received.compareTo("on 3") == 0) {
        digitalWrite(CtransistorPin, HIGH);
      }
      if (received.compareTo("on 4") == 0) {
        digitalWrite(DtransistorPin, HIGH);
      }
      if (received.compareTo("off 1") == 0) {
        digitalWrite(AtransistorPin, LOW);
      }
      if (received.compareTo("off 2") == 0) {
        digitalWrite(BtransistorPin, LOW);
      }
      if (received.compareTo("off 3") == 0) {
        digitalWrite(CtransistorPin, LOW);
      }
      if (received.compareTo("off 4") == 0) {
        digitalWrite(DtransistorPin, LOW);
      }
      if (received.compareTo("STATUS") == 0) {
        if (AtransistorPin == HIGH); (BtransistorPin == HIGH); (CtransistorPin == HIGH); (DtransistorPin == HIGH); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgA);
          sms.endSMS();
        }
        if (AtransistorPin == LOW); (BtransistorPin == LOW); (CtransistorPin == LOW); (DtransistorPin == LOW); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgB);
          sms.endSMS();
        }
        if (AtransistorPin == LOW); (BtransistorPin == HIGH); (CtransistorPin == HIGH); (DtransistorPin == HIGH); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgC);
          sms.endSMS();
        }
        if (AtransistorPin == HIGH); (BtransistorPin == LOW); (CtransistorPin == LOW); (DtransistorPin == LOW); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgD);
          sms.endSMS();
        }
        if (AtransistorPin == LOW); (BtransistorPin == LOW); (CtransistorPin == HIGH); (DtransistorPin == HIGH); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgE);
          sms.endSMS();
        }
        if (AtransistorPin == HIGH); (BtransistorPin == HIGH); (CtransistorPin == LOW); (DtransistorPin == LOW); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgF);
          sms.endSMS();
        }
        if (AtransistorPin == LOW); (BtransistorPin == HIGH); (CtransistorPin == LOW); (DtransistorPin == HIGH); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgG);
          sms.endSMS();
        }
        if (AtransistorPin == HIGH); (BtransistorPin == LOW); (CtransistorPin == HIGH); (DtransistorPin == LOW); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgH);
          sms.endSMS();
        }
        if (AtransistorPin == LOW); (BtransistorPin == HIGH); (CtransistorPin == HIGH); (DtransistorPin == LOW); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgI);
          sms.endSMS();
        }
        if (AtransistorPin == HIGH); (BtransistorPin == LOW); (CtransistorPin == LOW); (DtransistorPin == HIGH); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgJ);
          sms.endSMS();
        }
        if (AtransistorPin == HIGH); (BtransistorPin == HIGH); (CtransistorPin == HIGH); (DtransistorPin == LOW); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgK);
          sms.endSMS();
        }
        if (AtransistorPin == LOW); (BtransistorPin == LOW); (CtransistorPin == LOW); (DtransistorPin == HIGH); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgL);
          sms.endSMS();
        }
        if (AtransistorPin == LOW); (BtransistorPin == LOW); (CtransistorPin == HIGH); (DtransistorPin == LOW); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgM);
          sms.endSMS();
        }
        if (AtransistorPin == HIGH); (BtransistorPin == HIGH); (CtransistorPin == LOW); (DtransistorPin == HIGH); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgN);
          sms.endSMS();
        }
        if (AtransistorPin == LOW); (BtransistorPin == HIGH); (CtransistorPin == LOW); (DtransistorPin == LOW); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgO);
          sms.endSMS();
        }
        if (AtransistorPin == HIGH); (BtransistorPin == LOW); (CtransistorPin == HIGH); (DtransistorPin == HIGH); {
          sms.beginSMS(remoteNumber);
          sms.print(txtMsgP);
          sms.endSMS();
        }
      }
    }
    delay (500);
    sms.flush();
  }
  delay(1000);
}

This message has 127 characters. Your array could be 128 instead of 200. >>>>

Arduino Remotely controlled Sockets. Status Report as of now: S1-ACTIVE S2-ACTIVE S3-ACTIVE S4-ACTIVE All Sockets are active"

I used this website to count the characters. http://www.lettercount.com/

All messages that have this: "Arduino Remotely controlled Sockets. Status Report as of now:" could be shorten by printing something like this:

Serial.print("Arduino Remotely controlled Sockets. Status Report as of now:");
Serial.print("S1-ACTIVE S2-ACTIVE S3-ACTIVE S4-ACTIVE All Sockets are active");

Where the only part that is in an array is the second print part of the message.

I would probably use switch-case to determine which second message is printed.

You could also put most, or all, of that text into PROGMEM and access it via memcpy_P or other memory utilities or by FlashStringHelper.

dougp:
You could also put most, or all, of that text into PROGMEM and access it via memcpy_P or other memory utilities or by FlashStringHelper.

What characters from the code can be put into PROGMEM, and do you mean for example the entire void loop can be accessed via progmem?

ChristoperRichard:
What characters from the code can be put into PROGMEM

The character arrays can be put in PROGMEM. This can drastically reduce RAM usage. The tradeoff is it then takes special techniques like memcpy_P to access/retrieve them.

ChristoperRichard:
and do you mean for example the entire void loop can be accessed via progmem?

The program you upload, setup() and loop() and certain variables, resides in PROGMEM by default, hence the name.

Here's a sample thread.

I'm only letting you know these things exist. I won't pretend to enough knowledge to confidently guide anyone.

As far as I can see, putting the messages in arrays is a waste of time as you only use them once.

Remove the common text from the messages and put only that in an array of chars in PROGMEM. Print it before each message, then print the specific message. Create the specific message by writing a function to read the state of the outputs and send them.

Use strings (zero terminated arrays of chars) instead of Strings to avoid memory fragmentation.

compose your messages using snprintf...

char outgoingSMS[MAX_MESSAGE_LENGTH + 1]
snprintf(outgoingSMS, sizeof(outgoingSMS), "Arduino Remotely controlled Sockets. Status Report as of now: S1-%sACTIVE S2-%sACTIVE S3-%sACTIVE S4-%sACTIVE", digitalRead(AtransistorPin)? "" : "IN", digitalRead(BtransistorPin)? "" : "IN", digitalRead(CtransistorPin)? "" : "IN", digitalRead(DtransistorPin)? "" : "IN") 

sms.beginSMS(remoteNumber);
sms.print(outgoingSMS);
sms.endSMS();

your entire code which is over 190 lines could be done in less than 40 i'm guessing.

Trivial saving compared to BulldogLowell's but for future reference at least, your pin variables such as AtransistorPin could be byte rather than int.

This is not at all the correct way to express a multiple conditional:

        if (AtransistorPin == HIGH); (BtransistorPin == LOW); (CtransistorPin == LOW); (DtransistorPin == HIGH); {

should be

        if (AtransistorPin == HIGH and BtransistorPin == LOW and CtransistorPin == LOW and DtransistorPin == HIGH)
           {

or

        if (AtransistorPin == HIGH && BtransistorPin == LOW && CtransistorPin == LOW && DtransistorPin == HIGH)
           {

and this subtle error:

    if (received.compareTo("STATUS") == 0) {
        if (AtransistorPin == HIGH); (BtransistorPin == HIGH); (CtransistorPin == HIGH); (DtransistorPin == HIGH); {

what are the odds that any of these are ever false?