I have a project using the Plasma 2040 programmed in Python. I have a Python list of 8 items which I wish to step through sequentially for each press of the “Boot” button but to then return to the first item when I reach the last in the list and start again, “ad infinitum” (or at least until I switch Off!). Any thoughts?
Demo below on a method using Pico Display V1 and button A
import time from pimoroni import Button button_a = Button(12) list = ["a","b","c","d","e","f","g","h"] p = -1 while True: if button_a.read(): p = (p + 1) % 8 print(p,list[p])
MicroPython 99c2589 on 2022-07-07; Raspberry Pi Pico W with RP2040 Type "help()" for more information. >>> %Run -c $EDITOR_CONTENT 0 a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 0 a 1 b 2 c 3 d 4 e 5 f
Hope this is what you wanted.
Thank you Tony for a very prompt response and I think this will get me where I want to be. Sorry if it seemed to be a “Noddy” problem but I am still learning Python and have been scratching my head over this for a few days. Thanks again!
p = (p + 1) % 8
Huh, I’d made a mental note to reply to this and had an
if p == 8, p = 0 loop in mind for the roll-over, but using
% 8 is a super-elegant way to do that. You learn something every day!
I only twigged this myself a couple of weeks ago while working on a Neopixel ring and I’ve been coding since 1968!
The beauty of it is that it works in both directions (+1 and -1).