Skip to content

Commit

Permalink
line sensor analog code (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartenPostma authored Nov 22, 2024
1 parent 85b4618 commit 15a089e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions leaphymicropython/sensors/linesensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""A module for the new leaphy line sensor"""
from leaphymicropython.utils.pins import read_pin
from leaphymicropython.utils.pins import read_analog


def read_line_sensor(pin: int) -> bool:
Expand All @@ -9,3 +10,28 @@ def read_line_sensor(pin: int) -> bool:
:return: sensor_state: gives a value from the module
"""
return read_pin(pin)


def calibrate_analog_line_sensor(
pin: int, below_white: int, above_black: int, test="yes"
):
"""
Maps analog line sensor output to white | black | unclear
:param pin: int, the pin to set
:param below_white: int, below this value, the color is white
:param above_black: int, above this value, the color is black
:param test: str, if set to 'yes', the value and the mapped value are shown in the shell
:return: color: white | black | unclear
"""
value = read_analog(pin)
color = "unclear"
if value < below_white:
color = "white"
elif value > above_black:
color = "black"

if test == "yes":
print(value, color)
return color
2 changes: 1 addition & 1 deletion leaphymicropython/utils/pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ def read_analog(pin: int) -> int:
"""
pin = pin_to_gpio(pin)
adcpin = ADC(Pin(pin))
return round(adcpin.read_u16() / 257)
return adcpin.read_u16()

0 comments on commit 15a089e

Please sign in to comment.