ADC range = command

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
jp171
Posts: 5
Joined: Mon Oct 18, 2021 2:02 pm

ADC range = command

Post by jp171 » Mon Oct 18, 2021 2:12 pm

I'm pretty new to programming so this may be simple but I cant figure it out yet.

I'm attempting to make small ranges of adc signal (from one input) do different functions.
I know the ranges and what I want each to trigger but I'm not sure how I make the pico do them.
ie. if adc=(300-600):
print("moving")
Any help would be very appreciated.

hippy
Posts: 130
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: ADC range = command

Post by hippy » Mon Oct 18, 2021 4:14 pm

One way ...

Code: Select all

if adc >= 300 and adc <= 600:
    print("moving")
elif adc < 300:
    print("Less than 300")
else:
    print("Greater than 600")

jp171
Posts: 5
Joined: Mon Oct 18, 2021 2:02 pm

Re: ADC range = command

Post by jp171 » Wed Oct 20, 2021 5:32 pm

hippy wrote:
Mon Oct 18, 2021 4:14 pm
One way ...

Code: Select all

if adc >= 300 and adc <= 600:
    print("moving")
elif adc < 300:
    print("Less than 300")
else:
    print("Greater than 600")
Thank you!

jp171
Posts: 5
Joined: Mon Oct 18, 2021 2:02 pm

Re: ADC range = command

Post by jp171 » Thu Oct 21, 2021 2:16 pm

hippy wrote:
Mon Oct 18, 2021 4:14 pm
One way ...

Code: Select all

if adc >= 300 and adc <= 600:
    print("moving")
elif adc < 300:
    print("Less than 300")
else:
    print("Greater than 600")
This only works for 1 set of ranges if I try to use a second set of ranges I get syntax error.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ADC range = command

Post by dhylands » Thu Oct 21, 2021 6:00 pm

How are you coding your second set of ranges?

jp171
Posts: 5
Joined: Mon Oct 18, 2021 2:02 pm

Re: ADC range = command

Post by jp171 » Thu Oct 21, 2021 6:01 pm

I tried a different approach:

Code: Select all

while True:
    reading = adc_read.read_u16()
    
    def postions(position):
        if reading >= 39000 and reading <= 40100:
            position = "lower"
        elif reading >= 20800 and reading <= 21000:
            position = "right"
        elif reading >= 22000 and reading <=23200:
            position = "left"
        elif reading >= 30000 and reading <= 32000 :
            bullet = "lift"
        else:
            position = "stopped"
        return position
  print(postions)

But when I run it it doesnt print what I want its:
<function postions at 0x20013370>
<function postions at 0x20013380>
<function postions at 0x20013390>
<function postions at 0x200133a0>
<function postions at 0x200133b0>
<function postions at 0x200133c0>
<function postions at 0x200133d0>
<function postions at 0x200133e0>
<function postions at 0x200133f0>
<function postions at 0x20013400>

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ADC range = command

Post by dhylands » Thu Oct 21, 2021 6:09 pm

You need to use

Code: Select all

print(positions())
and not

Code: Select all

print(positions)
Having the () causes the function positions to be called and the return value is printed. The second example doesn't call the function positions but rather prints the function object (like a function pointer)

Also your 4th check doesn't assign the variable position, but rather assigns the variable bullet which means that position will wind up being None in that particular code path.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ADC range = command

Post by dhylands » Thu Oct 21, 2021 6:17 pm

Personally, I would probably rewrite this to look like:

Code: Select all

def postions(position):
    if reading >= 20800 and reading <= 21000:
        return "right"
    if reading >= 22000 and reading <= 23200:
        return "left"
    if reading >= 30000 and reading <= 32000:
        return "lift"
    if reading >= 39000 and reading <= 40100:
        return "lower"
    return "stopped"

while True:
    reading = adc_read.read_u16()
    print(postions(reading))
Defining the function inside the while loop isn't good. You're basically redefining the function for each iteration of the loop, which while legal doesn't make sense. You also need to pass the reading into positions when you call it.

The positions function will treat values like 21500 as stopped, which you may or may not want.

jp171
Posts: 5
Joined: Mon Oct 18, 2021 2:02 pm

Re: ADC range = command

Post by jp171 » Fri Oct 22, 2021 9:23 am

Thank you so much dhylands! That is working great! I had to do a little tweaking on the ranges but it is working finally.

Post Reply