SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By Clontarf
I'm writing a code that is to keep track of knocks on a vibration sensor (on pin 11) of a lilypad simblee, and show the current score (number of knocks) and high score (highest number of knocks before the score is reset to "0" by pressing a button in the UI in the app.)

The code all compiles and uploads correctly, and the UI displays as expected (except for the current and high score variables) but I have several issues:

-in my code, the "currentScore" variable is initialized at 0 and should only increase once a threshold on the pin is exceeded, and then increase by one each time that threshold is exceeded. When running, currentScore stays at 1 constantly, even with nothing attached to pin 11. It never increases past 1.

-the "reset score" button should reset currentScore to "0" when pressed (occuring in the EVENT_PRESS part of the code where the button is pressed, near the bottom of the code). However, This does not occur, the CurrentScore is always displayed as 1

-The LED button and switch worked fine before I added the SimbleeForMobile.updateValue() code segments to update the user interface of the app. Now if either the button or the switch are pressed, the LED stays on permanently or will intermittently turn off and on for the first few times the button is pressed and then stay on/off until I reset the board. Previously the switch would turn it on/off and keep it on/off, and the button would momentarily change the state of the LED to the opposite of its current state.

Below is the code, if anyone can find any problems that may be causing these issues please let me know. I found that I need to call " if (SimbleeForMobile.updatable)" before any updating of values otherwise both the app will crash and the simblee will disconnect.

EDIT: I've noticed I didn't make use of the variables "sValue" and "hsValue" in my code (this code is me trying to make an old code compatible with the simblee as I learn how to use SimbleeForMobile, and I may have not formatted things correctly). I will keep messing around with it, if there are glaring issues or obvious things to fix I am appreciative of any tips.
Code: Select all
 * LED_Control LED Control from smart phone Mike Hord @ SparkFun Electronics 26
 * Jan 2016
 * This example demonstrates the use of the SimbleeForMobile library to control
 * a pin on a Simblee module from a phone app. We'll show both a button and a
 * switch in this example.
 * Resources: Please install the Simblee support files before attempting to use
 * this sketch; see
 * for
 * details.
 * ****************************************************************************/

// To use the SimbleeForMobile library, you must include this file at the top
// of your sketch. **DO NOT** include the SimbleeBLE.h file, as it will cause
// the library to silently break.
#include <SimbleeForMobile.h>

const int led = 13; 
int ledState = LOW;

// Every draw command returns a uint8_t result which is the object id that was
// created. If you wish to change the object later, you'll need this value,
// and if you want to catch an event created by an object, you'll need it
// there, too. Make sure you create these id variables outside of any function,
// as you'll need to refer to them in many other functions.

uint8_t btnID;
uint8_t switchID;
uint8_t titleText;
uint8_t btnText;
uint8_t scoreText;
uint8_t hiText;
uint8_t scorePlaceholder;
uint8_t hScorePlaceholder;

int16_t sValue = 0;
int16_t hsValue = 0;
int sensePin = 11;

void setup() 

  pinMode(led, OUTPUT);
  pinMode(sensePin, INPUT);
  digitalWrite(led, ledState);

  // advertisementData shows up in the app as a line under deviceName. Note
  // that the length of these two fields combined must be less than 16
  // characters!
  SimbleeForMobile.advertisementData = "Sidekik";
  SimbleeForMobile.deviceName = "Skik_1";

  // txPowerLevel can be any multiple of 4 between -20 and +4, inclusive. The
  // default value is +4; at -20 range is only a few feet.
  SimbleeForMobile.txPowerLevel = -4;

  // This must be called *after* you've set up the variables above, as those
  // variables are only written during this function and changing them later
  // won't actually propagate the settings to the device.

void loop() 

  int senseVal = analogRead(sensePin);
  int currentScore = 0;
  int highScore = 0;

   if(senseVal > 50){
    currentScore = (currentScore+1);

  if (currentScore > highScore){
    highScore = (currentScore);
 if (SimbleeForMobile.updatable){
    SimbleeForMobile.updateValue(scorePlaceholder, currentScore);
    SimbleeForMobile.updateValue(hScorePlaceholder, highScore);
  // This function must be called regularly to process UI events.


// ui() is a SimbleeForMobile specific function which handles the specification
// of the GUI on the mobile device the Simblee connects to.
void ui()
  // color_t is a special type which contains red, green, blue, and alpha 
  // (transparency) information packed into a 32-bit value. The functions rgb()
  // and rgba() can be used to create a packed value.
  color_t bgColor = rgb(198, 208, 252);

  // These variable names are long...let's shorten them. They allow us to make
  // an interface that scales and scoots appropriately regardless of the screen
  // orientation or resolution.
  uint16_t wid = SimbleeForMobile.screenWidth;
  uint16_t hgt = SimbleeForMobile.screenHeight;

  // The beginScreen() function both sets the background color and serves as a
  // notification that the host should try to cache the UI functions which come
  // between this call and the subsequent endScreen() call.

  titleText = SimbleeForMobile.drawText(
    (wid/2) -85, 20, "Sidekik App", BLUE, 36

  scoreText = SimbleeForMobile.drawText(
    (wid/2) -70, 100, "Current Score", BLACK, 26

  scorePlaceholder = SimbleeForMobile.drawText(
    (wid/2) -70, 150, sValue, BLACK, 26

  hiText = SimbleeForMobile.drawText(
    (wid/2) -60, 300, "High Score", BLACK, 26

  hScorePlaceholder = SimbleeForMobile.drawText(
    (wid/2) -70, 350, hsValue, BLACK, 26

  // Create a button slightly more than halfway down the screen, 100 pixels
  // wide, in the middle of the screen. The last two parameters are optional;
  // see the tutorial for more information about choices for them. The BOX_TYPE
  // button has a bounding box which is roughly 38 pixels high by whatever the
  // third parameter defines as the width.
  btnID = SimbleeForMobile.drawButton(
                              (wid/2) - 75,          // x location
                              (hgt/2) + 175,          // y location
                              150,                   // width of button
                              "Reset Score",         // text shown on button
                              BLACK,                 // color of button
                              BOX_TYPE);             // type of button

  // Buttons, by default, produce only EVENT_PRESS type events. We want to also
  // do something when the user releases the button, so we need to invoke the
  // setEvents() function. Note that, even though EVENT_PRESS is default, we
  // need to include it in setEvents() to avoid accidentally disabling it.
  SimbleeForMobile.setEvents(btnID, EVENT_PRESS | EVENT_RELEASE);

  btnText = SimbleeForMobile.drawText(
      (wid/2) -35, (hgt/2)+225, "LED on/off", BLUE, 14
  // Create a switch above the button. Note the lack of a title option; if you
  // want to label a switch, you'll need to create a textBox object separately.
  // A switch's bounding box is roughly 50 by 30 pixels.
  switchID = SimbleeForMobile.drawSwitch(
                              (wid/2) - 25,          // x location
                              (hgt/2)+ 250,            // y location
                              BLUE);                 // color (optional)

// This function is called whenever a UI event occurs. Events are fairly easy
// to predict; for instance, touching a button produces a "PRESS_EVENT" event.
// UI elements have default event generation settings that match their expected
// behavior, so you'll only rarely have to change them.
void ui_event(event_t &event)
  // We created the btnID and switchID variables as globals, set them in the
  // ui() function, and we'll use them here.

  if ( == btnID)
    if (event.type == EVENT_PRESS)
      if (SimbleeForMobile.updatable){
        SimbleeForMobile.updateValue(scorePlaceholder, 0);
      if (ledState == HIGH) digitalWrite(led, LOW);
      else digitalWrite(led, HIGH);
    if (event.type == EVENT_RELEASE)
      if (ledState == HIGH) digitalWrite(led, HIGH);
      else digitalWrite(led, LOW);

  // If the event was a switch press, we want to toggle the ledState variable
  // and then write it to the pin.
  if ( == switchID)
    if (ledState == HIGH) ledState = LOW;
    else ledState = HIGH;
    digitalWrite(led, ledState);

By Clontarf
After replacing all instances of "currentScore" with "sValue" and all instances of "highScore" with "hsValue" the updated scorePlaceholder value in the UI shows a constantly increasing number beginning anywhere from -28000 to 30000 every time the board is restarted (when it should just be 0) and the "reset score" button does nothing
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]