Internet Button - individual commands

I am working on something with the Internet Button(IB) and I am having trouble understanding the order within he code. Basically what I am looking to accomplish is having three LEDS of a certain color as a group for each button (2, 3, 4) and when a button is pressed the corresponding LED group turns off. This part I have…but for only one button. Meaning if after the IB powers up and the LEDs light up, if I press button 2, button 2’s LED group turns off. However, pressing buttons 3 or 4 do not turn off their LED group.

I guess I am unclear on where I need to place code so that it separates out these three individual commands.

Just to give a quick picture of what my project is: I want to press a button in the a.m. and it go off. (I am implementing timer function to reactivate after 22hrs). Then my wife press a button in the a.m. and my oldest son press the remaining button in the a.m.

After each individual has pressed their button, the LED group for that button goes off and remains off until the 22hr timer has reached its end.

Thanks in advance for any help.

If you could include the code you’ve already got, we could make some suggestions :slight_smile:

here it is and I will go ahead and say that I am sure it’s good for some laughs :smirk:

// This #include statement was automatically added by the Particle IDE.
#include "SparkTime/SparkTime.h"

// This #include statement was automatically added by the Particle IDE.
#include "InternetButton/InternetButton.h"

InternetButton b = InternetButton();

bool two     = true;
bool three   = true;   
bool four    = true;
int lastTwo;
int lastThree;
int lastFour;

void setup() {
    activateTwo();
    activateThree();
    activateFour();
    
    lastTwo = millis();
    lastThree = millis();
    lastFour = millis();
    
    b.begin();
}



void loop() {
    if (millis() - lastTwo > 7200) {
        if(b.buttonOn(2)){
            if (two) {
                two = false;
                b.ledOff(2);
                b.ledOff(3);
                b.ledOff(4);
                lastTwo = millis();
            }
        } else {
            two = true;
            activateTwo();
        }
    }


    if (millis() - lastThree > 7200) {
        if(b.buttonOn(3)){
            if (three) {
                three = false;
                b.ledOff(5);
                b.ledOff(6);
                b.ledOff(7);
                lastThree = millis();
            }
        } else {
            three = true;
            activateThree();
        }
    }


    if (millis() - lastFour > 7200) {
        if(b.buttonOn(4)){
            if (four) {
                four = false;
                b.ledOff(8);
                b.ledOff(9);
                b.ledOff(10);
                lastFour = millis();
            }
        } else {
            four = true;
            activateThree();
        }
    }
}

void activateTwo(){
    if(two){
        b.ledOn(2, 30, 255, 0);
        b.ledOn(3, 30, 255, 0);
        b.ledOn(4, 30, 255, 0);
    }
}
void activateThree(){
    if(three){
        b.ledOn(5, 0, 145, 255);
        b.ledOn(6, 0, 145, 255);
        b.ledOn(7, 0, 145, 255);
    }
}
void activateFour(){
    if(four){
        b.ledOn(8, 255, 0, 132);
        b.ledOn(9, 255, 0, 132);
        b.ledOn(10, 255, 0, 132);
    }
}

First, there is a mistake in the loop for the else of button4:

four = true;
            activateThree(); // this should be activateFour(); 

Then I am wondering why the code is waiting for 7200msec after the button is pressed. 22h is 7920000ms, are you using 7.2sec for testing?

Thanks @Fabien, I caught that shortly after my post determining why that group of LEDs were not activating.

Yes, using 7200 only for testing.

In Setup, can you try to move the following line at the beginning. Indeed, activateTwo(); is using the button while not initialized yet.

b.begin();

I just tried moving b.begin(); and now the LEDs are lit and pressing the buttons does not turn off the LEDs

I have successfully programmed the internet button to function the way I intended. Taking advice from my buddy at the office today, I scratched most of the code and worked from the bottom up again. Here is the final result:

// This #include statement was automatically added by the Particle IDE.
#include "SparkTime/SparkTime.h"

// This #include statement was automatically added by the Particle IDE.
#include "InternetButton/InternetButton.h"

InternetButton b = InternetButton();

bool one     = true;
bool two     = true;
bool three   = true;   
bool four    = true;

//7200 for testing
Timer oneTimer(7200, activateOne);
Timer twoTimer(7200, activateTwo);
Timer threeTimer(7200, activateThree);
Timer fourTimer(7200, activateFour);

void setup() {
    
    b.begin();
    
    activateOne();
    activateTwo();
    activateThree();
    activateFour();
    
}



void loop() {
    
    if(b.buttonOn(1)){
        if (one) {
            deactivateOne();
            oneTimer.start();
        }
    } else
    if(b.buttonOn(2)){
        if (two) {
            deactivateTwo();
            twoTimer.start();
        }
    } else
    if(b.buttonOn(3)){
        if (three) {
            deactivateThree();
            threeTimer.start();
        }
    } else
    if(b.buttonOn(4)){
        if (four) {
            deactivateFour();
            fourTimer.start();
        }
    } 
}
void deactivateOne(){
    one = false;
    b.ledOff(11);
    b.ledOff(1);
}
void activateOne(){
    one = true;
    b.ledOn(11, 255, 0, 0);
    b.ledOn(1, 255, 0, 0);
}
void deactivateTwo(){
    two = false;
    b.ledOff(2);
    b.ledOff(3);
    b.ledOff(4);
}
void activateTwo(){
    two = true;
    b.ledOn(2, 30, 255, 0);
    b.ledOn(3, 30, 255, 0);
    b.ledOn(4, 30, 255, 0);
}
void deactivateThree(){
    three = false;
    b.ledOff(5);
    b.ledOff(6);
    b.ledOff(7);
}
void activateThree(){
    three = true;
    b.ledOn(5, 0, 145, 255);
    b.ledOn(6, 0, 145, 255);
    b.ledOn(7, 0, 145, 255);
}
void deactivateFour(){
    four = false;
    b.ledOff(8);
    b.ledOff(9);
    b.ledOff(10);
}
void activateFour(){
    four = true;
    b.ledOn(8, 255, 0, 132);
    b.ledOn(9, 255, 0, 132);
    b.ledOn(10, 255, 0, 132);
}