Skip to content

Commit

Permalink
arduino fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
beckf committed May 8, 2024
1 parent fe443b6 commit edaddb7
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,24 @@ def __init__(self):
self.ultrasonic_last_check = 0.00

def read_data(self, val):
# don't try more than 3 times
for i in range(0, 2, 1):
self.ser.reset_input_buffer()
# don't try more than 4 times
for i in range(0, 3, 1):
try:
count = 0
data = None
while data is None and count <= 3:
count = count + 1
data = self.ser.readline()
except:
return None
d = data.decode()
if d[0] == "$":
d = d[1:]
dd = d.split("|")
if val == "ultrasonic":
return [dd[0], dd[1], dd[2], dd[3]]
# is this needed?
self.ser.reset_input_buffer()
data = self.ser.readline()
d = data.decode()
if d[0] == "$":
d = d[1:]
dd = d.split("|")
if val == 0:
return [dd[0], dd[1], dd[2], dd[3]]
else:
return dd[int(val)]
else:
return dd[int(val)]
else:
# not a new line from arduino, try again
# not a new line from arduino, try again
continue
except:
continue
return "error"

Expand Down Expand Up @@ -73,9 +70,6 @@ def get_ultrasonic(self):
Reads the ultrasonic sensors from the Arduino. Returns the ultrasonic as
:return: list
"""
try:
self.ultrasonic_last_check = time.time()
ints = [int(x) for x in self.read_data("ultrasonic")]
return ints
except:
return [0, 0, 0, 0]
self.ultrasonic_last_check = time.time()
ints = [int(x) for x in self.read_data(0)]
return ints

0 comments on commit edaddb7

Please sign in to comment.