Microbit virtual pet

I made a virtual pet for microbit.

The microbit is an ARM-based embedded computer system designed by the BBC for use in computer  science education. It comes packed with sensors and a 25-LED display.  The microbit website allows you to program the device and I first discussed it in this post over here.



Today I built a virtual pet. Grab the code here and run it on your microbit or in the simulator.

Microbit virtual pet when started. It is happy.

As with all virtual pets you get to look after and feed a creature within the device. You can feed the creature by holding the 'A' button, and clean up after its waste with the 'B' button.  Holding both buttons together will get the creature to talk to you - it will tell you what it needs.  The creature gets hungry after a certain period of time which can be changed in the 'hungerThreshold' variable.

This virtual pet always does a poo after you have fed it.

Currently that is all, but there is room to improve: The creature does not die from neglect; it always eats even when it is not hungry; it does not complain when you shake it.

Update - 2016-06-29 - The micorbit virtual pet now has a new name: Mike the Microbit, and he will die from neglect.


Improved SenseHat headlines ticker

Last time I introduced my SenseHat RSS feed display code. Today I have made some improvements to the script.

The Sense HAT provides an 8x8 LED maxtrix display, accelerometer, gyroscope, magnetometer, air pressure sensor, temperature sensor and air pressure sensor, as well as a small joystick.  Basically a bundle of sensors that plug in directly to the GPIO pins on your Raspberry Pi. They are well worth purchasing should you wish to upgrade your Pi.



First off, the list 'feedlink' can be populated with as many RSS feeds as you like.  Here I have three BBC feeds, but they could be substituted for any feed you like.  Currently the ticker loops through all of the articles in each feed. You could change it so that each feed is chained to the end of the previous one.  With this code you can switch the the start of the next feed by shaking the Raspberry Pi.  The shake is detected by a change in the 'pitch' of the SenseHat.  You can change the sensitivity of the shake with the THRESHOLD variable.  The new feed will be displayed after the previous article has finished.
The RSS feed ticker scrolling over the SenseHat, however my camera frame rate can't keep up.

I have added some exception handling to the showFeed routine to handle an index out of bounds error. I think this could occur with the previous code.



#Sense Hat RSS reader
#version 2
#For Python 2
from sense_hat import SenseHat
import feedparser
import time


def showFeed(d, n):
    """Shows feed (d) article (n)"""
    try:
        sense.show_message(d.entries[n].description,
                           back_colour=[255,0,0],
                           text_colour=[255,255,255],
                           scroll_speed=0.07)
    except IndexError as e:
        sense.show_message("ERROR")



        
sense = SenseHat()
sense.set_rotation(270)
sense.low_light = True

feed = []
feedlink = ['http://feeds.bbci.co.uk/news/rss.xml?edition=uk',
            'http://feeds.bbci.co.uk/news/technology/rss.xml',
            'http://feeds.bbci.co.uk/news/uk/rss.xml']

ARTICLE_LIMIT = 20
THRESHOLD = 15 # threshold for tilt (changes feed)

print "Running on SenseHat:"
while True:
    #read the feeds in
    for thisFeed in range(len(feedlink)):
        feed.append(feedparser.parse(feedlink[thisFeed]))

    i = 0 #article pointer
    f = 0 #feed pointer
    
    while i < ARTICLE_LIMIT:
        orientation1 = sense.get_orientation_degrees()
        time.sleep(0.5)
        print 'feed ',f,'article',i
        showFeed(feed[f],i)
        orientation2 = sense.get_orientation_degrees()
        #check for shake
        print 'shake detected ',abs(orientation2['pitch'] - orientation1['pitch'])
        if (abs(orientation2['pitch'] - orientation1['pitch'])> THRESHOLD):
            f += 1 #change feed
            i = 0 # return to start of feed
            if (f == len(feed)):
                f = 0
        else:
            i += 1
    time.sleep(2.5)

RSS feed for Raspberry Pi SenseHat

I have written a short script for running a news feed on a Raspberry Pi SenseHat.  The Sense HAT provides an 8x8 LED maxtrix display, accelerometer, gyroscope, magnetometer, air pressure sensor, temperature sensor and air pressure sensor, as well as a small joystick.  Basically a bundle of sensors that plug in directly to the GPIO pins on your Raspberry Pi. They are well worth purchasing should you wish to upgrade your Pi.



The feed picks up the headlines from the BBC news service and then runs continuously on the SenseHat display. Any other valid newsfeed could be substituted for the BBC feed.

RSS feed running on the Raspberry Pi with SenseHat.  I couldn't get a much better photo then this.

Step 1

I used feedparser for the RSS feeds.  This can be installed on your Pi using the following command:

sudo pip install feedparser

Step 2

The following Python 2 code runs an infinite loop which loads the first twenty articles from the BBC website and displays them continuously on the SenseHat display.

#Sense Hat RSS reader
#For Python 2
from sense_hat import SenseHat
import feedparser
import time

sense = SenseHat()
sense.set_rotation(270)
ARTICLE_LIMIT = 20

print "Ticker running on SenseHat"
while True:
    for i in range(ARTICLE_LIMIT):
        time.sleep(0.5)
        d = feedparser.parse('http://feeds.bbci.co.uk/news/rss.xml?edition=uk')
        sense.show_message(d.entries[i].description,
                           back_colour=[255,0,0],
                           text_colour=[255,255,255],
                           scroll_speed=0.07)
        
    time.sleep(2.5)

If you liked this article, then you might like my other SenseHat posts, or my other Raspberry Pi posts.

RetroClinic Datacentre for BBC Micro

The post was going to be called: 'You need to hear about my 1MB RAM disk', or 'Not even the US military have this hardware in their nuclear defence system'.

Yesterday the datacentre arrived for my BBC Master 128.

The datacentre is available from retroclinic.com and is a modern hardware upgrade for your vintage BBC microcomputer.  Setting-up was easy as I chose the 'external' datacentre option which is virtually plug-and-play (although I did need to install the RFS ROM chip).

BBC Master running TimeTrek.  The Datacentre is the 3D-printed box with the red light sitting on top of the disk drive.

Datacentre provides you with a RAM filing system.  This means that you get four virtual floppy disks available to use straight away.  The computer treats these as it would any floppy disk with the advantage of being totally silent and noticeably faster.

You also get one Non-volatile 200K RAM disk.  This works in much the same way as the other four with the added advantage of not losing its contents when the power is switched off.  I am going to use this for commonly-used apps. You can also configure the master to boot into this disk which is very handy.

Probably the most compelling reason to upgrade your beeb with the datacentre is the fact that you can plug a USB flash drive into the USB 2 port.  Your computer sees this as another (sixth) disk drive which you can, if you like, use to store programs and files as with the other disks.  More compelling is that you can transfer any file from your PC onto the USB disk and have your BBC computer read these files.  Even more so is that you can dump BBC disk images - both single-sided and double-sided disks.  I have a 4GB flash disk containing over 600 disk images (and it is only 1% full).  I am reliable informed that people have plugged up to 2TB of solid state storage into their computer.  Enough room for every line of code every written for the beeb.

Once you have a USB drive full of disk images, you can transfer them onto either a floppy disk to run on the computer, or simply copy the image onto one of the four volatile RAM disks.  I spent several hours yesterday reliving the BeeBug magazine disks. You can, of course, run these disk images in an emulator on your PC, but there is nothing like running old software on vintage hardware (and a 32 inch screen).

Copying from a disk image to the RAM filing system is a process that takes one OS command *import -02 <imagefilename> and takes about ten seconds to complete for a double-sided disk.

It is possible to export the contents of a RAM disk back to a disk image for the purposes of sharing your disks or keeping a back-up in the cloud.  In fact the process is so convenient that you play loose and easy with your disks.  No longer are you confined to careful archiving of physical disks; ensuring that the most data is packed onto your precious floppy disk.

Perpetual Calendar from BeeBug by P Brown.  Yay! It doesn't think that it is 1916.
The device also has a USB 'slave' port for direct communication with your PC.  I haven't had time to play with this much yet as I was too busy looking through old disk images.  The device is a fully functional USB 2.0 specification host, and as such, you can use virtually any USB device in it you like, including keyboards, mice, joysticks, etc, but that's a post for a different time.


It is possible to accidentally corrupt your nonvolatile RAM disk, so regular backups to the USB drive are strongly advised.  I managed to do this (with resulting data loss) but fortunately the system comes with a utility to restore the disk.

If you are thinking about purchasing a BBC microcomputer, then the Retroclinic datacentre is an absolute must-have addition, so contact Mark now.

#bbcmicro #bbcmaster #retroclinc #bbcbasic #computers #vintage #floppydisk #beebem

What happens when you let a computer compose music?

What happens when you let a computer compose music?

Well the results are pretty impressive with Jukedeck.  By combining computer science and music composition theory, you can create A.I. generated tracks at the push of a button.


Jukedeck allows you to create a new composition in a few seconds by selecting the genre and mood from a list.  Every track you make is unique so there is no danger of your music appearing elsewhere. It is ideal for the growing army of video bloggers who need some background music for their creations.

Tracks can be used royalty free for individuals and small businesses (fewer than 10 employees). Other licensing options exist, including the option to buy the copyright for your tune.

The free account limits you to five downloads per month, but you can get more for each friend you invite (so click that image now!).

+1 geek experience point for Ed Rex, Jukedeck's founder.

What happens when you let a computer compose art?  
Find out in Modern Art is rubbish.

Label