"if nothing happens for 5 mins, do this" function

Hi all

This is for a rain gauge project..

Ive got a float variable counter that counts nicely. I want to add a timer to it so that if there are no switches for 5mins then send the value and an SMS and reset the counter.

Obviously i dont want to use a delay because it will not be able to count if there is another switch.

I tried using stuff out of the BlinkWithoutDelay example but it has just seemed to work in the same way as a delay because it wont count once it gets to that part. But im probably doing it wrong...

Do I need to put a timer library in for something this simple?
im just really confused about this one!!

#include <stdlib.h>
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
SMSGSM sms;
                                        // this constant won't change:
const int  buttonPin = 4;               // the pin that the pushbutton is attached to
const int ledPin = 13;                  // the pin that the LED is attached to
                                // Variables will change:
int buttonState = 0;                     // current state of the button
int lastButtonState = 0;                 // previous state of the button
float myval;                             //0.2mm multiple
int numdata;
boolean started=false;

char str_rainfall[6];
char rainfall[10];
char smsbuffer[160];
char n[20];
char sms_position;
char phone_number[20];                  // array for the phone number string
char sms_text[100];
int i;

long interval = 100000; 

void setup() 
{ 
pinMode(buttonPin, INPUT);              // initialize the button pin as a input:
pinMode(ledPin, OUTPUT);                // initialize the LED as an output:
Serial.begin(9600);                    // initialize serial communication:
Serial.println ("Arduino on");
if (gsm.begin(4800));
Serial.print ("GSM on");
//if (sms.SendSMS("+27630812488", "power on"));
Serial.print("\nSMS sent OK");
Serial.println("\nGauge ready");
}

void loop() 
{
char txtMsg[200];
buttonState = digitalRead(buttonPin);                       // read the pushbutton input pin:
if (buttonState != lastButtonState)                         // compare the buttonState to its previous state
  {
  if (buttonState == HIGH)                                  // if the state has changed, increment the counter, if the current state is high then the circuit is open
    {
    myval += 0.2;
    Serial.print("Rainfall:    ");
    Serial.print (myval);
    Serial.println("mm");    
    }
    else
    {
    delay(1000);//1 second inbetween tips
    }
 if (buttonState == LOW && myval !=0.0)
{
//interval;   
dtostrf(myval, 4, 2, str_rainfall);
sprintf(rainfall,"%smm rainfall", str_rainfall);
if(sms.SendSMS("+27630812488", rainfall));
myval = 0;
 } 
  }
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}

Can you post your version of code that tries to use millis instead of delay, and tries to send the message after 5 minutes? I think it'll be easier to get that working, rather than starting from the code you have here.

Oh, and use the autoformatter before posting code to make it easier for us to read.

Show us your attempt at using the BWD technique..

.

Every time "something" happens, set a global variable of unsigned long type to millis():

lastSomethingHappen=millis();

in loop, check whether enough time has passed since that, and in that case do something:

if (lastSomethingHappen + 300000 < millis()) {
doSomething();
//if doSomething() doesn't reset the timekeeping variable, you have to do that here, otherwise it will repeatedly do every time loop runs
}

DrAzzy:
if (lastSomethingHappen + 300000 < millis()) {

ALWAYS use subtraction with millis() so you don't have a problem when it rolls over

if (millis() - lastSomethingHappen >= 300000UL) {

...R

stuwilson:
I want to add a timer to it so that if there are no switches for 5mins then send the value

To do that you should record the value of millis() every time there is a count - effectively resetting the clock. Like this pseudo code

if there is a count
   lastCountMillis() = millis()
   // other stuff
}
if (millis() - lastCountMillis >= interval) {
   // time has run out - send stuff
}

...R