Raxis777
Posts: 9
Joined: Tue Aug 24, 2021 8:06 pm

one element delay in loop

Tue Sep 21, 2021 4:54 pm

I would like to ask if it is possible to delay one command in a loop (print) without delaying other commands in the code? My code is based on actions in milliseconds, and I would like to print the result every e.g. 5 seconds.

Code: Select all

import time
import Adafruit_GPIO.SPI as SPI
import MAX6675.MAX6675 as MAX6675
def c_to_f(c):
return c * 9.0 / 5.0 + 32.0
CLK = 22
CS = 27
DO = 23
czujnik = MAX6675.MAX6675(CLK, CS, DO)
x=1
y=0
try:
while True:
temp = czujnik.readTempC()
print("Temperatura termopary: {0:0.3F}˚C ".format(temp, c_to_f(temp)))
GPIO.output(13,x)
time.sleep(0.1)
GPIO.output(13,y)
time.sleep(0.05)
GPIO.output(6,x)
time.sleep(0.1)
GPIO.output(6,y)
time.sleep(0.05)
if temperatura > temperatura_max:
exit(0)
except:
GPIO.cleanup()

User avatar
Paeryn
Posts: 3355
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: one element delay in loop

Tue Sep 21, 2021 6:00 pm

The code you posted is not valid Python because you haven't got any indentation where indentation is required. Also you use GPIO but at no point has GPIO been defined/imported.

I don't think you want to "delay" the print as that would have unwanted implications. I think what you want is to only execute the print if it has been at least 5 seconds since it last printed. Something like

Code: Select all

time_last_printed = time.monotonic() - 5
while True:
    temp = czujnik.readTempC()
    time_now = time.monotonic()
    if time_now - time_last_printed >= 5:
        time_last_printed = time_now
        print("Temperatura termopary: {0:0.3F}˚C ".format(temp, c_to_f(temp)))
    GPIO.output(13,x)
    time.sleep(0.1)
    # etc.
The initial setting of time_last_printed to be 5 seconds before the current time is so that the print will happen on the first iteration of the loop. The function time.monotonic() returns a time in seconds (floating point) that is guaranteed to never go backwards, adjusting your date/time whilst your program is running won't affect the calculation.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

Raxis777
Posts: 9
Joined: Tue Aug 24, 2021 8:06 pm

Re: one element delay in loop

Tue Sep 21, 2021 6:34 pm

Paeryn wrote:
Tue Sep 21, 2021 6:00 pm
The code you posted is not valid Python because you haven't got any indentation where indentation is required. Also you use GPIO but at no point has GPIO been defined/imported.

I don't think you want to "delay" the print as that would have unwanted implications. I think what you want is to only execute the print if it has been at least 5 seconds since it last printed. Something like

Code: Select all

time_last_printed = time.monotonic() - 5
while True:
    temp = czujnik.readTempC()
    time_now = time.monotonic()
    if time_now - time_last_printed >= 5:
        time_last_printed = time_now
        print("Temperatura termopary: {0:0.3F}˚C ".format(temp, c_to_f(temp)))
    GPIO.output(13,x)
    time.sleep(0.1)
    # etc.
The initial setting of time_last_printed to be 5 seconds before the current time is so that the print will happen on the first iteration of the loop. The function time.monotonic() returns a time in seconds (floating point) that is guaranteed to never go backwards, adjusting your date/time whilst your program is running won't affect the calculation.
This is exactly what I was looking for, thank you for your help :D I forgot to add defined GPIOs here, but i have it in my code. I don't really understand what u meant with indentations in python code, could you explain it little more?

User avatar
Paeryn
Posts: 3355
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: one element delay in loop

Tue Sep 21, 2021 10:23 pm

Raxis777 wrote:
Tue Sep 21, 2021 6:34 pm
I don't really understand what u meant with indentations in python code, could you explain it little more?
If you look at your post there is no indentation present in the Python code at all. Python depends on indentation to delimit blocks of code, for example in your post you have this...
Raxis777 wrote:
Tue Sep 21, 2021 4:54 pm

Code: Select all

while True:
temp = czujnik.readTempC()
As you can see, the second line is indented to the same level as the line above (neither have any indentation) which means that there aren't any lines of code in the loop. It's an IndentationError if a block of code is syntactically expected but no indented lines of code follow (unless the block is written on the same line as the statement requiring it, but don't do that).

I'm sure that on your RPi you have indentation (else it wouldn't run), but since you posted the code without any indentation then we're left to guess what lines are in which blocks, there's nothing to show where the while loop ends or where the function c_to_f ends etc.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

Raxis777
Posts: 9
Joined: Tue Aug 24, 2021 8:06 pm

Re: one element delay in loop

Thu Sep 23, 2021 3:11 pm

Paeryn wrote:
Tue Sep 21, 2021 10:23 pm
Raxis777 wrote:
Tue Sep 21, 2021 6:34 pm
I don't really understand what u meant with indentations in python code, could you explain it little more?
If you look at your post there is no indentation present in the Python code at all. Python depends on indentation to delimit blocks of code, for example in your post you have this...
Raxis777 wrote:
Tue Sep 21, 2021 4:54 pm

Code: Select all

while True:
temp = czujnik.readTempC()
As you can see, the second line is indented to the same level as the line above (neither have any indentation) which means that there aren't any lines of code in the loop. It's an IndentationError if a block of code is syntactically expected but no indented lines of code follow (unless the block is written on the same line as the statement requiring it, but don't do that).

I'm sure that on your RPi you have indentation (else it wouldn't run), but since you posted the code without any indentation then we're left to guess what lines are in which blocks, there's nothing to show where the while loop ends or where the function c_to_f ends etc.
Ok, now i get it. Yes i have indentations in my code, but didn't copy it here. Anyway, thank you for your help, it works perfect now :)

Return to “Python”