elapsedMillis not compiling? [solved] ("application.h")

trying to use the elapsedMillis library but Im getting a few error. Im not really sure why can someone point me in the right direction.

elapsedMillis.h:34:36: error: 'millis' was not declared in this scope
  elapsedMillis(void) { ms = millis(); }

elapsedMillis.h:35:49: error: 'millis' was not declared in this scope
  elapsedMillis(unsigned long val) { ms = millis() - val; }

??

@lightx, how are you compiling your code? IDE, CLI, DEV ,locally?

@peekay123 at the moment, WEB IDE

@lightx, it is behaving as if `#include “application.h” is not there but if your file is a .ino file then the compiler takes care of adding that. Can you paste your code here so I can take a look?

im sorry in advance @peekay123 … here is the code

#include "elapsedMillis.h"

elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop runs


// Debug mode
boolean DEBUG = false;

// motion variables BEGIN -----------------------------------
int val = 0;
int calibrateTime = 10000;      // wait for the thingy to calibrate
long unsigned int lowIn;        // the time when the sensor outputs a low impulse
unsigned long pause = 60000UL;  // amount of milliseconds the sensor has to be low before we assume all motion has stopped
boolean lockLow = true;
boolean takeLowTime;
boolean currentMotion = false;
int pirPin = D0;                        //the digital pin connected to the PIR sensor's output

// motion variables END -------------------------------------
int ledPin = D1;                     // led pin
char myIpString[24];                // localIP

// light sensor pin
int ldrPin = A0; //analog pin 0
unsigned int ldrReading = 0;

// delay in milliseconds 
unsigned int interval = 1000;

void setup() {
    Serial.begin(9600);
    if(DEBUG){Serial.println("Registering Variables");}

    // my network localIP
    IPAddress localIp = WiFi.localIP();
    sprintf(myIpString, "%d.%d.%d.%d", localIp[0], localIp[1], localIp[2], localIp[3]);
    Spark.variable("ipAddress", myIpString, STRING);
    Spark.variable("motion", &currentMotion, BOOLEAN);
    Spark.publish("boulder rev 14 is online...", NULL, 60, PRIVATE);
    Spark.variable("ldrReading", &ldrReading, INT);

    pinMode(ledPin, OUTPUT);        // led as output
    pinMode(pirPin, INPUT);         // declare sensor as input
    pinMode(ldrPin, INPUT);
    
    if(DEBUG){Serial.println(".. done");}

    // take control of the LED
    RGB.control(true);
    // red, green, blue, 0-255
    RGB.color(0, 0, 0);
    delay(20);
}

void loop() {
    if (calibrated()) {            ////
        readTheSensor();        // motion
        reportTheData();        ////
    }
    ldrReading = analogRead(ldrPin);
    publishLDR();
}

void publishLDR() {
    if (timeElapsed > interval) {
        Spark.publish("ldrReading", NULL, 60, PRIVATE);
    }
    timeElapsed = 0;
}

bool calibrated() {
    return millis() - calibrateTime > 0;
}

void readTheSensor() {
    val = digitalRead(pirPin);
}

void reportTheData() {
   if(digitalRead(pirPin) == HIGH){
        if(lockLow){
            //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;
            digitalWrite(ledPin, HIGH);
            if(DEBUG) {
             Serial.println("---");
                Serial.print("motion detected at ");
            Serial.print(millis()/1000);
            Serial.println(" sec");
            }
            delay(50);
            currentMotion = true;
            Spark.publish("boulder_motion", "true");
        }
      takeLowTime = true;
    }


    if(digitalRead(pirPin) == LOW){
        if(takeLowTime) {
            lowIn = millis();          //save the time of the transition from high to LOW
            takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }

        //if the sensor is low for more than the given pause,
        //we assume that no more motion is going to happen
        if(!lockLow && millis() - lowIn > pause) {
            //makes sure this block of code is only executed again after
            //a new motion sequence has been detected
            lockLow = true;
            digitalWrite(ledPin, LOW);
            Spark.publish("boulder motion ended", NULL, 60, PRIVATE);
            if(DEBUG) {
                Serial.print("motion ended at ");      //output
                Serial.print((millis() - pause)/1000);
                Serial.println(" sec");
            }
            delay(50);
            currentMotion = false;
            Spark.publish("boulder_motion", "false");
        }
    }
}

@lightx, did you attach the elapsedMillis library to your app?

@peekay123 yep. I did. The error is coming from the elapsedMillis.h file…

i dont really understand why its giving an error about the millis() function … ?!?

I just made an app, attached the library and pasted your code. I compiled without error.

Can you please change the #include line to #include "elapsedMillis/elapsedMillis.h"

well thats weird, so if I copy the elapsedMillis.h code into its own tab it wont work but if I “include” the library it will… ???

why is that @peekay123?

If you include it in its own tab, it needs to be in the .h tab. You also have to go back to the original #include statement.

indeed @peekay123 and since the error was the .H tab. The problem was the millis function which it shoudnt do that .. right?! why a function like millis() giving an error I guess is my point.

@lightx, I get it to compile without errors so there is something off with the way your doing things IMO.

be sure you have

#include "application.h"

at the top of the .h file

As @peekay123 had already pointed out rather early in this thread.

I had this problem last week and for me it was due to having an #include statement on the first line. I had removed the comment about the IDE adding that include to clean up my code. Adding an empty line or comment on line 1 fixed this issue. Also mentioned here: #Include on first line fails to compile in Sparkulator

thank you @peekay123, @mdma and @ScruffR for pointing me to “application.h” (yes I should have listen to @peekay123 in the first place)

one quick question, why isnt there something about “application.h” in docs.particle.io? Im hacking my way through this and I would love to know more about what this “application.h” is.

application.h is just a header file that wraps up a bunch of other #include statements so that you can use all the classes and functions you’d usually need for writing your program (e.g. spark_wiring.h) - this is similar to Arduino.h on that platform.
With this one header you’ll have some abstraction layer (e.g. spark_wiring.h can be changed to particle_wiring.h behind the scenes without breaking existing code) and you got the convenience that you only need to add one header instead of a bunch.

Another background info:
If your main project file is an .INO file, you don’t need to #include "application.h" since the preprocessor will do that for you. In a .H/.CPP file, you need to do it yourself.

2 Likes