VS Arduino
Visual Micro >> Project Guidance >> Using millis() in a class method - out of scope
https://www.visualmicro.com/forums/YaBB.pl?num=1500981366

Message started by davidbal on Jul 25th, 2017 at 11:16am

Title: Using millis() in a class method - out of scope
Post by davidbal on Jul 25th, 2017 at 11:16am
When I try to use millis() in a class method e.g.

void GeneralTimer::startTimer(unsigned long dur)
{
     duration = dur;
     startTime = millis();
     endTime = startTime + duration;
}

The compiler complains millis() is out of scope. I can understand why as it is a standard arduino library, but can't see a way of resolving the scope.

I have seen an example here https://learn.adafruit.com/multi-tasking-the-arduino-part-1/a-classy-solution where it seems to work, although it is all defined within the class.

Any tips?

Regards,

David

Title: Re: Using millis() in a class method - out of scope
Post by Visual Micro on Jul 25th, 2017 at 11:32am
have you included the arduino.h in your code?

Title: Re: Using millis() in a class method - out of scope
Post by davidbal on Jul 25th, 2017 at 11:59am
Thanks, that fixed the problem.
It is quite a big project with multiple files and thought it might of already been included. But obviously not.

I get the feeling that the .ino file includes that file automatically?

Regards,

David

Title: Re: Using millis() in a class method - out of scope
Post by Visual Micro on Jul 25th, 2017 at 12:21pm
Yes when .ino is converted to .cpp the include is added

When you create new .cpp/h files using the visual micro menus the arduino.h is included for you

If you create your own cpp/h files manually you will always need to include the arduino.h

Title: Re: Using millis() in a class method - out of scope
Post by davidbal on Jul 25th, 2017 at 6:56pm
OK Thanks..I will remember that tip...

Regards,

David

VS Arduino » Powered by YaBB 2.6.12!
YaBB Forum Software © 2000-2024. All Rights Reserved.