Skip to content

Commit

Permalink
add code to block the interrupt after a recent button click
Browse files Browse the repository at this point in the history
  • Loading branch information
Dillon authored and Dillon committed Nov 22, 2015
1 parent b5721d7 commit bbd6ce0
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions spark-lamp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ int lampState = 0b00; // stores the state of both lamps
ClickButton dillonButton(dillonLamp, LOW, CLICKBTN_PULLUP);
ClickButton saraButton(saraLamp, LOW, CLICKBTN_PULLUP);

/* There's an issue with the relay interrupt being called right after
a lamp switch is pressed (but only if the light switch is "off"),
so we use this counter to block the interrupt */
int blockInterrupt = 0;
#define blockInterruptTimes 5

/* Set up pins and Spark web functions */
void setup()
{
Expand Down Expand Up @@ -82,21 +88,31 @@ void loop()
dillonButton.Update();
saraButton.Update();

// count down as it goes through the loop
if (blockInterrupt)
{
//Serial.println(blockInterrupt);
blockInterrupt--;
}

// Dillon's button was clicked
if (dillonButton.clicks != 0){

if(dillonButton.clicks == 1){
//Serial.println("SINGLE click");
blockInterrupt = blockInterruptTimes;
toggleDillon();
}

if(dillonButton.clicks == 2){
//Serial.println("DOUBLE click");
blockInterrupt = blockInterruptTimes;
toggleSara();
}

if(dillonButton.clicks == -1){
//Serial.println("SINGLE LONG click");
blockInterrupt = blockInterruptTimes;
matchToggle("DILLON");
}
}
Expand All @@ -106,16 +122,19 @@ void loop()

if(saraButton.clicks == 1){
//Serial.println("SINGLE click");
blockInterrupt = blockInterruptTimes;
toggleSara();
}

if(saraButton.clicks == 2){
//Serial.println("DOUBLE click");
blockInterrupt = blockInterruptTimes;
toggleDillon();
}

if(saraButton.clicks == -1){
//Serial.println("SINGLE LONG click");
blockInterrupt = blockInterruptTimes;
matchToggle("SARA");
}
}
Expand Down Expand Up @@ -216,16 +235,28 @@ void matchToggle(String button){

/* set both lamps to the state of the lightswitch */
void lightSwitchLamps(){
if (digitalRead(lightSwitch) == LOW){
// turn both lamps off
mySwitch.send(DILLON_OFF, BIT_LENGTH);
mySwitch.send(SARA_OFF, BIT_LENGTH);
lampState = 0b00;
}
else{
// turn both lamps on
mySwitch.send(SARA_ON, BIT_LENGTH);
mySwitch.send(DILLON_ON, BIT_LENGTH);
lampState = 0b11;
// only trust this interrupt if the state hasn't changed recently
/* Note: Due to the relay bouncing, this interrupt is called multiple
times in a row for one switching motion. We don't want to debounce
them in software because it's useful to quickly switch the lights
from one state to another to get them in sync */
if (blockInterrupt == 0)
{
if (digitalRead(lightSwitch) == LOW){
//Serial.println("lightSwitch off");
//blockInterrupt = blockInterruptTimes;
// turn both lamps off
mySwitch.send(DILLON_OFF, BIT_LENGTH);
mySwitch.send(SARA_OFF, BIT_LENGTH);
lampState = 0b00;
}
else{
//Serial.println("lightSwitch on");
//blockInterrupt = blockInterruptTimes;
// turn both lamps on
mySwitch.send(SARA_ON, BIT_LENGTH);
mySwitch.send(DILLON_ON, BIT_LENGTH);
lampState = 0b11;
}
}
}

0 comments on commit bbd6ce0

Please sign in to comment.