
import pyb

def led_dance(delay):
    dots = {}
    control = pyb.Switch(1)
    while True:
        if not control.value():
            dots[pyb.millis() % 25] = 16
        for d in dots:
            pyb.pixel(d, dots[d])
            if dots[d] == 0:
                del(dots[d])
            else:
                dots[d] = int(dots[d]/2)
        pyb.delay(delay)

led_dance(101)


import microbit

def led_dance(delay):
    dots = [ [0]*5, [0]*5, [0]*5, [0]*5, [0]*5 ]
    microbit.display.set_display_mode(1)
    while True:
        dots[microbit.random(5)][microbit.random(5)] = 128
        for i in range(5):
            for j in range(5):
                microbit.display.image.set_pixel_value(i, j, dots[i][j])
                dots[i][j] = int(dots[i][j]/2)
        microbit.sleep(delay)

led_dance(100)

Update for new version of micropython for microbit
Update for new version of micropython for microbit

  
- import pyb
+ import microbit
  
  def led_dance(delay):
-     dots = {}
-     control = pyb.Switch(1)
+     dots = [ [0]*5, [0]*5, [0]*5, [0]*5, [0]*5 ]
+     microbit.display.set_display_mode(1)
      while True:
+         dots[microbit.random(5)][microbit.random(5)] = 128
+         for i in range(5):
+             for j in range(5):
+                 microbit.display.image.set_pixel_value(i, j, dots[i][j])
-         if not control.value():
-             dots[pyb.millis() % 25] = 16
-         for d in dots:
-             pyb.pixel(d, dots[d])
-             if dots[d] == 0:
-                 del(dots[d])
-             else:
-                 dots[d] = int(dots[d]/2)
+                 dots[i][j] = int(dots[i][j]/2)
-         pyb.delay(delay)
+         microbit.sleep(delay)
  
- led_dance(101)
+ led_dance(100)
  
Update for new version of micropython for microbit
## Code Before:

import pyb

def led_dance(delay):
    dots = {}
    control = pyb.Switch(1)
    while True:
        if not control.value():
            dots[pyb.millis() % 25] = 16
        for d in dots:
            pyb.pixel(d, dots[d])
            if dots[d] == 0:
                del(dots[d])
            else:
                dots[d] = int(dots[d]/2)
        pyb.delay(delay)

led_dance(101)

## Instruction:
Update for new version of micropython for microbit
## Code After:

import microbit

def led_dance(delay):
    dots = [ [0]*5, [0]*5, [0]*5, [0]*5, [0]*5 ]
    microbit.display.set_display_mode(1)
    while True:
        dots[microbit.random(5)][microbit.random(5)] = 128
        for i in range(5):
            for j in range(5):
                microbit.display.image.set_pixel_value(i, j, dots[i][j])
                dots[i][j] = int(dots[i][j]/2)
        microbit.sleep(delay)

led_dance(100)
