Changing response times for high / low circuit

Good Morning all;

I have been working on a piece of code and was looking for some assistance as I am not 100% there yet.

I am using a MKR NB1500 and a MKR Relay Proto board. I am looking 2 things

  1. If I get rid of the delays under the void alarms and use milli's would this mean that all 4 of the alarms would be looking for a change of state at the same time?

  2. Change the SMS response time for void loop alarms. Lets say that after the first notification that there is an alarm state change that someone can either text it to bring it to a non responsive state or to change the response time to a longer duration?

Thank you

#include <MKRNB.h>
#include "arduino_secrets.h" 

NB nbAccess;
NB_SMS sms;

char PINNUMBER [] = SECRET_PINNUMBER;
String sender = SECRET_YOUR_NUMBER;

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
  }

  Serial.println("SMS Messages Receiver");

  // connection state
  bool connected = false;

  // Start LTE connection
  while (!connected) {
    if (nbAccess.begin(PINNUMBER) == NB_READY) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("LTE initialized");
  Serial.println("Waiting for messages");
  pinMode(1,OUTPUT);
  pinMode(0,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(14,OUTPUT);

  pinMode(16,INPUT_PULLUP);
  pinMode(17,INPUT_PULLUP);
  pinMode(18,INPUT_PULLUP);
  pinMode(19,INPUT_PULLUP);

}

void loop_relay()
{
  int c;
  String texmsg = "";
  // If there are any SMSs available()
  if (sms.available()) {
    Serial.println("Message received from: ");

    // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);
    if (String(senderNumber) == sender) {
      // An example of message disposal
      // Any messages starting with # should be discarded
      if (sms.peek() == '#') {
        Serial.println("Discarded SMS");
        sms.flush();
      }
      //read the first char of the message text to witch the respective relays on the shielkd
      c = sms.read();
      switch (c) {
        case 49:
          digitalWrite(1, !digitalRead(1));
          texmsg = "Relay 1, state: " +  String(digitalRead(1));
          sms.beginSMS(senderNumber);         //resposnse to the remote sender with the relay number and the status of the relays
          sms.print(texmsg);
          sms.endSMS();
          break;
        case 50:
          digitalWrite(2, !digitalRead(2));
          texmsg = "Relay 2, state: " +  String(digitalRead(2));
          sms.beginSMS(senderNumber);  //resposnse to the remote sender with the relay number and the status of the relays
          sms.print(texmsg);
          sms.endSMS();
          break;
        default:
          break;
      }

      Serial.println("\nEND OF MESSAGE");

      // Delete message from modem memory
      sms.flush();
      Serial.println("MESSAGE DELETED");
    } else {
      sms.flush();
      Serial.println("MESSAGE DELETED");
    }
  }

  delay(1000);

}

void loop_alarm(){
  int sensorVal = digitalRead(16);
  Serial.println(sensorVal);
  if (sensorVal ==HIGH){
    digitalWrite(11,LOW);
    String texmsg = "Alarm";
          sms.beginSMS(senderNumber);         //resposnse to the remote sender with the relay number and the status of the relays
          sms.print(texmsg);
          sms.endSMS();
          delay(10000);
   }
  else {
    digitalWrite(11, HIGH);
  }
}     
void loop_alarm2(){
  int sensorVal = digitalRead(17);
  Serial.println(sensorVal);
  if (sensorVal ==HIGH){
    digitalWrite(12,LOW);
    String texmsg = "Alarm2";
          sms.beginSMS(senderNumber);         //resposnse to the remote sender with the relay number and the status of the relays
          sms.print(texmsg);
          sms.endSMS();
          delay(10000);
   }
  else {
    digitalWrite(12, HIGH);
  }
} 
void loop_alarm3(){
  int sensorVal = digitalRead(18);
  Serial.println(sensorVal);
  if (sensorVal ==HIGH){
    digitalWrite(13,LOW);
    String texmsg = "Alarm3";
          sms.beginSMS(senderNumber);         //resposnse to the remote sender with the relay number and the status of the relays
          sms.print(texmsg);
          sms.endSMS();
          delay(10000);
   }
  else {
    digitalWrite(13, HIGH);
  }
} 
void loop_alarm4(){
  int sensorVal = digitalRead(19);
  Serial.println(sensorVal);
  if (sensorVal ==HIGH){
    digitalWrite(14,LOW);
    String texmsg = "Alarm4";
          sms.beginSMS(senderNumber);         //resposnse to the remote sender with the relay number and the status of the relays
          sms.print(texmsg);
          sms.endSMS();
          delay(10000);
   }
  else {
    digitalWrite(14, HIGH);
  }
} 
         
void loop(){
  loop_relay();
  loop_alarm();
  loop_alarm2();
  loop_alarm3();
  loop_alarm4();

}

would that be a bad thing?

won't serial msgs get queued and processed when read?

take a look at this version

#undef MyHW
#ifdef MyHW
#define SECRET_PINNUMBER   "1234"
#define SECRET_YOUR_NUMBER "5678"

enum { NB_READY };

struct NB {
    NB (void) { }
    int begin (char *s)  { return 0; }
};

struct NB_SMS {
    NB_SMS (void) { }

    int  available (void)   { return Serial.available (); }
    void beginSMS (char *num) { }
    void endSMS (void)    { }

    char peek (void)    { return '_'; }
    char read (void)    { return Serial.read (); }
    void flush (void)    { }

    void remoteNumber (char *s, int nByte) {
        strncpy (s, SECRET_YOUR_NUMBER, nByte);
    }

    void print (char *s)  { Serial.println (s); }
    void print (String s) { Serial.println (s); }
};

#else
# include <MKRNB.h>
# include "arduino_secrets.h"
#endif

// -----------------------------------------------------------------------------
struct Alarm {
    byte    pinSensor;
    byte    pinOutput;
    String  textmsg;
    unsigned long msecLst;
    byte    sensorLst;
};


#define MyHW
#ifdef MyHW
Alarm alarm [] = {
    { A1, 10, "Alarm" },
    { A2, 11, "Alarm2" },
};

const byte pinRelay [] = { 12, 13 };
enum { Off = HIGH, On = LOW };

#else
Alarm alarm [] = {
    { 16, 11, "Alarm" },
    { 17, 12, "Alarm2" },
    { 18, 13, "Alarm3" },
    { 19, 14, "Alarm4" },
};

const byte pinRelay [] = { 1, 2 };
enum { Off = LOW, On = HIGH };
#endif

#define N_Alarm     (sizeof(alarm)/sizeof(Alarm))

NB nbAccess;
NB_SMS sms;
char PINNUMBER [] = SECRET_PINNUMBER;
String sender     = SECRET_YOUR_NUMBER;
char senderNumber[20];

// -----------------------------------------------------------------------------
void setup ()
{
    // initialize serial communications and wait for port to open:
    Serial.begin (9600);
    while (! Serial)
        ;

    Serial.println ("SMS Messages Receiver");
    // connection state
    bool connected = false;

    // Start LTE connection
    while (!connected) {
        if (nbAccess.begin (PINNUMBER) == NB_READY) {
            connected = true;
        } else {
            Serial.println ("Not connected");
            delay (1000);
        }
    }

    Serial.println ("LTE initialized");
    Serial.println ("Waiting for messages");
    pinMode (1,OUTPUT);
    pinMode (0,OUTPUT);

    for (unsigned n = 0; n < sizeof(pinRelay); n++)  {
        digitalWrite (pinRelay [n], Off);
        pinMode      (pinRelay [n], OUTPUT);
    }

    Alarm *p = alarm;
    for (unsigned n = 0; n < N_Alarm; n++, p++)  {
        pinMode (p->pinOutput, OUTPUT);
        pinMode (p->pinSensor, INPUT_PULLUP);
    }
}

// -----------------------------------------------------------------------------
void
relayTgl (
    int relayIdx )
{
    byte pin = pinRelay [relayIdx];

    digitalWrite (pin, !digitalRead (pin));

    String texmsg = "Relay " + String(relayIdx)
            + ", state: " +  String (digitalRead (pin));
    sms.beginSMS (senderNumber);         //resposnse to the remote sender with the relay number and the status of the relays
    sms.print (texmsg);
    sms.endSMS ();
}

// -----------------------------------------------------------------------------
void loop_relay ()
{
    if (sms.available ()) {
        Serial.print ("Message received from: ");
        // Get remote number
        sms.remoteNumber (senderNumber, 20);
        Serial.println (senderNumber);

        if (String (senderNumber) == sender) {
            // An example of message disposal
            // Any messages starting with # should be discarded
            if (sms.peek () == '#') {
                Serial.println ("Discarded SMS");
                sms.flush ();
            }
            //read the first char of the message text to witch the respective relays on the shielkd

            switch (sms.read ()) {
            case 49:
                relayTgl (0);
                break;

            case 50:
                relayTgl (1);
                break;

            default:
                break;
            }

            Serial.println ("END OF MESSAGE");
        }

        // Delete message from modem memory
        sms.flush ();
        Serial.println ("MESSAGE DELETED");
    }
}

// -----------------------------------------------------------------------------

// -------------------------------------
void
alarmChk (
    Alarm  *p )
{
    int sensorVal = digitalRead (p->pinSensor);
#ifdef Debug
    Serial.println (sensorVal);
#endif

    if (p->sensorLst != sensorVal)  {
        p->sensorLst = sensorVal;

#define MyHW
#ifdef MyHW
        if (sensorVal == LOW) {
#else
        if (sensorVal == HIGH){
#endif

            digitalWrite (p->pinOutput,LOW);
            sms.beginSMS (senderNumber);
            //resposnse with relay number and the status of the relays
            sms.print (p->textmsg);
            sms.endSMS ();
        }
        else {
            digitalWrite (p->pinOutput, HIGH);
        }
    }
}

// -----------------------------------------------------------------------------

void loop (){
    loop_relay ();

    Alarm *p = alarm;
    for (unsigned n = 0; n < N_Alarm; n++, p++)  {
        alarmChk (p);
    }
}
1 Like

True.

I started to test your code and after initial testing I do like how the alarm 1 and 2 are functioning.

But;

I have not had luck with the Relays, I will have to wait until tomorrow and hopefully a clearer mind will allow me to realize the SMS string to send.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.