Need help with code

I want the spark core to monitor 5 buttons and give feedback when one of the buttons is attached. I would also like to keep a ticker of how many times a button has been pressed. I need lots of help with interacting and coding here. Anyone have some advice?

Hi @mikesr20

That sounds like it will be great and not hard at all to do on Spark. First off, if you haven’t looked at the examples in the doc and the tutorial section here in the forum, I would do a little reading there first.

Can I ask what you mean by give feedback? From the core locally on a display or LEDs or did your mean over the network? Keeping a count of how many times a particular button has been pressed is easy too–where does the count get displayed?

thanks for the quick feedback, I am going through the examples but am having trouble with understanding the web side of the blinking the LED’s from a web page.
I would like to eventually develop an app that keep the running total, for the moment I could have it setup on a webpage. I would like to be able to log the data and compare how many times each button has been pressed vs each button. (i.e. button 1 has been pressed 10 times, button 3 has been pressed 5 times) eventually being able to log the information and graph it over a time scale.
Is it possible to get a time and date stamp associated with the button press?

@mikesr20, are these buttons being pressed by a human? I ask because I need to understand if these are irregular presses or not and how long each press is expected to be. I have a button library which may be helpful and yes, using the Time calls in the firmware, you can timestamp to 1 second accuracy. :smile:

Yes they will be pressed by a human.

@mikesr20, there is a ClickButton library that can handle the reading and debouncing of buttons. I will be publishing that library shortly. There are several ways to interact with the Spark including Spark.variables, Spark.function and Spark.publish. I suggest you look at the core firmware documentation to get an idea of what these do. I also highly recommend looking at the tutorials that @bko prepared in the Tutorials category of this forum. :smile:

1 Like

Ok, so I have worked through several examples I am needing help with Spark.Publish (if that is the right function for me to use). I would like the data to “push” from the core to google docs when a button is pushed. All I need is to know which of the 5 buttons is pushed and a time/date stamp. Please ignore any of the quirks in the code below I know it needs cleaned up severely:
int button1 = D0; // button is connected to D0
int button2 = D1; // button is connected to D1
int button3 = D2; // button is connected to D2
int button4 = D3; // button is connected to D3
int button5 = D4; // button is connected to D4
int LED = D6; // LED is connected to D6
int val = 0; // variable to store the read value
int Button1Count = 0;
int Button2Count = 0;
int Button3Count = 0;
int Button4Count = 0;
int Button5Count = 0;
char resultstr[64];

void setup()
pinMode(LED, OUTPUT); // sets pin as output
pinMode(button1, INPUT_PULLDOWN); // sets pin as input
pinMode(button2, INPUT_PULLDOWN); // sets pin as input
pinMode(button3, INPUT_PULLDOWN); // sets pin as input
pinMode(button4, INPUT_PULLDOWN); // sets pin as input
pinMode(button5, INPUT_PULLDOWN); // sets pin as input
Spark.variable(“Button1Count” , &Button1Count, INT);
Spark.variable(“button1” , &button1, INT);
Spark.variable(“Button2Count” , &Button2Count, INT);
Spark.variable(“button2” , &button2, INT);
Spark.variable(“Button3Count” , &Button3Count, INT);
Spark.variable(“button3” , &button3, INT);
Spark.variable(“Button4Count” , &Button4Count, INT);
Spark.variable(“button4” , &button4, INT);
Spark.variable(“Button5Count” , &Button5Count, INT);
Spark.variable(“button5” , &button5, INT);
Spark.variable(“result”, &resultstr, STRING);


void loop()
val = digitalRead(button1); // read the input pin
digitalWrite(LED, val); // sets the LED to the button’s value
if (digitalRead(button1) == HIGH)
//Button1Count ++;
Spark.Publish(“button1”, NULL, 15, PRIVATE);


val = digitalRead(button2); // read the input pin
digitalWrite(LED, val); // sets the LED to the button’s value
if (digitalRead(button2) == HIGH)
Button2Count ++;
sprintf(resultstr, “{“Button2Count”:%d}”, Button2Count);
val = digitalRead(button3); // read the input pin
digitalWrite(LED, val); // sets the LED to the button’s value
if (digitalRead(button3) == HIGH)
Button3Count ++;
sprintf(resultstr, “{“Button3Count”:%d}”, Button3Count);

val = digitalRead(button4); // read the input pin
digitalWrite(LED, val); // sets the LED to the button’s value
if (digitalRead(button4) == HIGH)
Button4Count ++;
sprintf(resultstr, “{“Button4Count”:%d}”, Button4Count);

val = digitalRead(button5); // read the input pin
digitalWrite(LED, val); // sets the LED to the button’s value
if (digitalRead(button5) == HIGH)
Button5Count ++;
sprintf(resultstr, “{“Button5Count”:%d}”, Button5Count);