Notifications
Clear all

Multiple Timer Library

35 Posts
2 Users
1 Likes
6,433 Views
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  
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???


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@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

   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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!


   
ReplyQuote
Page 3 / 3