How to make time buffer or time sensitive loop?

I’ve written some code to send a text when a PIR sensor is triggered.

Problem is it keeps spamming me with texts even with a delay, obv. My original code was great until it stopped working and I don’t have time to re-write it all, a minimalist version will have to do.

I’ve attempted one at the bottom but am really stuck, just need perhaps a function to not run a different function if it has run in the past x seconds, etc…

Here is my code so far.

(Excuse the mess, and comments lol…)

#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
SMSGSM sms;
boolean started=false;





const unsigned long buffertime = 60000;
unsigned long lastsms = 0;

int inputPin = 2; 
int val = 0;
int pirState = LOW;  

#define LED 13

void setup() {
//  pinMode(ledPin, OUTPUT);      // declare LED as output
pinMode(inputPin, INPUT);     // declare sensor as input
//  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    delay(3000);
    Serial.println("Text");
    sendTextMessage();
    
        
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
      if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}



//
//
//
//{
//
//  int i = 1;
//  delay(5000);
//  while(x <= 5){
//    digitalWrite(LED, HIGH);
//    delay(1000);
//    digitalWrite(LED, LOW);
//    delay(250);   
//  x = x + 1;
//  
//  }
//
//
//

///////////////////////////////////////////////////////////////////////////
////
////  pinMode(LED, OUTPUT);
////  int x = 1;
////  delay(5000);
////  while(x <= 5){
////    digitalWrite(LED, HIGH);
////    delay(1000);
////    digitalWrite(LED, LOW);
////    delay(250);   
////  x = x + 1;
////  }
//
//////////////////////////////////////////////////////////////////////////


//
//
//
//
//void startLed()
//{
//
//  pinMode(LED, OUTPUT);
//  int x = 1;
//  delay(5000);
//  while(x <= 5){
//    digitalWrite(LED, HIGH);
//    delay(1000);
//    digitalWrite(LED, LOW);
//    delay(250);   
//  x = x + 1;
//  }
//  
//}
//
//
//
void startTextMessage()  
{  




  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  if (gsm.begin(2400)){
    Serial.println("\nstatus=READY");
    started=true;  
  }
  else Serial.println("\nstatus=IDLE");
  
  if(started){
    //Enable this two lines if you want to send an SMS.
    if (sms.SendSMS("07503235931", "Arduino SMS"))
      Serial.println("\nSMS sent OK");
  }




  
}

 
void sendTextMessage()
{
  boolean send_sms = false;

  if (last_sms_time > 0 && millis() < last_sms_time + MIN_MS_BETWEEN_SMS)
  {
    Serial.println("SMS blocked since we already sent one recently.");
  }
    send_sms = true;
    
//  }
  
//  
  
  if (send_sms)
  {
    startTextMessage();
    //cellularSerial.print(message);

    
    last_sms_time = millis();
    Serial.print("Actually sent the following SMS to ");
  } else
  {
    Serial.print("Didn't actually send this SMS to ");
  }
}

The demo Several Things at a Time illsutrates how to use millis() to manage timing.

I can’t make sense of your code.

What affects the value of inputPin? It looks like it should send a message whenever that is HIGH.

I don’t know what pirState is intended to represent because it does not seem to be changed by any external device.

…R

Yeah, it’s not all of the code, like I said it’s something I put together quickly and needs to be like the original. And to be cleaned up.

It’s a pretty confusing tutorial tbh.

So in terms of this part of my code.

void startTextMessage()  
{  




  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  if (gsm.begin(2400)){
    Serial.println("\nstatus=READY");
    started=true;  
  }
  else Serial.println("\nstatus=IDLE");
  
  if(started){
    //Enable this two lines if you want to send an SMS.
    if (sms.SendSMS("07503235931", "Arduino SMS"))
      Serial.println("\nSMS sent OK");
  }




  
}

 
void sendTextMessage()
{
  boolean send_sms = false;

  if (lastsms > 0 && millis() < lastsms + MIN_MS_BETWEEN_SMS)
  {
    Serial.println("SMS blocked since we already sent one recently.");
  }
    send_sms = true;
    
//  }
  
//  
  
  if (send_sms)
  {
    startTextMessage();
    //cellularSerial.print(message);

    
    last_sms_time = millis();
    Serial.print("Actually sent the following SMS to ");
  } else
  {
    Serial.print("Didn't actually send this SMS to ");
  }
}

What do I do?

I’m just confused as to how I change mine to work…

StreetTripple: Yeah, it's not all of the code, like I said it's something I put together quickly and needs to be like the original. And to be cleaned up.

Needs to be like the original what? If you mean your original code, then post it so we can see it. You did say it was "great".

You here to help or?

... I said It wasn't working because I didn't want to say I perm deleted it by accident...

So now I have to re-write it... So get off your high horse.

Problem is it keeps spamming me with texts

ROLF!

Mark

StreetTripple: You here to help or?

... I said It wasn't working because I didn't want to say I perm deleted it by accident...

So now I have to re-write it... So get off your high horse.

So basically, you lied.

That horse

() is really dead. () has already bolted. () is a different colour. () was a gift. () can reach the other side of the stream perfectly well. () won't drink. () is not a unicycle. () is a horse, of course, of course. () is wooden. (*) is not really very high

Right, help or leave maybe?

I personally find it offending that you don't have the decency to clean your code out; it shows no respect for people that are willing to try to help.

But OK, I assume that you only want to call sendTextMessage once if val equals high. Something like below (not tested) might do the trick.

void loop(){
  static int oldval = LOW;
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    delay(3000);
    Serial.println("Text");
    if(val != oldval)
    {
      sendTextMessage();
      oldval = val;
    }

Thanks and I'll offer my apologies.

It may work, I didn't want to clean my code as I still had a lot to add.

Thanks a lot.