uoitlogo
App Inventing Workshop

Stepper

    A pedometer for your phone!
qr_stepper apk_stepper
stepper_flowchart
When the app is initialized, the Clock starts and the event sources (the buttons) are loaded. The start button turns both the accelerometer and the orientation sensor on which measure real-time data as the phone is moved.

A logic controller is implemented that evaluates the X- and Y-accelerations as the phone is moved in a stepping motion. Once specific accelerations have been reached (ie. a step has been taken), a snapshot of the time is taken, and the accelerometer is disabled. That time snapshot is immediately compared with the system clock to determine the difference. If the difference is small enough then the step function is called, updating the steps by 1.
stepper_design
Component Palette Name Description
Label User Interface lblheaderSteps Visual-only Label.
Label User Interface lblBlank Visual-only Label, space.
Label User Interface lblSteps Updates with number of steps.
Button User Interface btnStart Starts the accelerometer, orientation sensor. Clears step count.
Button User Interface btnStop Stops the accelerometer and orientation sensor.
Button User Interface btnRecord Updates log with date, time, and steps.
Label User Interface lblheaderLog Visual-only Label
Label User Interface lblLog Where logged steps appear.
Label User Interface lblTime Hidden. Holds value of time snapshot.
Accelerometer Sensors AccelerometerSensor1 Measures proper acceleration of the device.
Orientation Sensor Sensors OrientationSensor1 Measures the roll, pitch, and azimuth of the device.
Clock Sensors Clock1 Current time in Unix. Interval = 100
block_menu
Control, Logic, Math, Text, Lists, Colors, Variables, and Procedures can all be accessed from the "Blocks" panel on the left-hand side. Each object that is placed in the design window has properties associated with it that can utilized using blocks. For instance, a button has contains events that listen for things such as a quick tap or a long hold. Logic can be added with these events to control various properties (like text and colour) or to call other functions.

As what was defined above, we want the start button to enable the accelerometer and orientation sensor, as well as to reset the step counter. This can all be done with five blocks.
start
Drag out a .Click event for the Start Button. Set the boolean value of the accelerometer and orientation sensor to TRUE as to activate them. Initialize the global variable "step" by dragging the block from the "Variables" tab. Attach the number 0.

TIP: Click on the white space and type a number or variable for quick access!

Once you initialize a global variable, you can select it from the drop-down menu of the "set" and "get" commands. Set the global step variable to 0.
Lastly, set the text of lblSteps to "0". Custom text can be added from the "Text" tab.
count
Next, we will need a procedure that will update the step count on the screen. Drag out a to-do block the "Procedures" tab. Add a "set" block for the text of the lblSteps label. From the variables tab, add a "get" command for the steps variable. The steps variable starts at 0 like we defined it. Later on we will call this procedure with a new step variable which will be displayed in the lblSteps label.
print
Add another to-do procedure, call it "print". Click the blue square on the print block to add a new input. Call this input variable "textPrint".

NOTE: textPrint is a local variable, not global, and will not be listed in the Variables tab.

Drag out a "set" command for the text of the lblLog label. This function will create a list (a log) of all the recorded steps by appending the text each time the procedure is called. Add a "join" block from the Text tab. This will be used to combine the new text will the previous ones recorded. Hover over the textPrint to see the get and set commands. Drag the "get textPrint" block into the first joint of the join block. Add a custom text block to the next joint and type "\n" which creates a new line. Lastly, add the lblLog.Text variable. Every time a new step count is recorded it gets stored in the lblLog label.
record
Drag out a .Click event for the Record button. The only thing we want to do when we press the Record button is update the log, so we will drag out the "call" command for our print procedure from the Procedure tab. We will add another join block to combine the date, time and steps counted. Click the "Clock1" tab from the left panel gives us the option to call a built in function known as .FormatDateTime. This function converts the system time (in Unix) to a nice readable format. We need to give it some input, so add the .Now call block the Clock tab as well.

In the middle of the join block, simply add a custom text block with the word " Steps: ". Adding a space to each side makes it more visually appeal on the screen. Finally, add the .Text variable for our lblSteps label, which stores our step count in a text format.
accelerometer
This is where the magic happens. Initialize a global variable and call it time. Set its value to 0.



What we are interested in doing here is to determine the moment the device reaches a position within the path of a step. The numbers -2 and 8 were of my personal choosing based on what I consider a walking motion [**HOW YOU CAN ANALYSE YOURS**].
We need to create a Procedure called timeSnapShot. Include an if-then statement inside. We want to check when the X-Acceleration is less-than or equal to -2 AND when the Y-Acceleration is greater-than or equal to 8. When this is true, we want to set          the boolean value of .Enable for the AccelerometerSensor1 to False; turn if off.

Additionally, we want to set the value of the variable time equal to the System Time at that moment (this will be our snap-shot).

Drag out the .AccelerationChanged event from Accelerometer panel. The first thing we'll do is call the timeSnapShot procedure that we just made. As long as the Accelerometer is enabled, it will be checking for the positions we defined and giving us time snap-shots.
What we need to do next is test the time difference between the snap-shot (time) and the .SystemTime of the Clock1. If the difference is smaller than 20, it constitutes as a step. The step variable will then increase by 1, and the procedure updateSteps will be called.
orientation
This next block is will be used to turn the accelerometer back on. Drag out a .Timer event for our Clock1. Add an if-then statement. Again, based on my own observation, have determined that when the Roll [**What is Roll?**] of the device reaches a value of 20, then a person has completed a step. So once the .Roll of the OrientationSensor1 is greater-than or equal to 20, we need to set the .Enabled value of the AccelerometerSensor1 to TRUE.
stop
Lastly, we need to give the stop button a purpose.
Simply drag the .Click event of the btnStop button out, set the .Enabled values of both the accelerometer and orientation sensor to FALSE.