Visual Micro
https://www.visualmicro.com/forums/YaBB.pl
Other >> 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 12:16pm

Title: Using millis() in a class method - out of scope
Post by davidbal on Jul 25th, 2017 at 12:16pm
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 12:32pm
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 12:59pm
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 1: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 7:56pm
OK Thanks..I will remember that tip...

Regards,

David

Visual Micro » Powered by YaBB 2.5.2!
YaBB Forum Software © 2000-2020. All Rights Reserved.