user12034971
user12034971

Reputation:

Other ways of creating a delay other than time.sleep() in python

I am doing a bit of python on my casio calculator and i have run into a little problem, the version of python that my calculator uses is microPython 1.9.4. I am not able to use import time as the time module isn't in this version. Any help would be greatly appreciated.

edit: changed version to 1.9.4

My Code (the time.sleep() is near the bottom):

import time
barriers = []
playerPosition = [11, 1]
playTime = 0
while playTime <= 0:
  line = 6
  while line >= 1:
    if line > 2:
      if playerPosition[1] != line:
        print("                     ")
      else:
        currentLine = ""
        xPosition = 1
        while xPosition < playerPosition[0]:
          currentLine = currentLine + " "
          xPosition = 1 + xPosition
        currentLine = currentLine + "|"
        xPosition = 1 + xPosition
        while xPosition < 21:
          currentLine = currentLine + " "
          xPosition = 1 + xPosition
    else:
      obstructions = []
      obstructions.clear()
      if playerPosition[1] == line:
        obstructions.append(playerPosition[0])
      for barrier in barriers:
        obstructions.append(barrier)
      obstructions.sort()
      currentLine = ""
      nextObstruction = 0
      xPosition = 1
      while xPosition <= 21:
        try:
          if xPosition != obstructions[nextObstruction]:
            currentLine = currentLine + " "
          else:
            currentLine = currentLine + "|"
            nextObstruction = 1 + nextObstruction
        except:
          currentLine = currentLine + " "
        xPosition = 1 + xPosition
      print(currentLine)
    line = line - 1
  barrierID = 0
  while barrierID < len(barriers):
    if barriers[barrierID] > 1:
      barriers[barrierID] = barriers[barrierID] - 1
    else:
      barriers.remove(barrierID)
  time.sleep(0.5)
  playTime = 1 + playTime

Upvotes: 2

Views: 1880

Answers (3)

Peter Warrington
Peter Warrington

Reputation: 694

We can make our own custom sleep function using the inbuilt rtc module like so that takes microseconds as a value.

import machine
def sleep(microseconds):
    """sleep(microseconds)
    Delay execution for a given number of microseconds."""
    rtc = machine.RTC()
    rtc.init((0, 0, 0, 0, 0, 0, 0, 0))
    if microseconds < 0:
        raise ValueError("microseconds must not be negative")
    start = rtc.now()[6]
    while rtc.now()[6] - start < microseconds:
        pass

We can then add use this code by removing the import for time and simply using sleep with the value in microseconds, 0.5 seconds being 500000 microseconds.

sleep(500000)

Upvotes: 1

Andreu997
Andreu997

Reputation: 11

If there isn't a time library, you could try a for loop to simulate a delay:

for x in range(10000000):
    pass

Upvotes: 0

Peter Warrington
Peter Warrington

Reputation: 694

Micropython doesn't include the time module, however it does have a module called utime which implements a smaller subset of standard python's time module, luckily including sleep.

So, all you need to do is:

  • change import time to import utime
  • and change time.sleep(0.5) to utime.sleep(0.5)

You can find the documentation for utime at http://docs.micropython.org/en/v1.9.1/pyboard/library/utime.html.

Upvotes: 1

Related Questions