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.
Return the time (in micros) of right now
Store some arbitrary time that I pass to it so I can reference it later
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.
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.
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
}
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?
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);
//=======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!
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.
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.