2

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
  • 1
    Instead of importing `time`, does importing `utime` work? – Peter Warrington Sep 07 '19 at 15:38
  • 2
    The corresponding micropython module is ``utime``: https://docs.micropython.org/en/latest/library/utime.html Just doing Replacing ``import time`` with ``import utime as time`` should be enough. – MisterMiyagi Sep 07 '19 at 15:42

3 Answers3

1

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.

Peter Warrington
  • 654
  • 10
  • 32
  • That doesn't seem to work. I am told that there is no module named utime. i have just realised that i am running micropython 1.9.4 not 1.9.1, which may be why it isn't working. If so, are there any other ways? –  Sep 07 '19 at 15:46
1

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)
Peter Warrington
  • 654
  • 10
  • 32
0

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

for x in range(10000000):
    pass