Notifications
Clear all

Under new management??

108 Posts
5 Users
9 Likes
24.7 K Views
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

Yeah, on the Island of Dr Moreau, six legged pigs can fly ? ? ? 


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

@pugwash

Posted by: @pugwash

"Pigs Have Flown"

It's OK to to break it up now and then ? 

Cheers, and talk again soon... need some sleep.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  
Posted by: @frogandtoad

@pugwash

Sorry for the delay... here's the code I said I would provide to replace those millis delay's: - I use this in my own little library that I have been creating which looks like this:

A directory in my library folder called: fat_utilities
Within that directory resides the following file:

Arduino code example looks like this:

// Be sure to include our new functions library
#include <fat_utilities.h>

void setup() {
  Serial.begin(9600);  
 }

void loop() {

  // Testing for default delay of 1 second...
  if(fat::delay(MILLIS, 1000)) {
    Serial.println(F("\nMilliseconds delay completed\n"));
   }

  // Both delays run simultainiously @ 1:4 ratio between prints

  // Testing for a custom delay of 0.25 seconds...
  if(fat::delay(MICROS, 250000)) {
    Serial.println(F("Microseconds delay completed"));
   }
   
 }

If you do not wish to use the namespace qualifier, then you can remove them, and add the following namespace qualifier instead:

#include <fat_utilities.h>

using namespace fat; // Example usage

This will now allow you to use the functions as normal:

  // Testing for default delay of 1 second...
  if(delay(MILLIS, 1000)) {
    Serial.println(F("\nMilliseconds delay completed\n"));
   }

  // Both delays run simultainiously @ 1:4 ratio between prints

  // Testing for a custom delay of 0.25 seconds...
  if(delay(MICROS, 250000)) {
    Serial.println(F("Microseconds delay completed"));
   }

 These functions can work together simultaneously:

-- OUTPUT --

Microseconds delay completed
Microseconds delay completed
Microseconds delay completed
Microseconds delay completed

Milliseconds delay completed

Microseconds delay completed
Microseconds delay completed
Microseconds delay completed
Microseconds delay completed

Milliseconds delay completed

Microseconds delay completed
Microseconds delay completed
Microseconds delay completed
Microseconds delay completed

Milliseconds delay completed

[ETC]...

Cheers!

You are going to have to explain a few things here:

  1. Why have you just put all the code in the .h file and not used .h and .cpp as seems to be the standard convention?
  2. Why do you use "namespace" at all?
  3. You confused me slightly by using a "reserved" word for a function similar to that of the standard C++ function. That, to me, seems to be bad practice!

I suppose I could add another namespace in the library file called "thin", also with its own separate thin::delay(MILLIS, 1000) function?


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

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

@pugwash

Posted by: @pugwash

You are going to have to explain a few things here:

  1. Why have you just put all the code in the .h file and not used .h and .cpp as seems to be the standard convention?
  2. Why do you use "namespace" at all?
  3. You confused me slightly by using a "reserved" word for a function similar to that of the standard C++ function. That, to me, seems to be bad practice!

I suppose I could add another namespace in the library file called "thin", also with its own separate thin::delay(MILLIS, 1000) function?

OK... here we go:

1) Easy peasy... it was very late when I took it out, and I simply forgot to do it - Sorry about that! 🙂

2) The namespace answers question 3, and that's exactly what they are for when creating libraries... to allow you to have the same named symbol or reserved identifier without clashing with any other libraries - If you're creating a library, it is recommended to use them... why shouldn't you be allowed to pick a good function name?

Yes, you can add your own (another) namespace too.

Cheers!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

Here we go again ? 

If you're creating a library, it is recommended to use them... why shouldn't you be allowed to pick a good function name?

Because the word "delay" implies to me "Stop the microcontroller!"

And that is why I changed it to "period", as in frequency!


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

@pugwash

Posted by: @pugwash

The fat_utilities.h file needs a rethink!!!

Should we be creating instances??

Hey, excellent pickup... something that I never though about carefully enough!  That's the great thing about posting code here, when others can pick up faults that the creator never considered for the same method!  Good catch, and yes, instances may we be able to resolve this issue... I'll have to revisit it and see what I can come up with... but by all means, do let me know if you think of anything else 🙂

Cheers!


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

@pugwash

Posted by: @pugwash

Here we go again ? 

If you're creating a library, it is recommended to use them... why shouldn't you be allowed to pick a good function name?

Because the word "delay" implies to me "Stop the microcontroller!"

And that is why I changed it to "period", as in frequency!

LOL, OK!

How about "pause" or "wait" ?

🙂


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

How about "pause" or "wait" ?

Mmmm! "Pause", now where have I heard that before?? Back to basic(s) I suppose ? 


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

@pugwash

Um... Intermission doesn't sound bad:

pauses

Although my code often uses the freeze function automatically!

? ? ? 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

I like coffee_break, I think I will use that one ? 

I hadn't thought of using my Thesaurus, until now!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

I have made an attempt at setting up timer instances, and this is what I came up with:

.ino sketch:

#include "myTimers.h"

Timer timer1, timer2;

void setup() {
  // starts the serial monitor interface and shows sketch details
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  //bool area;
  if(timer1.getPause(5000)){
    Serial.println("5 second delay");
  }

  if(timer2.getPause(15000)){
    Serial.println("15 second delay");
  }


}

.h sketch:

#ifndef ABOUT_PROGRAM_INFORMATION_2020
#define ABOUT_PROGRAM_INFORMATION_2020 

#if (ARDUINO >=100)
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

using namespace std;

class Timer
{
  public:
    /* declaring member functions */
    bool getPause(unsigned long _delay_time);
    unsigned long previous_time;
    unsigned long current_time;
    
  private:
    
};

#endif

.cpp sketch

#include "myTimers.h"

bool Timer::getPause(unsigned long delay_time)
{
  current_time = millis();

  if(current_time >= previous_time + delay_time) {
    previous_time = current_time;
    return true;
   }else{
    return false;
    }
}


Go easy on me, this is my first attempt at programming like this in C++ ? 


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

@pugwash

Posted by: @pugwash

Go easy on me, this is my first attempt at programming like this in C++ ? 

OK, it's not a bad attempt, but just a few small issues and some opinion 🙂

1) Looks like you developed your code outside of the Arduino IDE, hence the std namespace lingering there?  In the case that you didn't, you shouldn't use that name, as it is a reserved word for the standard C++ library.

2) All of your data members should be declared private to embrace data hiding [1]

3) It's not wrong, but sometimes when you don't have an associated setPause function, it kind of doesn't gel to have a getPause one... how about just "pause"?

4) You should incorporate and use namespaces... again, this is a recommended practice, and will allow you to use: pw::Timer t1, t2, t3 at the top of your program, and then you can use the names Timer and pause without conflict

5) Since you only have two return states, you don't need an else statement in your member function, just return false at the end of the member function

 bool Timer::pause(unsigned long delay_time) {

   current_time = millis();

   if(current_time >= previous_time + delay_time) {
     previous_time = current_time;
     return true;
    }

   return false;
  }

6) Since this code is no longer about displaying program information, I would choose a better name for the header include guards 🙂

[1] - Data hiding

When we write libraries, we do not want to expose critical data members that should not be modified by any user to the outside world, so making them private makes them not only inaccessible from outside the class, but it also makes them invisible to the user - Good IDE's with intellisense will not show these when private, because classes are all about data hiding and encapsulation.

Let me know if you have any questions... your mission (should you choose to accept it), is to incorporate the MICROS functionality I showed your in my previous function example 🙂

Cheers!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

Incorporating the MILLIS/MICROS option was not too difficult, Currently, the working version looks like this:

.ino file

#include "myTimers.h"

Timer timer1, timer2;

void setup() {
  // starts the serial monitor interface and shows sketch details
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  if(timer1.pause(MILLIS, 5000)){
    Serial.println("5000 milliseconds period");
  }

  if(timer2.pause(MICROS, 2000000)){
    Serial.println("2000000 microseconds period");
  }

}

.h file

#ifndef TWO_TIMERS
#define TWO_TIMERS 

#if (ARDUINO >=100)
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

enum DELAY_MODE{MILLIS, MICROS};

class Timer
  {
  public:
    /* declaring member functions */    
    bool pause(DELAY_MODE delay_mode, unsigned long delay_time);
    
  private:
    /* hidden private variables */
    unsigned long _previous_time;
    unsigned long _current_time;
    
  };

#endif

.cpp file

#include "myTimers.h"

bool Timer::pause(DELAY_MODE delay_mode, unsigned long delay_time){

  if(delay_mode == MILLIS){
    
    _current_time = millis();
  
    if(_current_time >= _previous_time + delay_time) {
      _previous_time = _current_time;
      return true;
     }
    return false;
  }

  if(delay_mode == MICROS){
    
    _current_time = micros();
  
    if(_current_time >= _previous_time + delay_time) {
      _previous_time = _current_time;
      return true;
     }
    return false;
    }
    
}

But namespace combined with the class declaration is still giving me grief during compilation.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

But namespace combined with the class declaration is still giving me grief during compilation.

Or did you mean wrapping the code section in a namespace, like this?

#include "myTimers.h"

namespace tim {

  bool Timer::pause(DELAY_MODE delay_mode, unsigned long delay_time){
  
    if(delay_mode == MILLIS){
      
      _current_time = millis();
    
      if(_current_time >= _previous_time + delay_time) {
        _previous_time = _current_time;
        return true;
       }
      return false;
    }
  
    if(delay_mode == MICROS){
      
      _current_time = micros();
    
      if(_current_time >= _previous_time + delay_time) {
        _previous_time = _current_time;
        return true;
       }
      return false;
    }
      
  }

}

Which does compile without errors!

If YES, what is namespace actually doing?


   
ReplyQuote
Page 6 / 8