0

Trying to code a function to read the ADCs on my PICO W using the latest uPython version and also the latest Thonny IDE version.

this is the code for the function:

def getADCvalue(ADCchannel):
value = 0
match ADCchannel:
    case 0:
        value = machine.ADC(26).read_U16()
    case 1:
        value = machine.ADC(27).read_U16()
    case 2:
        value = machine.ADC(28).read_U16()
return value

this is not about the ADC read is more about the Pithon syntax. is the first time I'm using the match case syntax and can't really get what the issue is, this is the error I get:

Traceback (most recent call last): File "", line 55 SyntaxError: invalid syntax

that is the line for the match ADCchannel:

not sure what is complaining about... I pass a integer parameter defining which ADC I want to read and then depending on the case statement trigger read the ADC.

any thoughts will be apreciated.

Best Regards EngineBeat.

JIST
  • 1,139
  • 2
  • 8
  • 30
Enginebeat
  • 23
  • 1
  • 1
  • 7
  • 1
    Are you sure you are using a version of Python that supports the `match` statement? There's no real reason to use `match` instead of `if ADCchannel == 0: ... elif ADCchannel == 1: ... elif ADCchannel == 2: ...`; the `match` statement is not more efficient, and checks each case in order just as the `if-elif` statement does. – chepner Apr 01 '23 at 18:00
  • If you are sure that `ADCchannel` cannot have a value other than 0, 1, or 2, you might consider a single `return` statement like `return machine.ADC(26 + ADCchannel).read_U16()`. If not, you can check the value of `ADCchannel` before using it in such an expression. – chepner Apr 01 '23 at 18:01

0 Answers0