Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8082

MicroPython • Nextion MicroPython: Variable not recognized with Thonny

$
0
0
hi all,
I have a while True loop that should run as long as the screen is not touched. If the screen is touched, the nex_return function returns OK=1.
When I don't set a condition, it works :

MPY: soft reboot
jbyte: 0
jbyte: 1
jbyte: 2
jbyte: 3
jbyte: 4
rxdata b'e\x00\x07\x01\xff\xff\xff'
tab: [101, 0, 7, 1, 255, 255, 255]
ok 1
jbyte: 5
jbyte: 6

As soon as I set a condition, Thonny gives me an error: "Variable not defined."
The indentation seems correct (checked with Notepad+).

Code:

from machine import UART, Pinfrom utime import sleepfrom nex import * # permet d'importer toutes les fonctions#-----------------------------------------------------------nex_comm = UART(0, 9600, parity=None, stop = 1, bits = 8, tx=Pin(0), rx=Pin(1),timeout=10)#pin0=jaune pin1=bleu du nextion time out 10 ou 100fff = b'\xff\xff\xff'global rxdataglobal tabreturnglobal okdef nex_return():#  ----- receive function -------------------------------------    while True:    #Retrieve commands from Touchpanel    global rxdata    if nex_comm.any():     rxdata=nex_comm.read()     tabreturn=list(rxdata)     print('rxdata',rxdata)     print ('tab:',tabreturn)     ok=1     print ('ok',ok)     break    else :     ok=0     break    return ok#-----------------main loop-------------------------------------------    jbyte=-1  while jbyte<255: #True:    nex_return()    sleep(1)    jbyte=jbyte+1    #conditionnal instruction---------------------------    if ok==1:      print ('ok2',ok)      ok=0    print ('jbyte:', jbyte)
error retourned :
MPY: soft reboot
Traceback (most recent call last):
File "<stdin>", line 35, in <module>
NameError: name 'ok' isn't defined

I'm learning µpython, before I wrote program in Bascom BASIC since years.
thanks for your help
Gustave

Statistics: Posted by gustave — Sun Jun 29, 2025 9:31 am



Viewing all articles
Browse latest Browse all 8082

Trending Articles