Compile failed, and the error code makes no sense to me

not quite sure how to make the nice code boxes everyone else has so im just going to copy and paste my stuff here, i tried to compile using SparkDev and the web IDE but neither worked. any help is appreciated.

here is my code:

int WindowUp = D0;
int WindowDown = D1;
int isOpen = 0;

void setup() {

    Spark.function("activate", WindowControl);

    pinMode(WindowUp, OUTPUT);
    pinMode(WindowDown, OUTPUT);

    digitalWrite(WindowUp, LOW);
    digitalWrite(WindowDown, LOW);
}

void loop() {

}

void WindowControl() {
    if (isOpen == 0) {
        digitalWrite(WindowUp, HIGH);
        delay(500);
        digitalWrite(WindowUp, LOW);
    }
    else if (isOpen == 1) {
        digitalWrite(WindowDown, HIGH);
        delay(500);
        digitalWrite(WindowDown, LOW);
    }
}

and the errors:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from the_user_app.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
the_user_app.cpp: In function 'void setup()':
the_user_app.cpp:7:45: error: invalid conversion from 'void (*)()' to 'int (*)(String)' [-fpermissive]
int WindowUp = 0;
^
In file included from ../inc/spark_wiring.h:33:0,
from ../inc/application.h:29,
from the_user_app.cpp:2:
../inc/spark_utilities.h:109:14: error: initializing argument 2 of 'static void SparkClass::function(const char*, int (*)(String))' [-fpermissive]
static void function(const char *funcKey, int (*pFunc)(String paramString));
^
make: *** [the_user_app.o] Error 1

As the docs state and the error message suggests, your Spark.function needs to be of this type
int fn(String) and not void fn().

Some background can be found here

1 Like

Hi @MasterVizz

Your Spark function needs to take a String (object) and return an int. That is the contract of all Spark functions so that the cloud can pass parameters and get return values to and from your functions in a consistent way. You need to do this even if you are not using the input and you should return an int value (such as 0 but anything integer really).

So you need:

int WindowControl(String cmds) {
 ...//as before
  return(0);
}

Using delay(500) in the Spark function is not ideal since that delays when the return value is sent to cloud, so it could timeout. A better way is to treat the Spark function like an interrupt handler and just set a variable so that the next time through loop() you do the actual work.

1 Like

thanks guys, just got one of these the other day, im still trying to get the hang of using it :smiley:

1 Like