Internet Button - Blink all LEDs for a set period of time

Well, I’ve read and Googled since yesterday. Much like when I taught myself PHP, HTML, CSS, etc. I need to ask a few questions and hopefully get some replies so I can connect my train of thought to how the code works.

I have the Internet Button. I’ve also started with an official library as it gets me a lot of the function I need. I’m connecting my button to various security things (cameras, switches, etc) for actions and notifications.

The library I’m using is in the Official Libraries - InternetButton - 9_ReleaseFirmware.CPP so I won’t copy all the code here (unless asked) as it’s easy to get to.

What I’m hoping to do, is make a function available in IFTTT that I can trigger with a motion sensor at home. I already have the connection working, so now I just need to figure out how to code my exact use case.

I’d like to have all the LEDs in the button blink (1 second on, one second off) for a set amount of time.

Bonus points for being able to set the duration in IFTTT (much like the rainbow function allows for)

Extra bonus points if I can input the duration as well as select the colour (This way I can use it for multiple alarm types, if not I can repeat the code once I know how to do it, changing the colour in the code)

It looks to me like in the setup I have to register my function (no idea if I’m using the right terms) in the block of code that starts with the comment about prototyping functions, which I did like:

// Prototypes of the functions that we'll use
int rainbowRemote(String command);
int ledOnRemote(String command);
int ledOffRemote(String command);
int ledAllOnRemote(String command);
int ledAllOffRemote(String command);
int gaugeRemote(String command);
int playSong(String command);
int motionAlarm(String command);

I then tried to smash together 2 pieces of code the each do a piece of what I need, the basic code I found for blinking all LEDs with the code for the rainbow function (as that allows for time input). I started here:

// My painful attempt to make my alarm. This is just the blinking LED
void ledMotion(){
    b.allLedsOn(100,0,150);
    delay(1000);
    b.allLedsOff();
    delay(1000);
}
// Maybe invokes the blinking LED above
int motionAlarm(String command){
    char inputStr[10];
    command.toCharArray(inputStr,10);
    int i = atoi(inputStr);
    long startMillis = millis();
    while(millis() < startMillis + i*1000){
        ledMotion();
    }
    return 1;
}

But I immediately realized that the blinky part of the code isn’t going to loop, so at best it’ll blink once.

Really, since I know so little, I don’t know how to Google to find what I need. I’d appreciate if anyone understands what I’m asking and could help with some guidance? Thanks!!

Some keywords to search the forum and/or docs might be “soft delay” and “software timer”.

The rule of thumb is: “Don’t use any delays or loops longer than a few milliseconds inside a Particle.function() routine!”, otherwise the call will timeout and block other functions meanwhile.
Rather use a flag to handle the task in loop() or start a software timer that does the blinking for you.

Instead of all this

    char inputStr[10];
    command.toCharArray(inputStr,10);
    int i = atoi(inputStr);

you can just use

  command.toInt();

If you want to store millis(), you’d need to declare that variable unsigned long or uint32_t rather than just long.
It’s even better to do this when using this kind of loop

  uint32_t ms = millis();
  while(millis() - ms < time2loop)
  { 
    ...
  }

Gives me a good amount of information to get started with, thanks for steering me!!

1 Like

Well, spent all day and man… I must be thick! I’ve read up on timers and millis and everything else through this site, arduino boards, etc.

I decided to start at the basic and then maybe expand. But I can’t even get the basic.

The minimum I need to learn is how to blink (something like 1 second on, 1 second off, or whatev) all LEDs on the internet button red for 30 seconds then turn off.

If anyone has a moment to guide me, OR has a sweet resource to learn from, I’d appreciate it!

I’ll whip something up when I’m back from work.


Update: Here you go

SYSTEM_THREAD(ENABLED)  // start running the code even without cloud connection

#include "InternetButton/InternetButton.h"

uint32_t blinkRate = 1000;  // milliseconds between blinks
uint32_t blinkTime = 30000; // milliseconds to keep blinking
uint32_t msSoftDelay;

InternetButton b = InternetButton();
Timer blinkTimer(blinkRate, doBlink);

void setup() 
{
    b.begin();
    b.allLedsOff();

    Particle.function("blink", startBlinking);
    Particle.function("duration", setDuration);
    Particle.function("rate", setRate);
}

void loop() 
{
    // this is demonstrating the use of "soft delay" for non-blocking timed tasks
    if (msSoftDelay != 0 && millis() - msSoftDelay > blinkTime)
    {
        blinkTimer.stop();
        b.allLedsOff();
        msSoftDelay = 0;
    }

    if (b.buttonOn(1) || b.buttonOn(2) || b.buttonOn(3) || b.buttonOn(4))
    {
        startBlinking("");  // just start blinking with current settings
        doBlink();          // first reactionw immediately
    }
}

int startBlinking(String cmd)
{
    blinkTimer.changePeriod(blinkRate);
    blinkTimer.start();
    msSoftDelay = millis();
    return blinkRate;   
}

int setDuration(String cmd)
{
    blinkTime = cmd.toInt();
    return blinkTime;
}

int setRate(String cmd)
{
    blinkRate = cmd.toInt();
    return blinkRate;
}

// this is the function called every "blinkRate" milliseconds by the software timer
void doBlink()
{
    static bool state = true;

    if(state)
        b.allLedsOn(0,0,255);
    else
        b.allLedsOff();
        
    state = !state;    
}
2 Likes

Man, thank you so much! I had come across all of the various code on my searches and could not figure out how to put it together.

This will take me a long way in figuring things out! Appreciate your time, time for me to learn!!

1 Like