0

I'm building a project to measure/track flyball dog races. The dogs timings are measured by photoelectric sensors at the start/finish line of the racing lane. I have most of my code working (available on github) but I'm having some difficulties in handling very short crossings of dogs. Since I cannot realistically run a real race at home next to my development machine :-), I'm trying to build a simulator class which gives the triggers that would normally come from my photoelectric sensors.

I have a RaceHandler class which has the following in its header file:

class RaceHandlerClass
{
 public:
   long* lRaceStartTime = &_lRaceStartTime;

 private:
   long _lRaceStartTime;
}
extern RaceHandlerClass RaceHandler;

As you can see I made a public pointer lRaceStartTime which points to the private _lRaceStartTime member variable. This might seem stupid/strange, but it really should a private member variable, I just want to 'temporarily' make it publicly available, so that I can use it in a temporary 'simulator' class to simulate a race towards my code.

Then I want to use this public pointer in the Simulator class like so:

long* lRaceStartTime = RaceHandler.lRaceStartTime;
long lRaceElapsedTime = micros() - lRaceStartTime;

However I get the following error when trying to compile this:

error: invalid operands of types 'long unsigned int' and 'long int*' to binary 'operator-'

I'm afraid my c++ knowledge ends here and I have no clue what I should do different to fix this... Any help would be greatly appreciated!

  • 1
    Since I cannot realistically run a real race at home next to my development machine - how about mice? – Nick Gammon Feb 11 '16 at 5:12
2

Try this:

long* lRaceStartTime = RaceHandler.lRaceStartTime;
long lRaceElapsedTime = micros() - *lRaceStartTime;

You assigned the pointer to lRaceStartTime on the first line, but on the second line you are subtracting the pointer from micros() which returns an unsigned long. You need to dereference the pointer before subtracting. Also you might consider making all your longs unsigned to match the return type of micros() http://arduino.cc/en/reference/micros

| improve this answer | |
  • Thanks, this indeed solved my problem. Guess I need a little more practice with pointers... – Alex Mar 31 '15 at 6:58
  • No worries, happy to help. Feel free to post more questions, this site needs to get out of beta! – portforwardpodcast Mar 31 '15 at 7:01
0

I realise your question was but a simple query on syntax. But I would like to point out that the whole idea of object orientation is encapsulating functions and data so that external calls do not have to know about the internal-workings of that class.

It's common with object oriented programming to use 'getters' and 'setters' to modify private member-variables of the class. You really should not be giving a pointer to a private member. If you need to read/write it, add a setter function.

class RaceHandler
{
public:
    void setStartTime()
    {
        race_start_time = micros();
    }

    void setStartTime(unsigned long start_time)
    {
        race_start_time = start_time;
    }

    unsigned long getStartTime()
    {
        return race_start_time;
    }

    unsigned long getElapsedRaceTime()
    {
        return micros() - race_start_time;
        // TODO: handle overflow of micros() after ~72 minutes
        // maybe use millis() instead
    }


private:
    unsigned long race_start_time;
}

Obviously this is laborious. Some languages have special support for getters and setters to provide the functionality with less syntax.

Time is always positive, use an unsigned long. As people have already said, millis() & micros() return an unsigned long too.

RaceHandler race1;

race1.setStartTime();

[...]

unsigned long elapsed_time = race1.getElapsedRaceTime();
| improve this answer | |
  • Time is always positive, use an unsigned long. - whilst this is quite true on the Arduino, I recently discovered that time_t is typedef'd to signed integer. In other words, you can represent times before 1st January 1970 by using negative numbers. Of course, this doesn't apply on the Arduino, as the concept of millis() returning time before the system was reset doesn't make a lot of sense. – Nick Gammon Feb 11 '16 at 5:15
  • @Nick Gammon - Yep, time_t is signed. We've got until 2038 to sort that out ;) – Kingsley Feb 15 '16 at 2:25
  • Which is getting increasingly close. :) – Nick Gammon Feb 15 '16 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.