[sdiy] Need some advice for Raspberry PICO + Micropython
francesco mulassano
francesco.mulassano at gmail.com
Fri Jun 24 19:04:21 CEST 2022
hi!
I need some advice for a project that i've started to learn RP2040 and
Micropython
HARDWARE
RP PICO
3 position switch
1 button
some leds
___
to save Pins, the switch is connected to a single ADC input and through a
comparator, i have 3 different voltage values that i use to identify the
switch position.
I use the switch to activate 3 different modes
MODE 0, MODE 1 and MODE 2
MODE 0
is a program selection function
when in this modality, the button serve to select the 'APP', indicated by a
led in a 3x3 matrix
selected app is lit.
MODE 1
in this mode, the selected app RUNS!
MODE 2
reserve
___
What can I do
- I do the detect of the MODE
- when I am in MODE 0, I call the appSelect function and with the button, i
cycle on the LEDs
*the problem*
-
*when I go back to MODE 1, it should leave the select mode and run the
selected APP but unfortunately the appSelect remains active*
___
The CODE
*main.py*
> from machine import ADC
> import utime
> import _thread
> import appselect_module
> mode = 0
>
> def readVoltage():
> analogValue = ADC(26)
> return analogValue.read_u16() / 65536 * 3.3
>
> def main():
> global mode
>
> while True:
> if mode == 1:
> print("MODE 1")
>
> utime.sleep(2)
> if mode == 2:
> print("MODE 2")
> utime.sleep(2)
> if mode == 0:
> print("MODE 0" + str(mode))
> utime.sleep(1)
> appselect_module.appSelect(mode)
>
> def mode():
> global mode
>
> while True:
>
> reading = readVoltage()
> print(reading)
>
> if reading > 1 and reading < 3:
> """
> MODE 1: App run
> """
> print("MODE 1")
> mode = 1
> utime.sleep(1)
> elif reading < 1:
> """
> MODE 2: Riserva
> """
> print("MODE 2")
> mode = 2
> utime.sleep(1)
> else:
> """
> MODE 0: App selection
> """
> print("MODE 0 + " + str(0))
> mode = 0
> utime.sleep(1)
> _thread.start_new_thread(mode, ())
> while True:
> main()
*appselect_module.py*
> import machine
> import time
> import _thread
> from primitives.pushbutton import Pushbutton
> import uasyncio as asyncio
> import hal
> import functions
> prev_app = 5
> next_app = 0
> total_apps = 6
> mode = 0
> functions.allLedOff()
> machine.Pin(hal.leds[prev_app]).low()
> def readVoltage():
> analogValue = ADC(26)
> return analogValue.read_u16() / 65536 * 3.3
> def appSelect(mode):
> global mode
> global next_app
> while mode == 0:
> print("SONO IN APP SELECT")
> def app_select():
> global prev_app
> global next_app
> global total_apps
> print("SONO IN APP SELECT e MODE " + mode)
> machine.Pin(hal.leds[prev_app]).low() #program precedente
>
> if prev_app == 0:
> machine.Pin(hal.leds[total_apps]).high()
> machine.Pin(hal.leds[0]).high()
> else:
> machine.Pin(hal.leds[prev_app]).high()
>
> next_app = prev_app + 1
> machine.Pin(hal.leds[next_app]).low()
> if next_app >= total_apps:
> prev_app = 0
> else:
> prev_app = next_app
>
> def app_confirm():
> print("confirm")
>
> def button():
>
> pb = Pushbutton(hal.button)
> pb.press_func(app_select, ()) # Callback on button press
> pb.long_func(app_confirm, ()) # Callback on long press
> while True:
> await asyncio.sleep(1)
>
> try:
> asyncio.run(button())
> finally:
> asyncio.new_event_loop()
Thanks for the help!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://synth-diy.org/pipermail/synth-diy/attachments/20220624/a43440fc/attachment.htm>
More information about the Synth-diy
mailing list