ESP32 MicroPython: Non-blocking Delays and Multithreading | Multitasking

![nonblock and multithreading]1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 import machine import time red = machine.Pin(27, machine.Pin.OUT) grn = machine.Pin(26, machine.Pin.OUT) blu = machine.Pin(25, machine.Pin.OUT) mode = machine.Pin(33, machine.Pin.IN, machine.Pin.PULL_UP) left = machine.Pin(32, machine.Pin.IN, machine.Pin.PULL_UP) rght = machine.Pin(35, machine.Pin.IN) entr = machine.Pin(34, machine.Pin.IN) r_start = time.ticks_ms() g_start = time.ticks_ms() b_start = time.ticks_ms() k_start = time.ticks_ms() r_interval = 300 g_interval = 500 b_interval = 700 k_interval = 200 state = 0 EDIT_RESOLUTION = 10 reset = 0 print('**************************') print(' DEFAULT Interval Values ') print('--------------------------') print('Red interval:', r_interval, 'ms') print('Grn interval:', g_interval, 'ms') print('Blu interval:', b_interval, 'ms') print('**************************') while True: if time.ticks_ms() - r_start >= r_interval: red.value( not red.value() ) r_start = time.ticks_ms() if time.ticks_ms() - g_start >= g_interval: grn.value( not grn.value() ) g_start = time.ticks_ms() if time.ticks_ms() - b_start >= b_interval: blu.value( not blu.value() ) b_start = time.ticks_ms() if time.ticks_ms() - k_start >= k_interval: k_start = time.ticks_ms() if mode.value()==0: if state==0: # idle mode state = 1 print() print('*************') print('Red edit mode') print('-------------') elif state==1: # red edit mode state = 2 print() print('*************') print('Grn edit mode') print('-------------') elif state==2: # grn edit mode state = 3 print() print('*************') print('Blu edit mode') print('-------------') elif state==3: # blu edit mode state = 0 print() print('*************') print('Idle mode') print('-------------') if left.value()==0: if state==1: # red edit mode if r_interval - EDIT_RESOLUTION > 0: r_interval -= EDIT_RESOLUTION print('Red interval:', r_interval, 'ms') elif state==2: # grn edit mode if g_interval - EDIT_RESOLUTION > 0: g_interval -= EDIT_RESOLUTION print('Grn interval:', g_interval, 'ms') elif state==3: # blu edit mode if b_interval - EDIT_RESOLUTION > 0: b_interval -= EDIT_RESOLUTION print('Blu interval:', b_interval, 'ms') if rght.value()==0: if state==1: # red edit mode r_interval += EDIT_RESOLUTION print('Red interval:', r_interval, 'ms') elif state==2: # grn edit mode g_interval += EDIT_RESOLUTION print('Grn interval:', g_interval, 'ms') elif state==3: # blu edit mode b_interval += EDIT_RESOLUTION print('Blu interval:', b_interval, 'ms') if entr.value()==0: r_interval = 300 g_interval = 500 b_interval = 700 print() print('**************************') print('Values RESETTED to DEFAULT') print('--------------------------') print('Red interval:', r_interval, 'ms') print('Grn interval:', g_interval, 'ms') print('Blu interval:', b_interval, 'ms') print('**************************') https://1.bp.blogspot.com/-rYdcCBcd2K0/X26oBPSJclI/AAAAAAAACDc/jXve0NeUYHcXtYScTmdbod5hMptYgfJnwCLcBGAsYHQ/w640-h322/MP_009_Time.png ↩︎ ...

2022-03-07 · 3 min · 442 words · Jeapo

Welcome to Typecho

If you are seeing this post, it means your blog has been successfully installed.

2020-11-18 · 1 min · 14 words · Jeapo

About

About the Website No grand introductions here, just bits and pieces Article List Click the title to view the list of articles User Registration If any friends wish to register, click the title to sign up. Welcome to share your thoughts and experiences. About Myself Verbally, my logic struggles to keep up with my thoughts. I prefer practical work over idle speculation and value genuine experiences over empty actions. I enjoy tinkering with new computer-related technologies, both hardware and software. Over the past three years, I’ve built an integrated system—from sensors to microcontrollers to servers—as part of my thesis. I dislike competing for superficial recognition but still want to prove myself. I’ve participated in two mathematical modeling competitions, never achieving the top rank, but the results weren’t bad either. ...

2020-11-18 · 3 min · 606 words · Jeapo