In two previous posts I looked at control the :Move buggy using JavaScript Blocks or Python. In this post we are going to look at controlling the LEDs using Python (or more accurately micropython).
Pin 0 controls the LEDs, they are based on5 NeoPixel compatible, RGB, addressable LEDs; so the Neopixel protocols (and library for Neopixels) can be used.
Code First five colours of the rainbow. The array lig holds the RGB settings for the rainbow colours (more details on the RGB colours can be found at Lorraine Underwood's Halloween Cloud project). In the code below, the five LEDs have a different colour allocated to them.
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin0, 5)
lig=[[255,0,0],[255,127,0],[255,255,0],[0,255,0],[0,0,255],[75,0,136],[139,0,255]]
while True:
np[0] = lig[0]
np[1] = lig[1]
np[2] = lig[2]
np[3] = lig[3]
np[4] = lig[4]
np.show()
Code to cycle through the rainbow
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin0, 5)
lig=[[255,0,0],[255,127,0],[255,255,0],[0,255,0],[0,0,255],[75,0,136],[139,0,255]]
count1=1
count0=0
count2=2
count3=3
count4=4
while True:
np[0] = lig[count0]
if count0>=6:
count0=0;
else:
count0=count0+1
np[1] = lig[count1]
if count1>=6:
count1=0;
else:
count1=count1+1
np[2] = lig[count2]
if count2>=6:
count2=0;
else:
count2=count2+1
np[3] = lig[count3]
if count3>=6:
count3=0;
else:
count3=count3+1
np[4] = lig[count4]
if count4>=6:
count4=0;
else:
count4=count4+1
np.show()
sleep(500)
All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon
Very informative!!!!
ReplyDeleterobot kit
This comment has been removed by a blog administrator.
ReplyDelete