0

I’m trying to implement a clock/timer. It works as of now but only problem is I need to make the frameRate to 1 but it affects the whole program’s frameRate. How do I change frameRate only for the clock function?

def clock():
global sec, minutes, hours, col
    
sec+=1
if(sec == 60):
    sec = 0
    minutes+=1
if(minutes == 60):
    minutes = 0
    hours+=1
if(hours == 24):
    hours = 0
    minutes = 0
    sec = 0
        
textSize(25)
fill(255, 0, 0)
text(floor(sec), 185, 110)
text(floor(minutes), 135, 110)
text(floor(hours), 85, 110)

if(sec % 2 == 0):
    col = color(0)
else:
    col = color(255, 0, 0)

fill(col)
textSize(30)
text(":", 120, 110)
text(":", 170, 110)
Xen1212
  • 11
  • 3

1 Answers1

0

You can't change the frame rate only for one function because it doesn't make sense: The draw() function of processing is called in a loop at a defined frame rate (let's say that it's fixed at 60 times by second even thought in reality it can change). When you use the frameRate() function to change this value you change how fast the draw() function is called and since it is the one calling all your other functions you can't define that only for a specific function.

However you have other ways to achieve your clock/timer function:

First processing provides several time functions:

  • millis() returns the number of milliseconds since the program started. You could have you clock() function called by draw() make make it convert millis() to a number of seconds, minutes, hours, etc... This way you don't have to keep track of the time by yourself which will simplify your code a lot.

  • Depending on what you want to do you can also access your computer clock with second(), minute() and all the functions in the "Time & Date" section here.

Secondly you could use the time module of python as shown in this SO question it's a bit of the equivalent of the millis() idea but with native python function.

Finally, still depending on your needs, you could want to have a look at python's Timer objects to execute your clock() function at a defined interval outside of the draw() loop, but while it is completely possible it is not straight forward and can be tricky for someone new to programming.

statox
  • 2,827
  • 1
  • 21
  • 41