Help writing and debugging a simple class

I'm novice at programming, so please forgive the basic question and poor skills I'm about to show here!

I'm trying to make my code a little easier to understand by writing classes. I've watched several videos and followed lots of tutorials, but when I write my own classes I only get error messages. I've tried to write the class below as an example, and I'm hoping someone can help me get it working so I can apply it to my project.

The class below should do three things.

  1. Return the time (in micros) of right now
  2. Store some arbitrary time that I pass to it so I can reference it later
  3. Run a function so that I can subtract one time against another and return the answer.

This is what I've come up with, but I can't get it to work. Can someone help me debug it and get it working? Be gentle - as I say I'm novice and ultimately don't really know what I'm doing.

//=======Create Classes================
class timeControl {
  public:
    void getTime {
      long getTime = micros(); //1) Return the time (in micros) of right now
    };
    long storeSomeTime(long whatTime) {
      return whatTime;  //2) Store some arbitrary time that I pass to it so I can reference it later
    }
    long timeDifference(long startTime1,long endTime2) {
      return endTime  - startTime; //3) A function that subtracts one time against another and returns the answer.
    }
};

//=======Create Objects================
MotorTimes timeControl;


void setup() {
  
void loop() {
    Serial.println(MotorTimes.getTime);   //print the time in micros right now using the getTime function
    MotorTimes.storeSomeTime(micros()); //store the time right now in a variable within the class/ object MotorTimes.storeSomeTime
    delay(1000); //wait a second
    Serial.println(MotorTimes.timeDifference(MotorTimes.storeSomeTime, micros())); //use the function 'timeDifference' in the class to calculate the difference between now and the stored time 
    
  }

You've got an immediate scope problem with the variable getTime.
It's only visible in the function getTime.
getTime (function) should be of type "unsigned long" and should simply return the value returned by micros()

This is not calling getTime.

You need some class member variables (unsigned long) to store times.
They should be private.

Thanks for the reply, much appreciated.

I partially understood you. I've come across unsigned long before, and private.

I'm not exactly sure what you mean by this part for now, but I'll rewrite my code as I think you described, then repost in a bit and let's see how close I am.

@anon73444976

Here's what I think you said. I know it's off the mark though.

//=======Create Classes================
class timeControl {
  public:
    unsigned long getTime {
      unsigned long getTime = micros(); //1) Return the time (in micros) of right now
    };
    long storeSomeTime(long whatTime) {
      return whatTime;  //2) Store some arbitrary time that I pass to it so I can reference it later
    }
    long timeDifference(long startTime,long endTime) {
      return endTime  - startTime; //3) Run a function so that I can subtract one time against another and return the answer.
    }
   private:
    unsigned long _endTime = endTime;
    unsigned long startTime = startTime;
    unsigned long _whatTime = whatTime;
};

//=======Create Objects================
MotorTimes timeControl;


void setup() {
  
void loop() {
    Serial.println(MotorTimes.getTime);   //print the time in micros right now using the getTime function
    MotorTimes.storeSomeTime(micros()); //store the time right now in a variable within the class/ object MotorTimes.storeSomeTime
    delay(1000); //wait a second
    Serial.println(MotorTimes.timeDifference(MotorTimes.storeSomeTime, micros())); //use the function 'timeDifference' in the class to calculate the difference between now and the stored time 
    
  }

@kevcanni

1. Are you familiar with all the words pointed by arrows in the following Class Declaration (Fig-1)? If not, which one you don't know/understand?


Figure-1:

Now, answer to the following questions:

2. What is the name of the word class (small-c) -- I mean is it an ordinary word or a keyword?
3. What is the use of class?
4. What is name of the word timeControl -- I mean is it an object or "Class Name"?

5. What is the name of the word MotorTime -- I mean is it a ClassName? If yes, then you have to create it using class keyword.
6. What is the alternative name of ClassName?
7. What is name oe of timeControl -- I mean is it an object or a variabel?
8. What is the data type of timeControl?

That does nothing useful at all, and doesn't fulfil the promise you made that the function would return an unsigned long

This one does nothing except return the value you just gave it.

I want it to return "micros()" which I think is an unsigned long. should it be "void" as in the original code?

If it's void, it can't return a value.

So if it's not void, not long, not unsigned long, then what should it be?

The function should be of the same type as the value you're returning .
If you're not returning a value, it should be of type "void".

Do you want getTime to return a value?

Yes, I'd like it to return the time in micros()
in the main loop, this line should print the time in micros as worked out in the function
Serial.println(MotorTimes.getTime);

As I pointed out earlier, you're not calling the function getTime.

So, return that value.

Put simply, the idea of a class is to hide the data, and restrict access to it, so the class is in complete control of it.

you 'simple' class degugged! :wink:

//=======Create Classes================
class timeControl {
  public:
    unsigned long getTime(uint8_t currentTime = 1) { //currentTime = 1, Return the time (in micros) of right now
                                                     //currentTime = 0, Returns storeSometime value
      if (currentTime)  return micros();
      else return storedTime;
    }
    void storeSomeTime(unsigned long whatTime) {
      storedTime = whatTime;  //2) Store some arbitrary time that I pass to it so I can reference it later
    }
    unsigned long timeDifference(long startTime, long endTime) {
      return endTime  - startTime; //3) A function that subtracts one time against another and returns the answer.
    }

  private:
    unsigned long storedTime;
};

//=======Create Objects================
timeControl MotorTimes;

void setup() {
  Serial.begin(115200);
}
void loop() {
  Serial.println(MotorTimes.getTime());   //print the time in micros right now using the getTime function
  MotorTimes.storeSomeTime(micros()); //store the time right now in a variable within the class/ object MotorTimes.storeSomeTime
  delay(1000); //wait a second
  Serial.println(MotorTimes.timeDifference(MotorTimes.getTime(0), micros())); //use the function 'timeDifference' in the class to calculate the difference between now and the stored time

}

hope that helps...

if you have further questions regarding this 'corrected' code, you're more than welcome to ask then! :smiley:

Yes - thank you. I'm going to spend some time looking at this to see what you did. But thank you so much, really appreciated.

I'd give it a constructor, to initialise storedTime.

Cross check with the Class structure of Fig-1.

This is great, I understand how works and can adapt this for my project, it's taken a bit of time, but I'm understanding the "return" part much better now and why I was getting confused before. The only bit I didn't understand was this:

Serial.begin(115200);

Can you tell me why you chose 115200 and what I might expect if I used something different, say 9600?

Thanks for taking the time. To be honest, most of what you asked in the first post went over my head, but I appreciate the reference so that I can ask my questions better in future. Thank you.

You lost me a bit here. Sorry. I think it's solved now though.

I'd love to see what you mean . You clearly know your stuff. Thanks for taking the time already though, much appreciated.

It is imortant that you give some time to know about the constituent components of a class and then practice simple examples from the net. After that you come to the real application in your project. The C++ Programming will be really enjoyable.

1 Like