Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line sensor analog code #50

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading