In a previous post Micro:bit and Cube:bit 'says' Hello I introduced the start of me playing with the 4tronix Cube:bit. One of the things I want to try is get the cube to react to music, based around the accelerometers in a micro:bit picking up vibrations. Luckily, in an earlier post, I had done something similar for the Proto-Pic Micro:pixel (see Dancing Pixels for more details).
Essentially the idea is the vibrations from the music shake the micro:bit enough to give measurable changes in three axis, and these values are used to change the pixel's colour - in fact five pixels at a time.
The code shown below is all that was needed:
from microbit import *
import neopixel, random
np = neopixel.NeoPixel(pin0, 125)
while True:
for pxl in range (2,125, 5):
rd=int(abs(accelerometer.get_x())/20)
gr=int(abs(accelerometer.get_y())/20)
bl=int(abs(accelerometer.get_z())/20)
np[pxl] = (rd, gr, 0)
np[pxl-1] = (rd, gr, 0)
np[pxl+1] = (0, gr, rd)
np[pxl-2] = (rd, 0, 0)
np[pxl+2] = (0, gr,0)
np.show()
Here it is in action:
The music used in the video is
Please feel free to improve on this.
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
Robots and getting computers to work with the physical world is fun; this blog looks at my own personal experimenting and building in this area.
Showing posts with label neopixel. Show all posts
Showing posts with label neopixel. Show all posts
Saturday, 1 September 2018
Monday, 7 August 2017
kitronik :Move mini buggy (Python control of LEDs)
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
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
Sunday, 18 December 2016
4Tronix Bit:Bot - now there is a good idea.
When I first heard of this robot, my first thought was what a great idea; a robot with neopixels (I know I should be saying 'smart RGB LEDs' but neopixels is so much more snappier) controlled via a micro:bit.
A good starting point for learning more about this robot, is the details on the 4Tronix site/blog, which includes build guidance and programming instructions for micropython and PXT. Though for the micropython code you might need to change pinX.digital_write() to pinX.write_digital() where X is the pin number.
My play code was to randomly select which neopixels to light up, I didn't include code to turn them off so multiple ones can be on. The robot is driven forwards, waits, backward, waits, turns to the right and then the left; and then repeats.
Code:
from microbit import *
import neopixel, random
np = neopixel.NeoPixel(pin13, 12)
def forward(n):
pin0.write_digital(1)
pin8.write_digital(0)
pin1.write_digital(1)
pin12.write_digital(0)
sleep(n*1000)
def halt(n):
pin0.write_digital(0)
pin8.write_digital(0)
pin1.write_digital(0)
pin12.write_digital(0)
sleep(n*1000)
def backward(n):
pin0.write_digital(0)
pin8.write_digital(1)
pin1.write_digital(0)
pin12.write_digital(1)
sleep(n*1000)
def right_turn(n):
pin0.write_digital(1)
pin8.write_digital(0)
pin1.write_digital(0)
pin12.write_digital(1)
sleep(n*1000)
def left_turn(n):
pin0.write_digital(0)
pin8.write_digital(1)
pin1.write_digital(1)
pin12.write_digital(0)
sleep(n*1000)
while True:
pxl=random.randint(1,11)
rd=random.randint(1,32)
gr=random.randint(1,32)
bl=random.randint(1,32)
t1=random.randint(10,100)
np[pxl] = (rd, gr, bl)
np.show()
forward(1)
halt(1)
backward(1)
halt(1)
right_turn(1)
left_turn(1)
The video below shows it in action, the code is simple but this is a lovely robot to program especially if the mu editor is used.
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
A good starting point for learning more about this robot, is the details on the 4Tronix site/blog, which includes build guidance and programming instructions for micropython and PXT. Though for the micropython code you might need to change pinX.digital_write() to pinX.write_digital() where X is the pin number.
My play code was to randomly select which neopixels to light up, I didn't include code to turn them off so multiple ones can be on. The robot is driven forwards, waits, backward, waits, turns to the right and then the left; and then repeats.
Code:
from microbit import *
import neopixel, random
np = neopixel.NeoPixel(pin13, 12)
def forward(n):
pin0.write_digital(1)
pin8.write_digital(0)
pin1.write_digital(1)
pin12.write_digital(0)
sleep(n*1000)
def halt(n):
pin0.write_digital(0)
pin8.write_digital(0)
pin1.write_digital(0)
pin12.write_digital(0)
sleep(n*1000)
def backward(n):
pin0.write_digital(0)
pin8.write_digital(1)
pin1.write_digital(0)
pin12.write_digital(1)
sleep(n*1000)
def right_turn(n):
pin0.write_digital(1)
pin8.write_digital(0)
pin1.write_digital(0)
pin12.write_digital(1)
sleep(n*1000)
def left_turn(n):
pin0.write_digital(0)
pin8.write_digital(1)
pin1.write_digital(1)
pin12.write_digital(0)
sleep(n*1000)
while True:
pxl=random.randint(1,11)
rd=random.randint(1,32)
gr=random.randint(1,32)
bl=random.randint(1,32)
t1=random.randint(10,100)
np[pxl] = (rd, gr, bl)
np.show()
forward(1)
halt(1)
backward(1)
halt(1)
right_turn(1)
left_turn(1)
The video below shows it in action, the code is simple but this is a lovely robot to program especially if the mu editor is used.
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
Monday, 24 October 2016
Dancing pixels
In previous post I played with using the combination of the Proto-Pic Micro:pixel and Micro:Bit to react, using the accelerometer, to music through a computer speakers. The vibrations from the music shake the Micro:Bit enough to give measurable changes in three axis, and these values are used to change the pixels colour.
The latest version of this uses most of pixels.
Coded in micropython using the Mu editor.
from microbit import *
import neopixel, random
np = neopixel.NeoPixel(pin0, 32)
while True:
for pxl in range (2,32, 5):
rd=int(abs(accelerometer.get_x())/20)
gr=int(abs(accelerometer.get_y())/20)
bl=int(abs(accelerometer.get_z())/20)
np[pxl] = (rd, gr, 0)
np[pxl-1] = (rd, gr, 0)
np[pxl+1] = (0, gr, rd)
np[pxl-2] = (rd, 0, 0)
np[pxl+2] = (0, gr,0)
np.show()
Video of it action, using royalty-free music Electro Deluxe by My Free Mickey feat. Gurdonark. http://dig.ccmixter.org/files/myfreemickey/48180
As one of the reviewers on Proto-Pic site states, the box the Micro:pixel comes in does make a good diffuser.
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
The latest version of this uses most of pixels.
Coded in micropython using the Mu editor.
from microbit import *
import neopixel, random
np = neopixel.NeoPixel(pin0, 32)
while True:
for pxl in range (2,32, 5):
rd=int(abs(accelerometer.get_x())/20)
gr=int(abs(accelerometer.get_y())/20)
bl=int(abs(accelerometer.get_z())/20)
np[pxl] = (rd, gr, 0)
np[pxl-1] = (rd, gr, 0)
np[pxl+1] = (0, gr, rd)
np[pxl-2] = (rd, 0, 0)
np[pxl+2] = (0, gr,0)
np.show()
Video of it action, using royalty-free music Electro Deluxe by My Free Mickey feat. Gurdonark. http://dig.ccmixter.org/files/myfreemickey/48180
As one of the reviewers on Proto-Pic site states, the box the Micro:pixel comes in does make a good diffuser.
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
Saturday, 22 October 2016
Disco micro:pixel
In a previous post Proto-Pic board, Microbit and Micropython I played with the Proto-Pic micro:pixel 4x8 NeoPixel board.
This post is just a short description of a quick play with making it flashing blocks of different colours across the board. The routine produces five random numbers (three to define the colours, one for which pixel is selected and the last for the delay each iteration). The idea of being - a pixel is selected, but so are the ones either side of it, each one has a different combination of the colour values, but only two of the pixels are turned off after the delay.
from microbit import *
import neopixel, random
# Setup the Neopixel strip on pin0 with a length of 2 pixels
np = neopixel.NeoPixel(pin0, 32)
while True:
pxl=random.randint(1,30)
rd=random.randint(1,32)
gr=random.randint(1,32)
bl=random.randint(1,32)
t1=random.randint(10,100)
np[pxl] = (rd, gr, bl)
np[pxl-1] = (gr, bl, rd)
np[pxl+1] = (bl, rd, gr)
np.show()
sleep(t1)
np[pxl] = (0, 0, 0)
np[pxl+1] = (0, 0, 0)
The video below shows the routine in action. There is no connection between the pixels and the music on the video - but making the connection between music and the pixels would be an interesting project.
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
Thursday, 20 October 2016
micro:pixel board, micro:bit and micropython
A new (or to me at least) addition to devices you can attach a Micro:Bit to, is the Proto-Pic micro:pixel 4x8 NeoPixel board; essentially a board with 4 x8 grid of NeoPixels that you plug the Micro:Bit into. Following the advice of the website the values of RGB are all set to 32 or below to avoid pulling too much power. Pin0 is used to write to. You will need to use the Mu editor for this.
Two tests were tried
Example 1: To get the pixels to appear to light up from the last to the first one.
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin0, 32)
while True:
for x in range(0, 32):
for y in range(0, (32-x)):
np[y] = (y, 32-y, 1)
if (y>0):
np[y-1]=(0,0,0)
np.show()
sleep(30)
Example 2: To randomly select a pixel and its colour.
from microbit import *
import neopixel, random
np = neopixel.NeoPixel(pin0, 32)
while True:
pxl=random.randint(0,31)
rd=random.randint(1,32)
gr=random.randint(1,32)
bl=random.randint(1,32)
np[pxl] = (rd, gr, bl)
np.show()
sleep(500)
np[pxl] = (0, 0, 0)
This is a good, fun board to play with; relatively easy to use.
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
Two tests were tried
Example 1: To get the pixels to appear to light up from the last to the first one.
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin0, 32)
while True:
for x in range(0, 32):
for y in range(0, (32-x)):
np[y] = (y, 32-y, 1)
if (y>0):
np[y-1]=(0,0,0)
np.show()
sleep(30)
Example 2: To randomly select a pixel and its colour.
from microbit import *
import neopixel, random
np = neopixel.NeoPixel(pin0, 32)
while True:
pxl=random.randint(0,31)
rd=random.randint(1,32)
gr=random.randint(1,32)
bl=random.randint(1,32)
np[pxl] = (rd, gr, bl)
np.show()
sleep(500)
np[pxl] = (0, 0, 0)
This is a good, fun board to play with; relatively easy to use.
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
Wednesday, 3 August 2016
Traffic lights - Microbit, GlowBugs and micropython
In a previous post, I got a GlowBug to work with a micro:bit (http://robotsandphysicalcomputing.blogspot.co.uk/2016/08/microbit-and-glowbug.html) . In this post, I will show a relatively simple traffic lights system produced by turning off and on the pixels via a micro:bit.
Code
from microbit import *
import neopixel
# Setup the Neopixel strip on pin0 with a length of 3 pixels
np = neopixel.NeoPixel(pin0, 3)
while True:
#red
np[0] = (255, 0, 0)
np[1] = (0,0, 0)
np[2] = (0,0,0)
np.show()
sleep(1000)
#red and orange
np[0] = (255, 0, 0)
np[1] = (255, 69, 0)
np[2] = (0,0,0)
np.show()
sleep(1000)
#green only
np[0] = (0, 0, 0)
np[1] = (0, 0, 0)
np[2] = (0,255,0)
np.show()
sleep(1000)
#orange
np[0] = (0, 0, 0)
np[1] = (255, 69, 0)
np[2] = (0,0,0)
np.show()
sleep(1000)
It is simple, timings and more lights can be added to make a more interesting system. If you have done something similar please use the comments to discuss or link to it.
Thank you to @SCC_Lancaster for the loan of a micro:bit.
Related Posts
Microbit and GlowBugs
CodeBug and Glowbugs
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.
Code
from microbit import *
import neopixel
# Setup the Neopixel strip on pin0 with a length of 3 pixels
np = neopixel.NeoPixel(pin0, 3)
while True:
#red
np[0] = (255, 0, 0)
np[1] = (0,0, 0)
np[2] = (0,0,0)
np.show()
sleep(1000)
#red and orange
np[0] = (255, 0, 0)
np[1] = (255, 69, 0)
np[2] = (0,0,0)
np.show()
sleep(1000)
#green only
np[0] = (0, 0, 0)
np[1] = (0, 0, 0)
np[2] = (0,255,0)
np.show()
sleep(1000)
#orange
np[0] = (0, 0, 0)
np[1] = (255, 69, 0)
np[2] = (0,0,0)
np.show()
sleep(1000)
It is simple, timings and more lights can be added to make a more interesting system. If you have done something similar please use the comments to discuss or link to it.
Thank you to @SCC_Lancaster for the loan of a micro:bit.
Related Posts
Microbit and GlowBugs
CodeBug and Glowbugs
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.
Subscribe to:
Posts (Atom)
Remote Data Logging with V1 Microbit
In an earlier post https://robotsandphysicalcomputing.blogspot.com/2024/08/microbit-v1-datalogging.html a single microbit was used to log ...