millis timer not working for rain gauge

Hi there

I simply want the following to happen...

if this event happens start a new timer each time, once that time reaches say 1 minute then do this...

i.e. once I have recorded the rainfall text it to me.

But all the different millis codes ive been trying do not work at all they either get stuck half way down my code or they reach the interval time straight away

also it tells me I havn't defined currentMillis which I undersand but the blink without delay sketch compiles on its own fine?!

#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 previousMillis = 0;
long interval = 1000; 

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");  
    unsigned long currentMillis = millis();
    }
    else
    {
    delay(1000);//1 second inbetween tips
    }
 if(currentMillis - previousMillis > interval)
 {   
dtostrf(myval, 4, 2, str_rainfall);
sprintf(rainfall,"%smm rainfall", str_rainfall);
//if(sms.SendSMS("+27630812488", rainfall));
myval = 0;
Serial.println("SMS");
 } 
 else
 {
   delay(10);
 }
  }
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}

So where do you set previousMillis?

I assume you should be setting this somewhere and its causing you some grief.

The function millis() returns an "unsigned long". Please use "unsigned long" for previousMillis. That will avoid the rollover problem. Setting the currentMillis does not start the delay. It sets a value into currentMillis, that's all. You need a flag to know that the delay is busy. The 'interval' could be an integer, but it is safer to make that also an "unsigned long".

In the menu is a auto-text-format function. Could you try that ? The code is hard to read.

unsigned long previousMillis = 0UL;         // 'UL' means "unsigned long"
const unsigned long interval = 1000UL;
boolean flagDelay = false;

...

  // start the delay
  previousMillis = millis();        // timestamp this moment
  flagDelay = true;                  // the delay is active now

...
  
  // check every time the loop() runs for the delay
  if( flagDelay)
  {
    if (millis() - previousMillis >= interval)
    {
      // Reached the end of the delay.
      flagDelay = false;         // stop the delay
      
      // send sms
      ...
    }
  }

Ok I'll try that one.

I just don't understand why my one doesn't compile because of currentMillis.

But it compiles in the blinkwithoutdelay sketch?

Is it because when I use mine it is within an if statement?

stuwilson:
Ok I'll try that one.

I just don't understand why my one doesn't compile because of currentMillis.

But it compiles in the blinkwithoutdelay sketch?

Is it because when I use mine it is within an if statement?

No. It is because the blinkwithoutdelay sketch defines currentMillis, and you do not.

This is the first line of the loop() funtion in blinkwithoutdelay

  unsigned long currentMillis = millis();

The function millis() can be used to have a steady interval. I call that a software timer. There are two options with that: do everything at a fixed pace regardless the delay of the sketch or move the interval further in time if the sketch has a long delay.

The function millis() can also be used to do something after a delay. Because this is not just a software timer, a boolean flag has to indicate when the delay is active.

You had mixed that in a way that it would not work. Yes, using it inside the if-statement is one problem.
When you have used my code, please show the full sketch.