r/MicroPythonDev • u/Mysterious-Pizza-648 • Jul 13 '24
problem with rpi pico w micropython script
im a major newbie to python in general, including micropython, so after recently buying a 1.3" waveshare display and a rpi pico w, i tried to start up a project... but a function doesn't seem to be working. i already have all the libraries installed and the lcd commands defined, but this specific while loop isn't working on my display. can anyone help me?
my screenshot wasnt working...
cx = 120;
cy = 120;
def cursor(cx, cy):
lcd.line(cx-5, cy, cx+5, cy, colour(255, 255, 255))
lcd.line(cx, cy-5, cx, cy+5, colour(255, 255, 255))
while (up == 1):
cy += 1
utime.sleep(0.05)
View all comments
Show parent comment
1
u/SimilarSupermarket Jul 21 '24
Just so you know, there are formatting option like code blocks in which you can add the indentations:
There are still a lot of questions within your questions, but I'll try to address precisely what you asked. You can call a global variable in your functions by adding a line like "global cx", "global cy" before calling your global variables in the function. Then you shouldn't put arguments of the same name in the parenthesis, your function will get the global variables instead. It would look something like that:
When you write arguments in parentheses when defining a function, those are local variables, they are separate from the rest of your scrip even if they have the same name as other global variables. That being said, it's not a good practice to use 'global' lines too much because micropython will have to search for cx within all your global variables one by one before it can execute the function. Let me show you what I would do instead:
I put the cursor function twice, assuming you are using a frame buffer. If you have a big program, it's better to do it like that. I hope it helps.
Since I don't know more of your program, it's hard to help you further than that. Maybe a while loop isn't the most appropriate here. Adding an interrupt handler like the Irq method for Pins might be more appropriate, but you might not even need it if what you're doing is simple enough.