Help on syntax and flow

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
shriphoenix
 
Posts: 3
Joined: Thu Jul 22, 2021 12:43 pm

Help on syntax and flow

Post by shriphoenix »

Hi,
I am from C/C++ background and newbie to python.

I have written a code, need your help to solve the issue...
he below code if written in While True: super loop, no issues at all.. but error comes when i put it inside blinkLED() function... below i the code:

Code: Select all

import time
import board
import digitalio

intervalLED = 0.5
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
ledState = False
previousMillisLED = 1
currentMillisLED = 1

def blinkLED():
    currentMillisLED = time.monotonic()

    if currentMillisLED - previousMillisLED >= intervalLED:
        previousMillisLED = currentMillisLED

        if ledState:
            print("YES")
            ledState = False
        else:
            print("NO")
            ledState = True
        led.value = ledState


while True:
    blinkLED()
I get following error:
py.jpg
py.jpg (155.95 KiB) Viewed 59 times
Please help me to correct the code..

Regards,

Shri

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Help on syntax and flow

Post by dastels »

Your code sets the value of both currentMillisLED and previousMillisLED. In Python, variables are created when first assigned to in a specific lexical scope. when the code is directly in the loop, that scope is the global scope and all is well. However in blinkLED, the scope if the blinkLED function. This is fine for currentMillisLED since it is limited to the function; creating a new instance each time the function is executed isn't a problem. It is a problem for previousMillisLED however since it has to exist and retain it's value from call to call. There are two ways to fix this.

The simplest, though often frowned upon is to declare previousMillisLED as a global variable. This will cause the assignment to assign to the global previousMillisLED and not create a new instance. Do this at the start of the function:

Code: Select all

def blinkLED():
    global previousMillisLED
    currentMillisLED = time.monotonic()
Another way which is functionally sounder, but more cumbersome and potentially not as obvious is to pass in the global previousMillisLED and return an updated value for it (I've changed the name in the function to avoid confusion):

Code: Select all

import time
import board
import digitalio

intervalLED = 0.5
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
ledState = False
previousMillisLED = 1
currentMillisLED = 1

def blinkLED(previousMillis):
    currentMillisLED = time.monotonic()

    if currentMillisLED - previousMillis >= intervalLED:

        if ledState:
            print("YES")
            ledState = False
        else:
            print("NO")
            ledState = True
        led.value = ledState
        return currentMillisLED
    return previousMillis

while True:
    previousMillisLED = blinkLED(previousMillisLED)
Dave

User avatar
shriphoenix
 
Posts: 3
Joined: Thu Jul 22, 2021 12:43 pm

Re: Help on syntax and flow

Post by shriphoenix »

Hi Dave,
Your 2nd approach solved most of the issues however, ledState error still exists.. any solution??
py_1.jpg
py_1.jpg (65.3 KiB) Viewed 54 times
Regards,

Shri

User avatar
shriphoenix
 
Posts: 3
Joined: Thu Jul 22, 2021 12:43 pm

Re: Help on syntax and flow

Post by shriphoenix »

Hi Dave,

I changed the code like below.. now everything is working...

Code: Select all

# Write your code here :-)
import time
import board
import digitalio

intervalLED = 0.5
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
ledState = False
previousMillisLED = 1
currentMillisLED = 1

def blinkLED():
    global previousMillisLED
    global ledState
    
    currentMillisLED = time.monotonic()

    if currentMillisLED - previousMillisLED >= intervalLED:
        previousMillisLED = currentMillisLED

        if ledState:
            print("YES")
            ledState = False
        else:
            print("NO")
            ledState = True
        led.value = ledState


while True:
    blinkLED()
Thank you very much Dave....

Regards,

Shri

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”