Multiple Timer Library  

Page 3 / 3
  RSS

frogandtoad
(@frogandtoad)
Reputable Member
Joined: 9 months ago
Posts: 384
2020-02-22 1:51 pm  

@pugwash

Posted by: @pugwash

What goes up, must come down ?

Have finally joined the dots! The Alan Parsons Project is the answer!

That's eye in the sky stuff!

class Timer
 {
  private:
     double Start;
  public:
     Timer() : Start(millis()) {}
     ~Timer() {
       Serial.println(String(double((millis() - Start) / 1000), 3) + " seconds");
      }
 };
 
void setup() {
  Serial.begin(9600);
 }

void loop() {
  
    {
     Timer T; //Timing operation starts here
     delay(1000);
    } //Timing operation ends here
    
  }

Welcome to the workshop. we'll be scoping today! ? 

I need some sleep, cheers!


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-24 10:48 am  
Posted by: @frogandtoad

@pugwash

Posted by: @pugwash

What goes up, must come down ?

Have finally joined the dots! The Alan Parsons Project is the answer!

That's eye in the sky stuff!

class Timer
 {
  private:
     double Start;
  public:
     Timer() : Start(millis()) {}
     ~Timer() {
       Serial.println(String(double((millis() - Start) / 1000), 3) + " seconds");
      }
 };
 
void setup() {
  Serial.begin(9600);
 }

void loop() {
  
    {
     Timer T; //Timing operation starts here
     delay(1000);
    } //Timing operation ends here
    
  }

Welcome to the workshop. we'll be scoping today! ? 

I need some sleep, cheers!

I ran the above code but I am still confused. In the loop, an instance of Timer is created "T" and I assume calls itself. My guess is, that is what the tilde is for. Or you could explain if my assumption is wrong.

Although I haven't got any particular use for the following, I was wondering whether it is possible to create an instance of a class on the fly, use it for whatever purpose it is needed, and then destroy the instance thus freeing up the RAM it was occupying???

SteveC - Topple Rudd Poltman


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 9 months ago
Posts: 384
2020-02-24 11:23 am  

@pugwash

Posted by: @pugwash

I ran the above code but I am still confused. In the loop, an instance of Timer is created "T" and I assume calls itself. My guess is, that is what the tilde is for. Or you could explain if my assumption is wrong.

Not quite... it's the loop that creates an instance, and that instance is destroyed when the object goes out of scope by the braces I added - This is how we can use scope to our advantage to automate an action.  The next time the loop comes around, a new instance is created.

The tilde in the class is how you declare a user defined destructor, and what differentiates its signature from a default constructor.

Posted by: @pugwash

Although I haven't got any particular use for the following, I was wondering whether it is possible to create an instance of a class on the fly, use it for whatever purpose it is needed, and then destroy the instance thus freeing up the RAM it was occupying???

I think my answer above addresses exactly this question, because that's exactly what's happening! 🙂

Cheers!


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-24 11:32 am  

@frogandtoad

Timer() : Start(millis()) {}
     ~Timer() {
       Serial.println(String(double((millis() - Start) / 1000), 3) + " seconds");
      }

So in plain English, this means:

  1. Invoke the class
  2. Fill the variable "Start" with the current uptime in milliseconds
  3. Destroy the instance after printing the output to the monitor

SteveC - Topple Rudd Poltman


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 9 months ago
Posts: 384
2020-02-24 11:39 am  

@pugwash

Posted by: @pugwash
Timer() : Start(millis()) {}
     ~Timer() {
       Serial.println(String(double((millis() - Start) / 1000), 3) + " seconds");
      }

So in plain English, this means:

  1. Invoke the class
  2. Fill the variable "Start" with the current uptime in milliseconds
  3. Destroy the instance after printing the output to the monitor

Yes, pretty much how it works!
The destructor is ALWAYS called for an object automatically, when the object goes out of scope, or there is an exception and the stack unwinds.

{ // Introduce a new scope

Timer T; // Create an instance within this scope

} // End of new scope - Object instance destroyed here
  // Destructor automatically invoked to clean up its memory footprint and any other cleanup (database connection, etc...)

Cheers!

This post was modified 1 month ago 2 times by frogandtoad

ReplyQuote
Page 3 / 3

Please Login or Register