Add libraries to Spark

I am fairly new to coding, so I would like to apologize in advance if my question is too elementary for most of you on here.

I am familiar with using Arduino and adding libraries for it. However, I am not exactly sure how to the do the same for Spark. Would I need to flash my firmware each time I wanted to add a library? Does the .cpp and .h files have something to do with this? Also, is there a central location of Spark libraries?

Thanks!

willi2ps, we love questions from folks new to programming! Much like the Arduino, libraries are simply extra code you “compile” along with your program. They are indeed the extra .cpp and .h files. On the Spark web IDE, they are added with extra tabs to your main .ino “app” file that you created.

Because libraries are just extra code you add and is compiled along with your program, anytime you make changes to your program or add libraries you will need to flash those changes to your Spark Core. This would be the same with an Arduino.

The Spark Team is presently working on adding a library feature to the web IDE that would, like you said, centralize all the great libraries produced ported by the Elites and many of the forum members. This is VERY near to being finished and is being tested by the Elites so when it’s released, it is ready for the members to use and contribute to. :smile:

thank you! I see that you all rolled out the new library feature…awesome! I have another question. I am using this to make a food dispenser for my dog. If I wanted to feed him a specific time each day instead of a time interval, which library would I use?

thanks!

willi2ps:
You could take simple the time function.
See http://docs.spark.io/firmware/#libraries-time

void setup() {
    Time.zone( +/-UTC );      // Set your timezone (run at start)
}
void loop() {
      if ( time.hour() == X && time.minute() == Y { 
        feed();   // Feed the dog
      }
}

Curently their is no delay or milli break in this, because you don´t want to run this check 1000times a second :wink:

nice! I may not fully understand, but I thought that the delay would prevent it from checking “1000 times a second”. Isn’t it doing that now what the way the code is written?

@willi2ps, the great code snippet that @clyde provided does the IF statement as a fast as loop runs which could be about 2000 loops/sec. Since you are not checking seconds, you could add a delay(1000) at the end of loop but it would not do much since delay() will call the firmware background task much like reaching the end of loop() does. :smile:

thank you for the fast responses and code! @clyde @peekay123

Hi willi2ps:
Ok sorry for the short answer, I was at work and thought this would be enough to mention that problem. But as @peekay123 said - this thing loops so heavy that your spark core does check again and again and again if the statement is true. You could use delay(); but if your code grows you will find out that delay is not the best option as I remember from my Arduino project. Not sure if the same “problem” exists with the :spark: Core - but there comes time when interrupts looks pretty good to you and then all the delays spread over your entire code could be a mess.
When you use interrupts then you need (arduino knowledge, not tested on spark now) delayMicroseconds()! Read docs.spark.io under Firmware Reference -> Other Functions -> Interrupts.

Maybe this snipped could become quite handy:

// Dark Hack (pls do not try this at home)
// No millis() or delay() in interrupts, delayMicroseconds() max. input is 18383
// 18383ms are 0,018383s. Now you are able to do the calc! 1s=1000000microseconds      
// This loops runs currently 10 times which means 183830ms

for (int myDelay = 0; myDelay < 10 ; myDelay++) 
{          delayMicroseconds(16383);      }

Thank you for helping me with this. I am now getting some error message that I can figure out. Should I be adding a library or something?

Thank you!

Error Message:

Here is my code:

void setup() {
Time.zone(-5);
// Spark.syncTime();
pinMode(led, OUTPUT);
}

void loop() {
if (time.hour() == X && time.minute() == Y) {
feed();
}
}

void feed() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(6000);
}

@willi2ps all your “time” calls need to be Time with upper case “t”. So time.now() should Time.now(). :

1 Like