[Solved] How to add a timer in the tinker iOS app

I am not a programmer and I am trying to understand micro controller programming, All I want do is add a simple line of code to turn off a digital output in a spark core after a preselected time. If D7 was digitalWrite, have it stay on for 2 seconds and then turn off or change state to LOW. This is all thru the iOS app.

Thanks.

It would be possible to modify the tinker firmware to make all outputs timed. Or you could “hack” the analog or digital outputs to allow for customer timers.

For example:
D0-D7 are outputs
A0-A7 are timer values.

A analogOutput command to A0 for value 100 for example would have D0 toggle or turn off after 100 seconds.

Would that work for your situation?

That sounds promising, I only want the timer for maybe one pin, can you give me an example of what I need to add to the tinker app and where it needs to be placed.

Thanks

Do you want your time adjustable or constant?
If you want it constant it’s super easy. Adjusting the time with analogWritepins in Tinker App would be a bit fiddly on the handling side, but no problem on the code side :wink:

You say that you’re no programmer, but your willing to change the iOS Tinker app? That’s at least $99 a year (assuming you’re not jailbroken). It’s a lot easier to get rid of the Tinker app and make a webapp. Or, if you really want to keep the Tinker app, then edit the Tinker firmware on the Core, which shouldn’t be too hard.

it not hard for you guys that write code, but for someone starting out it can be very frustrating. I would like to keep the Tinker app and just add some code to it, just what to add and getting it right is the challenge.

Just want it to be constant, trying to make it open a garage door which needs a momentary switch action.

So I’ll assume that you’re willing to pay the developer fee, install XCode, and reprogram the iOS app? It’s no trivial task to “just add some code to it”.
Believe me, adding code to the Tinker firmware on the Core is MUCH easier. And to be honest, that’s kind of the point of the Core. I’d be a rather boring thing if you could only use the Tinker functionalities. If you want to, I can take a look at what it takes to edit the Tinker firmware, while keeping it compatible with the original Tinker app?
Random side question; you wouldn’t by any change have something on which you could host a simple HTML page? That’d be even nicer :slight_smile:

I agree with @Moors7 and we have already helped some people who said they have no clue about programming to get their own hands dirty and they liked to learn and see what they actually can do once they stop doubting themselves.

I’d go for the Tinker firmware and I’m sure with the right pointers you’ll do it.

Just to get yourself prepared for whatever we will be throwing at you, you can have a look at the Tinker firmware your Core already is running :wink:
The changes for your purpose should be no more than 10 to 20 lines of code :sunny:

https://github.com/spark/firmware/blob/master/src/application.cpp

3 Likes

When I first got to the Spark, I had no clue on how to program anything. I probably still don’t, but I get by :stuck_out_tongue:
Anyhow, it turned out I was right, and it wasn’t all that hard to edit the Tinker Firmware. Like you said, it was just “some lines of code” :wink:

if (pinNumber == 7){
    digitalWrite(pinNumber, value);
    delay(2000);
    digitalWrite(pinNumber, LOW);
}

Turns out, that was all it took. If you set D7 high in the Tinker app, it will go HIGH, wait 2 seconds, and go LOW again. The only thing is that the text in the app doesn’t change accordingly. Luckily, there’s still an indicator; after the pin goes LOW again, it sends a return to the app, after which the background of the button on the app fades away. I know that sounds a bit vague, but you’ll see it once you try it.

To see how the Tinker app works, you can have a look at the relevant docs over here: http://docs.spark.io/tinker/#tinkering-with-tinker-the-tinker-firmware
or in the github as provides above by @ScruffR , although the docs do some more explaining :wink:

Either way, you can try out my code by pasting the code below in the web IDE/Spark DEV and flash it to your Core. After that, hit the button in the app, and watch in awe as you blink the tiny blue LED :stuck_out_tongue:

int tinkerDigitalRead(String pin);
int tinkerDigitalWrite(String command);
int tinkerAnalogRead(String pin);
int tinkerAnalogWrite(String command);

//PUT YOUR VARIABLES HERE

void setup()
{
    Spark.function("digitalread", tinkerDigitalRead);
    Spark.function("digitalwrite", tinkerDigitalWrite);
    Spark.function("analogread", tinkerAnalogRead);
    Spark.function("analogwrite", tinkerAnalogWrite);

    //PUT YOUR SETUP CODE HERE
}

void loop()
{
    //PUT YOUR LOOP CODE HERE
}

int tinkerDigitalRead(String pin) {
    int pinNumber = pin.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    if(pin.startsWith("D")) {
        pinMode(pinNumber, INPUT_PULLDOWN);
        return digitalRead(pinNumber);}
    else if (pin.startsWith("A")){
        pinMode(pinNumber+10, INPUT_PULLDOWN);
        return digitalRead(pinNumber+10);}
    return -2;
}

//D7,HIGH
int tinkerDigitalWrite(String command){
    bool value = 0;
    int pinNumber = command.charAt(1) - '0';
    Serial.println(value);
    Serial.println(pinNumber);
    if (pinNumber< 0 || pinNumber >7) 
        return -1;
    if(command.substring(3,7) == "HIGH") 
        value = 1;
    else if(command.substring(3,6) == "LOW") 
        value = 0;
    else 
        return -2;
        
    if(command.startsWith("D")){
        pinMode(pinNumber, OUTPUT);
        //this checks if it's pin D7, then toggles
        //it off after 2 seconds.
        if (pinNumber == 7){
            digitalWrite(pinNumber, value);
            delay(2000);
            digitalWrite(pinNumber, LOW);
        }
        else
        //The above was the only thing that was added ^
            digitalWrite(pinNumber, value);
        return 1;
    }
    else if(command.startsWith("A")){
        pinMode(pinNumber+10, OUTPUT);
        digitalWrite(pinNumber+10, value);
        return 1;
    }
    else 
        return -3;
}

int tinkerAnalogRead(String pin){
    int pinNumber = pin.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    if(pin.startsWith("D")){
        pinMode(pinNumber, INPUT);
        return analogRead(pinNumber);}
    else if (pin.startsWith("A")){
        pinMode(pinNumber+10, INPUT);
        return analogRead(pinNumber+10);}
    return -2;
}

int tinkerAnalogWrite(String command){
    int pinNumber = command.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    String value = command.substring(3);
    if(command.startsWith("D")){
        pinMode(pinNumber, OUTPUT);
        analogWrite(pinNumber, value.toInt());
        return 1;}
    else if(command.startsWith("A")){
        pinMode(pinNumber+10, OUTPUT);
        analogWrite(pinNumber+10, value.toInt());
        return 1;}
    else return -2;
}
4 Likes

I was hoping it was simple, will try it soon thank you. How did you get started learning to program, I just keep getting frustrated, glad I tried the forum for help.

Thanks, works perfect. Thats exactly what I was looking for.

2 Likes

It really doesn’t get more simple than this. It were four lines of code: a comparison to check whether or not I had the right pin, a command to set the pin HIGH, a two-second wait, and a command so set the pin LOW again. The rest is still the Tinker Firmware, which is what you’ve been using thus far. The Spark is the one meant to be programmed, the app is not. The app with the Tinker Firmware are merely there to let you test the most basic functions. The real fun begins when you start to program it, and make it do the things you’d like it to do.
I started programming in school, with HTML/CSS, but that’s hardly worthwhile mentioning, since our teacher didn’t really do anything, other than saying:“if you can’t figure it out, Google it”. From there, I got into the Spark world, where I’ve had my first adventures with the Arduino style programming. I’ve been going slowly, learning as I move along, and still am today. The forum, and more importantly, the awesome people on the forum, have played a great role in that (shout-out to all you awesome people!). I followed the tutorials that are on here, and read through countless Arduino applications as well. Since they’re very similar, you can also use the Arduino resources to learn stuff. There are some great books for that, and it’s definitely worthwhile checking some of them out.
Getting frustrated about programming is actually a good thing, it means that you’re willing to make an effort. If I had to guess, I’d say that around 95% of my code never works the way it should. The learning process comes as soon as you’re trying to figure out why it isn’t working. It’s a bit of a cliché, but you can’t learn anything unless you make mistakes. Now, the great thing about programming is the fact that you will make mistakes. Lots of them. Repeatedly. Even stupid ones. Really stupid ones… Been there, done that, hated it, but learned a lot! It may be an odd quirk of mine, but I personally make a huge effort not to ask for help. I’d rather struggle with some stupidity for three weeks straight, but figure it out on my own, than ask for help. It’s not that I don’t trust anyone, but it helps me tremendously in learning. If you’ve wrestled with a problems for days on end, thinking about it when you go to sleep/wake up, you can be damn sure you’ll remember it in the future, whereas you’d forget it in a heartbeat if someone were to just give you the answer.
</philosopical insights in study methods>

Anyhow, enough with the rambling. This is not some devious scheme to deter you (or anyone else) from asking questions, but merely the way I prefer to learn stuff. Each to his own on that end, so if you do need any (further) help, please, by all means, feel free to ask!

4 Likes

Not quite as educational but look here, they even have Garage door monitors and controllers using Particle devices as the brains. A great place to learn is add your own features to something someone has started. I’m no expert but hadn’t written any firmware at all prior to a few months ago and found it easier than expected. Read, research and just try it. If it doesn’t work…post here with code and the error message, someone will usually point you in the right direction.

https://www.hackster.io/particle/projects

1 Like