Skip to content

Instantly share code, notes, and snippets.

@youngsoul
Created December 10, 2015 03:51
Show Gist options
  • Save youngsoul/9073b58009621420d326 to your computer and use it in GitHub Desktop.
Save youngsoul/9073b58009621420d326 to your computer and use it in GitHub Desktop.
Particle Photon Session 3 Project
#include "InternetButton/InternetButton.h"
/**
* Session3 Project
* Upon Startup:
* ** create a rainbow with the LEDs. hint: b.rainbow(10);
* ** set the Button LEDs (at position 3,6,9,12) to Red
* ** subscribe to an event called, 'showRainbow' that shows rainbow
* and returns the LEDs to their previous color. Either red or Green
* ** create a function call 'doReset' that will reset the LEDs to Red
*
* Loop:
* ** Detect when a button is pressed
* ** publish an event called: 'button1', 'button2', 'button3', 'button4'
* ** Light each LED associated with the button to Green
* ** Publish the 'buttonx' event for each button press
*
* Note:
* ** make sure you only publish one event per button press
*
*/
InternetButton b = InternetButton();
#define BUTTON1_LED 12
#define BUTTON2_LED 3
#define BUTTON3_LED 6
#define BUTTON4_LED 9
bool rainbow_mode = false;
bool button1_pressed = false;
bool button2_pressed = false;
bool button3_pressed = false;
bool button4_pressed = false;
bool button1_red = true;
bool button2_red = true;
bool button3_red = true;
bool button4_red = true;
float g_brightness = 0.1;
void ledOn(uint8_t i, int r_value, int g_value, int b_value, float brightness) {
r_value = (int)r_value*brightness;
g_value = (int)g_value*brightness;
b_value = (int)b_value*brightness;
b.ledOn(i, r_value, g_value, b_value);
}
void setButtonLights() {
if(button1_red == true) {
ledOn(BUTTON1_LED, 255,0,0, g_brightness);
} else {
ledOn(BUTTON1_LED, 0,255,0, g_brightness);
}
if(button2_red == true) {
ledOn(BUTTON2_LED, 255,0,0, g_brightness);
} else {
ledOn(BUTTON2_LED, 0,255,0, g_brightness);
}
if(button3_red == true) {
ledOn(BUTTON3_LED, 255,0,0, g_brightness);
} else {
ledOn(BUTTON3_LED, 0,255,0, g_brightness);
}
if(button4_red == true) {
ledOn(BUTTON4_LED, 255,0,0, g_brightness);
} else {
ledOn(BUTTON4_LED, 0,255,0, g_brightness);
}
}
/**
* Reset all of the LEDs to Red and the button pressed flags to false
* @params command - ignored
*/
int onResetButtonLights(String command) {
button1_red = true;
button2_red = true;
button3_red = true;
button4_red = true;
setButtonLights();
button1_pressed = false;
button2_pressed = false;
button3_pressed = false;
button4_pressed = false;
return 1;
}
// Particle.subscribe("do_event", myHandler, MY_DEVICES);
void onRainbowHandler(String event, String data)
{
b.rainbow(10);
b.allLedsOff();
setButtonLights();
}
void setup() {
// Tell b to get everything ready to go
// Use b.begin(1); if you have the original SparkButton, which does not have a buzzer or a plastic enclosure
// to use, just add a '1' between the parentheses in the code above.
b.begin();
onRainbowHandler("","");
Particle.subscribe("showRainbow", onRainbowHandler, MY_DEVICES);
Particle.function("doReset", onResetButtonLights);
}
void loop(){
// Process individual buttons and LED response
if (b.buttonOn(1) && button1_pressed == false) {
button1_pressed = true;
button1_red = false;
// Publish the event "button1" for other services like IFTTT to use
Particle.publish("button1","button 1 was pressed", 60, PRIVATE);
}
if (b.buttonOn(2) && button2_pressed == false) {
button2_pressed = true;
button2_red = false;
// Publish the event "button2" for other services like IFTTT to use
Particle.publish("button2","button 2 was pressed", 60, PRIVATE);
}
if (b.buttonOn(3) && button3_pressed == false) {
button3_pressed = true;
button3_red = false;
// Publish the event "button3" for other services like IFTTT to use
Particle.publish("button3","button 3 was pressed", 60, PRIVATE);
}
if (b.buttonOn(4) && button4_pressed == false) {
button4_pressed = true;
button4_red = false;
// Publish the event "button4" for other services like IFTTT to use
Particle.publish("button4","button 4 was pressed", 60, PRIVATE);
}
// based on the the buttonx_red value, set the led lights
setButtonLights();
if(b.allButtonsOff()) {
button1_pressed = false;
button2_pressed = false;
button3_pressed = false;
button4_pressed = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment