... | ... | @@ -217,25 +217,21 @@ while True: |
|
|
|
|
|
2. Complete the code. Specifically:
|
|
|
|
|
|
* In the data packet received block/function (capture_data in Python), get the Micro::Bit's x, y, and z acceleration values. One potential solution to this code section can be seen below.
|
|
|
* In the capture_data function, get the Micro::Bit's x, y, and z acceleration values. One potential solution to this code section can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
<img src=/uploads/4ccf8061254ed93b28fa661644f3a7bc/fit_1.PNG>
|
|
|
<pre>
|
|
|
radio.onDataPacketReceived( ({ receivedString: name, receivedNumber: value }) => {
|
|
|
if (name == "x") {
|
|
|
X_Accel = value
|
|
|
} else if (name == "y") {
|
|
|
Y_Accel = value
|
|
|
} else if (name == "z") {
|
|
|
Z_Accel = value
|
|
|
write_data()
|
|
|
} else if (name == "done") {
|
|
|
serial.writeLine("done")
|
|
|
basic.showIcon(IconNames.Happy)
|
|
|
}
|
|
|
})
|
|
|
function capture_data() {
|
|
|
X_Accel = input.acceleration(Dimension.X)
|
|
|
Y_Accel = input.acceleration(Dimension.Y)
|
|
|
Z_Accel = input.acceleration(Dimension.Z)
|
|
|
radio.sendValue("x", X_Accel)
|
|
|
radio.sendValue("y", Y_Accel)
|
|
|
radio.sendValue("z", Z_Accel)
|
|
|
}
|
|
|
</pre>
|
|
|
</details>
|
|
|
<details>
|
... | ... | @@ -253,14 +249,14 @@ def capture_data(): |
|
|
radio.send("z"+str(z_Accel))
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
<p/>
|
|
|
|
|
|
* When the A button is pressed, add code to set measuring to true and show a heart on the display. One potential solution to this code section can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
|
|
|
<img src=/uploads/c4abeb7a58bb34047868a4d9572dc642/fit_2.PNG>
|
|
|
<pre>
|
|
|
input.onButtonPressed(Button.A, () => {
|
|
|
measuring = true
|
... | ... | @@ -278,14 +274,14 @@ elif button_a.is_pressed(): |
|
|
display.show(Image.HEART)
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
<p/>
|
|
|
|
|
|
* When the B button is pressed, add code to set measuring to false and show a check mark on the display. One potential solution to this code section can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
|
|
|
<img src=/uploads/b5950957f5c1643187240ccda00de2c6/fit_3.PNG>
|
|
|
<pre>
|
|
|
input.onButtonPressed(Button.B, () => {
|
|
|
measuring = false
|
... | ... | @@ -303,14 +299,14 @@ elif button_b.is_pressed(): |
|
|
display.show(Image.YES)
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
<p/>
|
|
|
|
|
|
* When A and B are pressed together, add code to set measuring to false and display a happy face. One potential solution to this code section can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
|
|
|
<img src=/uploads/33bc3817db5bbdb01b9799054e8edf0c/fit_4.PNG>
|
|
|
<pre>
|
|
|
input.onButtonPressed(Button.AB, () => {
|
|
|
measuring = false
|
... | ... | @@ -330,14 +326,14 @@ if button_a.is_pressed() and button_b.is_pressed(): |
|
|
display.show(Image.HAPPY)
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
<p/>
|
|
|
|
|
|
* Finally, send the values over the radio. The final completed code can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
|
|
|
<img src=/uploads/7212d62c195108a82f1a71eae90021ef/fitTrack.PNG>
|
|
|
<pre>
|
|
|
let Z_Accel = 0
|
|
|
let Y_Accel = 0
|
... | ... | @@ -398,7 +394,6 @@ basic.forever(() => { |
|
|
<pre>
|
|
|
from microbit import *
|
|
|
import radio
|
|
|
|
|
|
radio.on()
|
|
|
radio.config(group=1)
|
|
|
radio.config(power=7)
|
... | ... | @@ -407,7 +402,6 @@ y_Accel = 0 |
|
|
x_Accel = 0
|
|
|
measuring = False
|
|
|
display.show(Image.YES)
|
|
|
|
|
|
def capture_data():
|
|
|
vals = {'x':0, 'y':0, 'z':0}
|
|
|
vals['x'] = accelerometer.get_x()
|
... | ... | @@ -415,12 +409,9 @@ def capture_data(): |
|
|
vals['z'] = accelerometer.get_z()
|
|
|
radio.send("x"+str(x_Accel))
|
|
|
radio.send("y"+str(y_Accel))
|
|
|
radio.send("z"+str(z_Accel))
|
|
|
|
|
|
|
|
|
radio.send("z"+str(z_Accel))
|
|
|
def write_data():
|
|
|
print(x_Accel, y_Accel, z_Accel, sep=',', end='\n')
|
|
|
|
|
|
while True:
|
|
|
if button_a.is_pressed() and button_b.is_pressed():
|
|
|
measuring = False
|
... | ... | @@ -439,8 +430,7 @@ while True: |
|
|
print("done")
|
|
|
elif incoming.startswith('x'):
|
|
|
elif incoming.startswith('y'):
|
|
|
elif incoming.startswith('z'):
|
|
|
|
|
|
elif incoming.startswith('z'):
|
|
|
write_data()
|
|
|
else:
|
|
|
#x_Accel, y_Accel, z_Accel = incoming.split(",")
|
... | ... | @@ -451,7 +441,6 @@ while True: |
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
|
|
|
3. Find a partner that programmed their Micro::Bit in the same language as you (so if you used JavaScript, find someone who also used JavaScript).
|
|
|
|
|
|
4. Begin by choosing who will be the tracker and who will be the hacker. The tracker will be the user of a fitness tracker. They will use their Micro::Bit to collect data about their movements. The other partner, the hacker, will obtain their data.
|
... | ... | |