recording "on time" with millis

As distinct from say digitalWrite(5) which does have a parameter, namely the pin number

Not the best example because digitalWrite takes two parameters but that's the idea :slight_smile:

Note I made a correction in the code above. (baud needs to be 115200 and forgot to check if button was not pressed when initializing the startTime)

if you want to test if the button is pressed for too long you can do that as well, here I check if you keep it pressed for more than 5 secs and if so the code will bark at you

(you'll get to see all the bouncing possibly of the button)

const byte buttonPin = 2;
unsigned long startTime, stopTime, totalTime; // are initialized to 0 by compiler
boolean buttonPressed = false;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {

  if ((buttonPressed == false) && (digitalRead(buttonPin) == LOW)) {
    // button is pressed
    buttonPressed = true;
    startTime = millis();

    Serial.print("start recording at ");
    Serial.println(startTime);

  } else { // button is not pressed, checking if this is a transition
    if (buttonPressed) { // button was pressed
      // button is released
      buttonPressed = false;
      stopTime = millis();
      totalTime += stopTime - startTime;

      Serial.print("stoped recording at ");
      Serial.print(stopTime);
      Serial.print(", duration was ");
      Serial.println(stopTime - startTime);

      Serial.print("Total cumulated pressed time is ");
      Serial.print(totalTime);
    }
  }

  if (buttonPressed) {
    if (millis() - startTime >= 5000ul) { // print this repetitively if more than 5 sec
      Serial.print ("Button is being pressed for ");
      Serial.print (millis() - startTime);
      Serial.println("ms, that's a long time...");
    }
  }
}

kenwood120s:
Nobody wants to chase you away....

You have to realise that millis, Millis and millis() are not the same thing.

millis with no () is a variable, and if you want a variable called millis you can do so, but it's confusing and you would have had to have declared it. That's why I asked if it compiled: if you have an undeclared variable millis, it won't compile.

And if you mean to get the value returned by the builtin function millis() with the (), then you need to say millis() not millis.

But when you post code snippets are you copying from your program or retyping?

To answer your very last post, millis() the function doesn't have parameters, it's a function that just return the current time since you fired up. (As distinct from say digitalWrite(5) which does have a parameter, namely the pin number.)

that is weird because it does compile like this even when millis was declared Milllis

#include "DHT.h"
#define tempsensor 10
#define DHTTYPE DHT22

DHT sensor(tempsensor, DHTTYPE);

const int relay = 7;
const int relay2 = 8;

int relayState = 0;
int relayPrevState = 0;

unsigned long relaycurrent = 0;
unsigned long relayprevious = 0;
unsigned long relay2oninterval = 8777UL;
unsigned long relay2offinterval = "untill relay has been on a total of 30 throughout its on off times";
unsigned long interval = 0;
unsigned long interval2 = 0;
unsigned long timeelapsed = 0;



unsigned long autocontrol = 1;


void setup() {
  pinMode (relay, OUTPUT);
  pinMode (relay2, OUTPUT);
  digitalWrite(relay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(relay2, HIGH);

  Serial.begin(9600);
  Serial.println("2=control settting");
  interval = relay2oninterval;
  interval2 = relay2offinterval;

}

void loop() {


  unsigned long Milllis = millis;

  float f1 = sensor.readTemperature(true);
  float h1 = sensor.readHumidity(true);
  if (isnan(h1) || isnan(f1)) {
    return;
  }


  relayState = digitalRead(relay);

  if (autocontrol == 1 )
  {
   
      if ( h1 > 85.00 )
      {
        digitalWrite(relay, LOW);
      relaycurrent = millis();
      Serial.println(millis());
      {
      if ( h1 < 81.00 );
      relayprevious = millis;
      digitalWrite(relay, HIGH);
      Serial.println(millis());
    }
  }
  }
}

J-M-L:
Not the best example because digitalWrite takes two parameters but that's the idea :slight_smile:

Note I made a correction in the code above. (baud needs to be 115200 and forgot to check if button was not pressed when initializing the startTime)

if you want to test if the button is pressed for too long you can do that as well, here I check if you keep it pressed for more than 5 secs and if so the code will bark at you

(you'll get to see all the bouncing possibly of the button)

const byte buttonPin = 2;

unsigned long startTime, stopTime, totalTime; // are initialized to 0 by compiler
boolean buttonPressed = false;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {

if ((buttonPressed == false) && (digitalRead(buttonPin) == LOW)) {
    // button is pressed
    buttonPressed = true;
    startTime = millis();

Serial.print("start recording at ");
    Serial.println(startTime);

} else { // button is not pressed, checking if this is a transition
    if (buttonPressed) { // button was pressed
      // button is released
      buttonPressed = false;
      stopTime = millis();
      totalTime += stopTime - startTime;

Serial.print("stoped recording at ");
      Serial.print(stopTime);
      Serial.print(", duration was ");
      Serial.println(stopTime - startTime);

Serial.print("Total cumulated pressed time is ");
      Serial.print(totalTime);
    }
  }

if (buttonPressed) {
    if (millis() - startTime >= 5000ul) { // print this repetitively if more than 5 sec
      Serial.print ("Button is being pressed for ");
      Serial.print (millis() - startTime);
      Serial.println("ms, that's a long time...");
    }
  }
}

i was thinking too hard and i never thought about putting it in the loop like this. im going to modify this into my code and rename everything etc, and i think this is really going to help get me close to what i need. thank you !

that's because you are ignoring the compiler warning that tells you that you store the address of the function millis() into your unsigned long....

[color=orange]warning: invalid conversion from 'long unsigned int (*)()' to 'long unsigned int' [-fpermissive]
   unsigned long Milllis = millis;
                           ^[/color]

That is because the name of a function by itself has a valid interpretation in C++. But it is not the one you need. You can't depend on the compiler to understand your intentions. It can only reject content that is not a valid part of the language.

It is not "weird". It is part of the definition of the language. You need to learn the language.

J-M-L:
that's because you are ignoring the compiler warning that tells you that you store the address of the function millis() into your unsigned long....

[color=orange]warning: invalid conversion from 'long unsigned int (*)()' to 'long unsigned int' [-fpermissive]

unsigned long Milllis = millis;
                          ^[/color]

i didn't know there was an error mine just said,,Sketch uses 4500 bytes (13%) of program storage space. Maximum is 32256 bytes.
Global variables use 312 bytes (15%) of dynamic memory, leaving 1736 bytes for local variables. Maximum is 2048 bytes..

so this, unsigned long Milllis = millis; wont work? does it need to be unsigned long Milllis = millis();?

notsolowki:
so this, unsigned long Milllis = millis; wont work? does it need to be unsigned long Milllis = millis();?

Does the documentation not say so? Does every sketch using millis() not do it this way? >:(

so this, unsigned long Milllis = millis; wont work?

well it will work... but it won't probably do what you want :slight_smile:

it's like you are traveling and you want to ask someone what time it is but because you don't speak their language very well, you ask "where is the shop selling watches?" instead of "what time is it?"... so they give you the street address of the shop selling watches... you got the information you asked for, but this was not really your intent, right.... ?

does it need to be unsigned long Milllis = millis();?

yes if you want to store in a variable weirdly called Mi[color=red]lll[/color]is with 3 l the number of milliseconds elapsed since the last reset of your arduino (the number of milliseconds since the Arduino board began running the current program)

aarg:
Does the documentation not say so? Does every sketch using millis() not do it this way? >:(

well now ill always know, but tbh the millis(); documentation dont say much. im trying to learn as i go. i got to keep doing the simple things i'm doing now until and hopefully learn as much as i can

notsolowki:
well now ill always know, but tbh the millis(); documentation dont say much. im trying to learn as i go.

...but "as you go" should be "as you learn by following working examples and consulting tutorials and documentation" rather than "by guessing" and "trying to figure it out from compiler errors" and "by asking about each and every failed attempt you make on an internet forum"

J-M-L:
You wasted ours then....

Last try... would this help you understand the concept:

const byte buttonPin = 2;

unsigned long startTime, stopTime, totalTime; // are initialized to 0 by compiler
boolean buttonPressed = false;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  if ((buttonPressed == false) && (digitalRead(buttonPin) == LOW)) {
    // button is pressed
    buttonPressed = true;
    startTime = millis();

Serial.print("start recording at ");
    Serial.println(startTime);

} else { // button is not pressed, checking if this is a transition
    if (buttonPressed) { // button was pressed
      // button is released
      buttonPressed = false;
      stopTime = millis();
      totalTime += stopTime - startTime;

Serial.print("stoped recording at ");
      Serial.print(stopTime);
      Serial.print(", duration was ");
      Serial.println(stopTime - startTime);

Serial.print("Total cumulated pressed time is ");
      Serial.print(totalTime);
    }
  }
}

i dont know if its because its printing it in the serial screen or what, but one thing i notice about this code is the time is really slow. it says 3 seconds when i hold the button for 6?

notsolowki:
the millis(); documentation dont say much.

That's because there's nothing to say about millis() the function: it tells you how long it is since the last power-up or reset and that's it.

It's from sketches like BlinkWithoutDelay that actually use millis() that you'll actually learn some techniques.

J-M-L:
Not the best example because digitalWrite takes two parameters but that's the idea :slight_smile:

Note I made a correction in the code above. (baud needs to be 115200 and forgot to check if button was not pressed when initializing the startTime)

if you want to test if the button is pressed for too long you can do that as well, here I check if you keep it pressed for more than 5 secs and if so the code will bark at you

(you'll get to see all the bouncing possibly of the button)

const byte buttonPin = 2;

unsigned long startTime, stopTime, totalTime; // are initialized to 0 by compiler
boolean buttonPressed = false;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {

if ((buttonPressed == false) && (digitalRead(buttonPin) == LOW)) {
    // button is pressed
    buttonPressed = true;
    startTime = millis();

Serial.print("start recording at ");
    Serial.println(startTime);

} else { // button is not pressed, checking if this is a transition
    if (buttonPressed) { // button was pressed
      // button is released
      buttonPressed = false;
      stopTime = millis();
      totalTime += stopTime - startTime;

Serial.print("stoped recording at ");
      Serial.print(stopTime);
      Serial.print(", duration was ");
      Serial.println(stopTime - startTime);

Serial.print("Total cumulated pressed time is ");
      Serial.print(totalTime);
    }
  }

if (buttonPressed) {
    if (millis() - startTime >= 5000ul) { // print this repetitively if more than 5 sec
      Serial.print ("Button is being pressed for ");
      Serial.print (millis() - startTime);
      Serial.println("ms, that's a long time...");
    }
  }
}

there is something wrong with this code it prints everything all the time. that's why there is such a huge delay in the code?

@ J-M-L, please fix you script

or not whatever

Looks like this Thread is also heading towards 82 Replies. :slight_smile:

...R

post #100 is mine.

Edit - guess it won't get that far.

notsolowki:
@ J-M-L, please fix you script

Wow. Just wow.

Please wire your button correctly

the button is wired correctly. you code just keeps repeating everything in the loop. there IS somthing wrong with the code. im going to assume its the brackets but i dont know. maybe right here shouldn't this be button not pressed.."

 } else { // button is not pressed, checking if this is a transition
    if (buttonPressed) { // button was pressed
      // button is released

im not sure. i think its a problem with buttonPressed state.