Skip to content

Commit cf6f706

Browse files
committed
Fixed errors
1 parent 44fd33a commit cf6f706

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

CPBoxing_Glove_Tracker/code.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
# SPDX-License-Identifier: MIT
44
import time
55
import ssl
6-
import board
76
import math
7+
import board
8+
import microcontroller
89
import wifi
910
import socketpool
1011
import adafruit_minimqtt.adafruit_minimqtt as MQTT
@@ -22,7 +23,6 @@
2223
aio_username = secrets["aio_username"]
2324
aio_key = secrets["aio_key"]
2425

25-
2626
# Wi-Fi
2727
try:
2828
print("Connecting to %s" % secrets["ssid"])
@@ -34,8 +34,6 @@
3434
time.sleep(5)
3535
microcontroller.reset()
3636

37-
38-
3937
# Create a socket pool
4038
pool = socketpool.SocketPool(wifi.radio)
4139

@@ -51,28 +49,21 @@
5149
# Initialize Adafruit IO MQTT "helper"
5250
io = IO_MQTT(mqtt_client)
5351

54-
def connected(client):
55-
print("Connected to Adafruit IO!")
56-
57-
58-
# Set up the callback methods above
59-
io.on_connect = connected
60-
6152
try:
6253
# If Adafruit IO is not connected...
6354
if not io.is_connected:
6455
# Connect the client to the MQTT broker.
6556
print("Connecting to Adafruit IO...")
6657
io.connect()
67-
58+
print("Connected to Adafruit IO!")
6859
except Exception as e: # pylint: disable=broad-except
69-
print("Failed to get or send data, or connect. Error:", e,
70-
"\nBoard will hard reset in 30 seconds.")
71-
time.sleep(30)
72-
microcontroller.reset()
60+
print("Failed to get or send data, or connect. Error:", e,
61+
"\nBoard will hard reset in 30 seconds.")
62+
time.sleep(30)
63+
microcontroller.reset()
7364

7465
threshold = 25 # set threshold value here
75-
time_interval = 0.1 # set the time interval in seconds
66+
time_interval = 0.5 # set the time interval in seconds
7667

7768
# create the I2C bus object
7869
i2c = board.STEMMA_I2C()
@@ -82,29 +73,18 @@ def connected(client):
8273
battery_monitor = LC709203F(i2c)
8374
battery_monitor.pack_size = PackSize.MAH400
8475

85-
86-
# initialize velocity variables to zero
87-
velocity_x = 0
88-
velocity_y = 0
89-
velocity_z = 0
90-
9176
t0 = time.monotonic()
9277

9378
while True:
9479
x, y, z = accelerometer.acceleration
9580
t1 = time.monotonic()
9681
dt = t1 - t0
97-
velocity_x += x*dt
98-
velocity_y += y*dt
99-
velocity_z += z*dt
10082

10183
total_acceleration = math.sqrt(x**2 + y**2 + z**2)
10284
if total_acceleration >= threshold:
10385
print("Battery Percent: {:.2f} %".format(battery_monitor.cell_percent))
10486
print("Collision strength: %.2f" % total_acceleration)
105-
print("Velocity X: %.2f Y: %.2f Z: %.2f \n" % (velocity_x, velocity_y, velocity_z))
10687
io.publish("punch-strength", total_acceleration)
107-
io.publish("punch-velocity", velocity_x)
10888

10989
# add code here to trigger an event or alert the user
11090
t0 = t1

0 commit comments

Comments
 (0)