Raspberry Pi Pico

Well.. it has been a long time since the last post here. I’m still using Raspberry Pi computers and in particular I’m writing this on my pi-topCEED.

I recently bought a couple of Raspberry Pi Pico boards. I didn’t know what to expect, particularly as I have no experience of micropython, but the first impressions are very good. I can’t fault the price – it comes in at less than a Pi Nano and for much of the basic control that I wanted, it looks ideal. It supports NeoPixel style LEDs, LED arrays, a little RTC board that I’d acquired a while back and servos. Cheaper than my usual microcontroller go-to IC, with on-board USB socket for power and download.

I’ve used “Thonny” with the add-in for the Pico, and that works fine, but I’m also experimenting with “BIPES” https://bipes.net.br/beta2/ui/ which seems to offer a good range of facilities. I’m having a couple of issues with variables, but I’m sure that it will soon be sorted.

BIPES is a Blockly-type of programming language, which suits anyone who has learnt Scratch, with the advantage that it supports all sorts of inputs and outputs connecting to those rows of holes or pins down the side of the board.

Watch this space for my experiments!

ASCII selfies

selfieI’ve been experimenting, after a long break, with jp2a again, converting captured images into ASCII representations, which are then sent to a local printer. I remember, long ago and as a child, having my face printed onto a t-shirt in Blackpool. The ASCII character set made up the individual “pixels”.

With this experiment, I’ve used an old Deskjet 930c printer and a cheap USB web cam.

From a software point-of-view, I’ve installed the following:

  • CUPS – The Unix Printing System
  • jp2a – Takes a jpeg image and turns it into ASCII text
  • GUVCVIEW – captures images from a webcam
  • Figlet – to create the text for an interesting banner at the bottom of the page

“lp” is the command that sends this text to the printer. There are loads of command line options available. I’ve wrapped this all up in a bash script and set it to run from the command line by making it executable.

To make this all work, I capture an image and it gets saved as my_photo.jpg. I then run the script shown above, typing ./printphoto.sh and the printer spews out a rendered version of whoever is in front of the camera. Neat… and I’ll demo it at the next Raspberry Jam.

Future options would be to use a big button to start the whole process off. I’ve done it before, but there’s not enough time remaining for that.

Minecraft Molehills?

Here’s a little program for generating random holes in the landscape and automatically putting a candle at the bottom. The idea is that this could be run on the Raspberry Pi running as a server so that other players can have a little fun hunting for them.

I’ve discovered that candles don’t like being put underwater… no surprise there!

Hole generated in Python with a candle at the bottom.

Watch your step – a deep hole created by the python program.

Screenshots have been created following the instructions at Raspberry Pi – Spy.

from mcpi import minecraft
import time
import random
mc = minecraft.Minecraft.create()

mc.postToChat("Python is now in control!")

def Molehole(x,y,z,depth):
    mc.setBlocks(x,y+40,z,x+0,y-depth,z+0,0) #clear the space first
    mc.setBlock(x,y-depth,z,50) #Put a candle at the bottom of the tunnel  


for loop in range(20):
    startx = random.randint(-120,120)
    startz = random.randint(-120,120)
    starty = 0
    Molehole(startx,starty,startz,20)

Poundland Universal Remote Control

I’ve been experimenting with the infra-red input commands on the PICAXE for some simple remote control Lego robots. There is an official remote control for the PICAXE – in an eye-catching blue case, but for economy I’ve bought a bunch of Poundland universal remote controls.

They don’t give the same hardwearing impression that the official ones give – the cases are a little more flimsy, but they do seem more than adequate to remote control a PICAXE and can therefore add a whole new dimension to PICAXE circuits. An infra-red receiver is an easy thing to add and it means that one input can effectively pretend to be many switches – all present remotely on the handset.

To program the remote, press and hold the TV remote for 5 seconds. The LED will light at this point. Type in the code 0495. When this is completed, the LED will go out and the remote is ready to use.

The command to receive the remote signals is to use:

irin [100],3,b0

Assuming that you’re using input 3. I often use the 08M2 Picaxe, and this is fine. The value is dumped into byte variable b0.

Often I use the following program:

main:
    irin [100],3,b0
    debug
    goto main

to see the effect of pressing different buttons in the debug window.

I’ve discovered the following codes  (File: PoundlandUniversalRemote as a PDF file):

PoundlandUniversalRemote

Bluedot – Bluetooth and the 4-tronix Initio Robot.

IMG_1595Some time ago, I wrote about Bluedot – and now I’ve used this to control the Initio robot.

Bluedot consists of two parts – an App and some Python code which runs on the Raspberry Pi. The Initio takes a variety of Raspberry Pi’s, but I recently received some PTFA funding to upgrade to a Pi3 for the club robot so that it would more effectively run Scratch.

Setting up Bluedot is easy – I then took the demonstration program from the Bluedot website and added GPIO commands into the mix.

While my preference is to have robots running autonomously, using sensors to find their way around (And the Initio certainly excels at this), having an App control the motors is certainly fun and a very immediate way of creating a remote control vehicle.

From here? Perhaps a Web cam streaming images to a remote monitor… Watch this space!

 

 


from bluedot import BlueDot
from signal import pause

import time, RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)

def dpad(pos):
    if pos.top:
        print("up")
        print("Both Forward")
        GPIO.output (26, 1)
        GPIO.output (21, 1)
        GPIO.output (19, 0)
        GPIO.output (24, 0)

    elif pos.bottom:
        print("down")
        GPIO.output (26, 0)
        GPIO.output (21, 0)
        GPIO.output (19, 1)
        GPIO.output (24, 1)

    elif pos.left:
        print("left")
        GPIO.output (26, 1)
        GPIO.output (21, 0)
        GPIO.output (19, 1)
        GPIO.output (24, 0)

    elif pos.right:
        print("right")
        GPIO.output (26, 0)
        GPIO.output (21, 1)

    elif pos.middle:
        print("fire")
        GPIO.output (19, 0)
        GPIO.output (21, 0)
        GPIO.output (24, 0)
        GPIO.output (26, 0)

bd = BlueDot()
bd.when_pressed = dpad

pause()

Pi-Hut Christmas Tree

IMG_20171226_201348727_HDR[1]My son was fortunate enough to receive a Pi-Hut Christmas tree for an early Christmas present. He’s only eight, so I wanted to supervise his soldering, but I needn’t have worried – the only problem encountered was when I fitted a missing LED, fitted it to the wrong side, removed it and broke the leg. Fortunately, I had a spare lying around in the shed so it was an easy fix.

The Christmas tree is a lovely kit. Gold plating means that the soldering is effortless and the edges have also been guilded for a bit of luxury. The whole board sits rather neatly on top of a Pi Zero W. I’ve been logging in remotely with SSH, but a development I’d like to try is to use BlueDot to remote control it from a phone.

There is a set of instructions for installing the gpiozero software, although I’m unfamiliar with this – I’ve always used rpi.GPIO in the past with good success. One thing I found missing was a map of all of the LEDs. They’re all numbered, but that doesn’t relate to anything on the pin connections. Mapping the outputs (BCM or Board number) was a bit of a mission but I think I’ve succeeded.

LED

Output (BCM)

Output (Pin)

Star 2 3
1 4 7
2 15 10
3 13 33
4 21 40
5 25 22
6 8 24
7 5 29
8 10 15
9 16 36
10 17 11
11 27 13
12 26 37
13 24 18
14 9 21
15 12 32
16 6 31
17 20 38
18 19 35
19 14 8
20 18 12
21 11 23
22 7 26
23 23 16

24

22

15

The next step I’d like to try is to run this from ScratchGPIO, possibly using SID as a starter so that I can run Scratch on a laptop and transfer it over later. For the meantime, I’ve just got a couple of alternative python programs to play with.

All-in-all, this has been a lovely kit that uses loads of outputs on the PiZero. Soldering is effortless and programming with the standard program supplied was easy. One day I’ll read the docs for GPIOzero, but for now, I’ll be teaching with Scratch.

Merry Christmas and a Happy 2018 to everyone!

 

 

 

 

Using a Raspberry Pi as a desktop PC replacement.

It started with a glass of water… and our old laptop. We probably wouldn’t have used the laptop at all, but it ran Vista which meant that it supported our old-ish Canon Canoscan LiDE scanner. The scanner is, in my view, gorgeous –  it’s light, slim and produces good scans. Sadly, Windows 7 or Windows 10 drivers are unavailable for the scanner. I’m not binning or replacing perfectly good kit, so we’re keeping it!

Unfortunately, a clumsy accident with the glass of water meant that the laptop doesn’t work any more. Just when I needed to do some scanning.

Back in the early days of Raspberry Pi use, I had some minor success with scanning from the command line, but now I know a bit more. I installed XSane, fixed some permissions with the way it handles writing to a folder (can’t remember what, though), and off I went. No problems. The batch mode is especially good for scanning a bunch of photos. 4 at a time and just walk away.

Pi-top CEED

All this was running on my Pi-top CEED. I’d picked this up as a kickstarted and now I’m so pleased with it. I’ve got a wireless keyboard and mouse, and the Raspberry Pi 3 inside really shifts it along well. It boots faster than our Windows 10 laptop and I’m not having to put up with the sound of a fan. It connects with no problem to our NAS and I’ve also managed to pair it with the shared printer.

So, the question remains, what would I install? I’ve got the following, squeezed on to the 8Gb card:

  • CUPS used to connect to a shared printer connected to our laptop. I’ve done this before and found it easier to share than trying to get the now-deceased Vista laptop sharing the printer.
  • XSane to run the scanner. I can scan individual images or a bunch at a time in batch mode.
  • GIMP loads WAY faster on the Pi-top CEED than it does on the Win Laptop. Very useable for the brief experiments I tried.
  • Open Office of course.
  • Seq24 for making music. Runs on my MIDI setup elsewhere in the house, but the Raspberry Pi 3 is also able to run some software synthesizers too. I’ve installed Rosegarden which seems to work well, but I need the time to learn it. It sure looks full-featured.
  • Audacity seems to run well, although I did have it crash once on a file. Not sure what and I didn’t have the chance to try again.
  • Fritzing is a PCB and breadboard design package. Works well and could be handy although I’ve spent so much time in our school’s PCB package that I’m not ready to leave quite yet.

There’s a few other packages that I’m using, but for the most part these are the ones I’ll need for general office-type productivity. My conclusion is that, for me, the Pi-top CEED is an excellent desktop replacement.

I’ll just move the glass of water out of the way…

Game programming in Scratch

xwingpicNot much to write about this time round (seems time is getting even more sparse these days…) but I wanted to post a little game created with my son in Scratch.

Usually, Scratch programming gets put on the back burner – I’ve often got other projects in mind and so I tend to neglect Scratch. This half-term, my son asked for an introduction to programming and so game writing was on the cards. We worked through some simple thoughts and it ended up being inspired by the Star Wars films since he’s quite a fan.

I particularly wanted to show how a simple game can be built up incrementally, starting with simple movement of a space ship and then adding asteroids that have to be dodged. These were given a simple animation of top-to-bottom to give an impression of forward movement.

Once this was working, the detection of hits came in. This was relatively easy – I’m always so impressed at how Scratch offers this as standard, particularly as I’ve tried writing this type of game as a late teenager in Forth on my Jupiter Ace. With that, the nearest one could get was redefining the character set to look like the objects being controlled and then deciding if you’re near enough.

The next stage was to add some kind of missile or torpedo. I know there are a number of ways of solving this but my preferred method was to make the spaceship’s x-position into a global variable (Available to all sprites). This is an excellent introduction to variable types, particularly as we forgot to tick the correct box initially.

A simple game like this is an excellent introduction to programming – it ticks all of the boxes of sequential instructions, conditional branching and iteration, with a generous smattering of local and global variables.

Our little game doesn’t have any custom sound effects, or a timer. These are no real challenge to add.

The final file is available to download for anyone who wants to try it.

Bluedot – Bluetooth Raspberry Pi control

This is something I’ve really been waiting for … a link between a Raspberry Pi and a mobile phone app.

Bluedot consists of two parts – an App and some Python code which runs on the Raspberry Pi. I used a Raspberry Pi Zero W, which is a handy combination of small size and built-in WiFi and Bluetooth.

Setting up Bluedot is easy – just follow the instructions and download the App from the Google App store.

IMG_20170630_094649037_HDR[1]

Bluetooth controlled belt

I’ve been using Bluedot to demonstrate some work for e-Textiles at school. It’s my intention to combine a small Raspberry Pi and power pack into something that can provide a range of patterns and colours controlled from a smart phone. Bluedot has enabled this to happen easily.

Now, here’s where I show my ignorance of Python terminology and correct use of syntax.

As I see it, Bluedot’s documentation points to the library receiving the events and then calling functions depending on what’s been pressed on the smart-phone’s screen.

I wanted to see whether it was possible to display patterns in a background loop and then change these. When I’m doing this with a microcontroller such as the Picaxe, I’d have a main loop reading a variable, in which I had stored the current “state”.  The state would be used to trigger different patterns of outputs. Some where in the main loop I would poll the inputs to see if there’s a change – if there is, I’d change the state variable to a new value. I wasn’t sure if this is the right thing to do within Python, but it has worked for me for at least two hours.

PiZeroPixelController

PiZero controller in 3d printed case.

My demonstration “belt”, which is currently adorning a mannekin, has 34 NeoPixel (WS2812) RGB LEDs connected to a long servo cable. The Pi Zero has a matching connector soldered to the relevant connections and is currently running from a PoundLand USB battery. My program allows the belt colour to be changed to Red, Green, Blue, Purple and random colour sparkles. I’ve used the Pimoroni Unicorn Hat python module to drive the LEDs although I did modify it to allow me to access individual LEDs along the line (Set Single Pixels) rather than relying on figuring out an X/Y matrix.

Overall, I’m pleased. I’m looking forward to some feedback regarding my approach to polling Bluedot rather than relying on events triggering functions. I’m sure there’s a better way but this sort-of suits my understanding as I sit on the fence between different programming languages.

Oh, and by the way, the Glowing Pendant is a ring of LEDs but controlled independently from a Redfern Electronics Crumble Controller.


## Bluetooth control of sparkles ##
from bluedot import BlueDot
from signal import pause
from random import randint
import time
import unicornhat as unicorn

ledno = 34
dim = 150
mode = 0
print("Bluedot Sparkles")

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(1)
width,height=unicorn.get_shape()

def scroll():
    for led in range(ledno):
        unicorn.setsingle(led,0,0,255)
        unicorn.show()
        time.sleep(0.05)

def allred():
    led = randint(0,ledno)
    red = 255
    unicorn.setsingle(led,red,0,0)
    unicorn.show()
    time.sleep(0.1)
    unicorn.setsingle(led,dim,0,0)
    unicorn.show()
    time.sleep(0.1)

def allgreen():
    led = randint(0,ledno)
    green = 255
    unicorn.setsingle(led,0,green,0)
    unicorn.show()
    time.sleep(0.1)
    unicorn.setsingle(led,0,dim,0)
    unicorn.show()
    time.sleep(0.1)

def allblue():
    led = randint(0,ledno)
    blue = 255
    unicorn.setsingle(led,0,0,blue)
    unicorn.show()
    time.sleep(0.1)
    unicorn.setsingle(led,0,0,dim)
    unicorn.show()
    time.sleep(0.1)

def allpurple():
    led = randint(0,ledno)
    red = 255
    blue = 255
    unicorn.setsingle(led,red,0,blue)
    unicorn.show()
    time.sleep(0.1)
    unicorn.setsingle(led,dim,0,dim)
    unicorn.show()
    time.sleep(0.1)

def alloff():
    for led in range(ledno):
        unicorn.set_pixel(led,0,0,0,0)
        unicorn.set_pixel(led,1,0,0,0)
    unicorn.show()

def randomsparkle():
    x = randint(0, (width-1))
    y = randint(0, (height-1))
    r = randint(0,2)*127
    g = randint(0,2)*127
    b = randint(0,2)*127
    unicorn.set_pixel(x, y, r, g, b)
    unicorn.show()
    time.sleep(0.1)

def dpad(pos):
    global mode
    if pos.top:
        alloff()
        print("up")
        mode = 1
    elif pos.bottom:
        alloff()
        print("down")
        allgreen()
        mode = 2

    elif pos.left:
        alloff()
        print("left")
        mode = 3

    elif pos.right:
        alloff()
        print("right")
        mode = 4

    elif pos.middle:
        alloff()
        mode = 5
        print("fire")

print("Starting bluedot")

scroll()
#alloff()
#scroll()
#allred()
#time.sleep(1)
#allblue()

bd = BlueDot()

while True:
    bd.when_pressed = dpad

#    print(mode)

    if mode == 5:
        randomsparkle()
    if mode == 1:
        allred()
    if mode == 2:
        allblue()
    if mode == 3:
        allgreen()
    if mode == 4:
        allpurple()

pause()