How to "hold" a variable

How do I "hold" a variable until It changes to one of my other variable IF options?
I have an arduino gsm shield(official) I want to be able to send it a message 1(on) or 2(off) to simply turn a light on or off When I sent the number 1 the light goes on for a short while and then it goes off again because the message gets erased but when I remove the erase feature the light just stays on when I send 2. I Probobly made some stuff that doesn't make sense since i'm still new to arduino.

Here is my program I have no idea what to do:

#include <GSM.h>

// PIN Number for the SIM
#define PINNUMBER "(my pin number)"
#define led 13

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char senderNumber[20];

void setup() {
  // 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
  }
   pinMode(led,OUTPUT);
  Serial.println("SMS Messages Receiver");

  // connection state
  boolean notConnected = true;

  // Start GSM connection
  while (notConnected) {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
      notConnected = false;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop() {
  char c;


  if (sms.available()) {
    Serial.println("Message received from:");

    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);
    // Any messages starting with # should be discarded
    if (sms.peek() == '#') {
      Serial.println("Discarded SMS");
      sms.flush();
    }

    // Read message bytes and print them

      while(c = sms.read())
      Serial.println(c);   
       if (c == 1){
        digitalWrite(led,HIGH);
         Serial.println("received command");
      }
       if (c == 2){
        digitalWrite(led,LOW);
         Serial.println("received command2");
       }
       delay(5000);
    Serial.println("\nEND OF MESSAGE");
    sms.flush();//delete message
    Serial.println("MESSAGE DELETED");
  }
  
  delay(3000);
}

Please help

  char c;This resets the value of c each time through loop(). Because c is a local variable its value cannot be predicted.

Change the declaration to
static char c;and once its value has been set it won't change unless you explicitly change it. Note that you only do anything with the value of c when a new message is received so does it actually matter if it is reset to another value ?

Will definitely try thank you!

The IF doesn't seem to be true it passes by the IF statement. should I rather use 1, '1' or "1" or what else?

Thank you soooo much it works perfecly now, now I just need to figure this out /|
|

If you are typing in '1' on the keyboard, then your code should be looking for the character '1'.

If you are sending the binary number 1 then the code should be looking for the byte 1.

    // Read message bytes and print them

while(c = sms.read())
          Serial.println(c); 
    if (c == 1){
    // should not come here too often ?
    }

Not sure when sms.read() does return a binary 0, but that's the only case when thewhileends.
When you're sure c == 0 after the while, anif does not make sense.

I doubt this is your intention.
( Do we miss some brackets ? )

If something works perfectly, then that's probably not the code you posted :wink:

michael_x:
Not sure when sms.read() does return a binary 0, but that's the only case when thewhileends.
When you're sure c == 0 after the while, anif does not make sense.

I doubt this is your intention.
( Do we miss some brackets ? )

If something works perfectly, then that's probably not the code you posted :wink:

Thanks! missed that now it works 100%

#include <GSM.h>

// PIN Number for the SIM
#define PINNUMBER "MY PIN :)"
#define led 13

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char senderNumber[20];
char x;

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  x = 'k';
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
   pinMode(led,OUTPUT);
  Serial.println("SMS Messages Receiver");

  // connection state
  boolean notConnected = true;

  // Start GSM connection
  while (notConnected) {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
      notConnected = false;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop() {
static char c;
//        Serial.println("1");

//Serial.println(x);
  if (sms.available()) {
    Serial.println("Message received from:");

    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);
    // Any messages starting with # should be discarded
    if (sms.peek() == '#') {
      Serial.println("Discarded SMS");
      sms.flush();
    }

    // Read message bytes and print them
        Serial.println("by sms");
c = sms.read();
      Serial.println(c);
//        Serial.println(c.length());
        Serial.println(x);
//          Serial.print;(x.length());
        delay(2000);
       if (c == x){
        Serial.println("1a);
        delay(1000);
        digitalWrite(led,HIGH);
         Serial.println("received command");
                 Serial.println("2");
      }
       if (c == '2'){
                Serial.println("1b");
        digitalWrite(led,LOW);
         Serial.println("received command2");
         
       }
       delay(5000);
    Serial.println("/nEND OF MESSAGE");
    sms.flush();//delete message
    Serial.println("MESSAGE DELETED");
            Serial.print("3");
  }
  
  delay(3000);
}