ADC range = command
ADC range = command
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.
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.
Re: ADC range = command
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")
Re: ADC range = command
Thank you!hippy wrote: ↑Mon Oct 18, 2021 4:14 pmOne way ...Code: Select all
if adc >= 300 and adc <= 600: print("moving") elif adc < 300: print("Less than 300") else: print("Greater than 600")
Re: ADC range = command
This only works for 1 set of ranges if I try to use a second set of ranges I get syntax error.hippy wrote: ↑Mon Oct 18, 2021 4:14 pmOne way ...Code: Select all
if adc >= 300 and adc <= 600: print("moving") elif adc < 300: print("Less than 300") else: print("Greater than 600")
Re: ADC range = command
I tried a different approach:
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>
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>
Re: ADC range = command
You need to use and not 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.
Code: Select all
print(positions())
Code: Select all
print(positions)
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.
Re: ADC range = command
Personally, I would probably rewrite this to look like:
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.
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))
The positions function will treat values like 21500 as stopped, which you may or may not want.
Re: ADC range = command
Thank you so much dhylands! That is working great! I had to do a little tweaking on the ranges but it is working finally.