Showing posts with label leds. Show all posts
Showing posts with label leds. Show all posts

Thursday, 23 December 2021

Hug Avoider 3 - experiments with Python and 4Tronix Eggbit

via GIPHY


4Tronix's Eggbit (in fact I bought three of them 
https://shop.4tronix.co.uk/collections/bbc-micro-bit/products/eggbit-three-pack-special  :-) recently) is a cute add-on for the microbit (see above). In two previous posts I looked at eggbit using microcode to  produce a hug avoider - warns when people at too close.

This post replicates some of this, but this time using Python and shows the stages of the build
  • Get the ultrasound to find the distance;
  • Produce smile and surprise on the eggbit's 'mouth';
  • Produce rainbow on the neopixels or all the pixels turning red;
  • Bring it all together so if the person is too close, less than 30cm it reacts.

 
1. Ultrasonic detection

Probably the most challenging bit of this was getting the ultrasonic distance measrement working. It actually is not that difficult; especially using code from  https://firialabs.com/blogs/lab-notes/ultrasonic-distance-sensor-with-python-and-the-micro-bit as the basis of the solution and pin15 does both triggering and receiving. Code sends a pulse out, picked up  and processed to get the distance from the delay. The code is shown below:

from microbit import *
from machine import time_pulse_us

sonar =pin15
sonar.write_digital(0)

while True:
    sonar.write_digital(1)
    sonar.write_digital(0)
    
    timeus=time_pulse_us(sonar,1)
    echo=timeus/1000000
    dist=(echo/2)*34300
    sleep(100)
    display.scroll(str(dist))


2. LEDs
To get a greater understanding of how 4Tronix's makecode extension (used in the previou posts) for the Eggbit controls the various pins the best resource was to reverse engineering the code  from https://github.com/4tronix/EggBit/blob/main/eggbit.ts in their github respository for the Eggbit. 

This gave the colours and the correct pin for the LEDs the code is shown below. Producing a rainbow method and a method to set the LEDs/neopixels to red. 

import neopixel


fireled=neopixel.NeoPixel(pin13,9)

def rainbow():
    fireled[0] = (255, 0, 40)
    fireled[1]=  (255,165,0)
    #block=yellow
    fireled[2] = (255,255,0)
    #block=green
    fireled[3] = (0,255,0)
    #block=blue
    fireled [4] = (0,0,255)
    # block=indigo
    fireled[5] = (75,0,130)
    # block=violet
    fireled[6] = (138,43,178)
    #block=purple
    fireled[7] = (255,0,255)
    fireled.show()
    
def blank_it():
    for j in range(8):
        fireled[j] = (63, 0, 0)
    fireled.show()



3. 'Face'
Eggbit has set of LEDs that represent a mouth, controlled via three pins. Only two of those are used in this example

'Smile' is pin2.write_digital(1) the 'lower' part of the mouth and turn off upper part of the mouth pin0.write_digital(0)

'Surprise' uses both parts 
pin2.write_digital(1)
pin0.write_digital(1)


4. Overall

So putting this altogether 

from microbit import *
from machine import time_pulse_us
import neopixel

sonar =pin15
sonar.write_digital(0)
fireled=neopixel.NeoPixel(pin13,9)

def rainbow():
    fireled[0] = (255, 0, 40)
    fireled[1]=  (255,165,0)
    #block=yellow
    fireled[2] = (255,255,0)
    #block=green
    fireled[3] = (0,255,0)
    #block=blue
    fireled [4] = (0,0,255)
    # block=indigo
    fireled[5] = (75,0,130)
    # block=violet
    fireled[6] = (138,43,178)
    #block=purple
    fireled[7] = (255,0,255)
    fireled.show()
    
def blank_it():
    for j in range(8):
        fireled[j] = (63, 0, 0)
    fireled.show()

def howfar():
    sonar.write_digital(1)
    sonar.write_digital(0)
    
    timeus=time_pulse_us(sonar,1)
    echo=timeus/1000000
    dist=(echo/2)*34300
    sleep(100)
    return dist

while True:
    dist=howfar()
    if dist>30:
        pin2.write_digital(1)
        pin0.write_digital(0)
        rainbow()
        display.show(Image.HAPPY)
    else:
        pin2.write_digital(1)
        pin0.write_digital(1)
        blank_it()
        display.show(Image.ANGRY)



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, 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

Tuesday, 23 August 2016

UFO has landed

CBiS Education generously sent me two of their new range of robotics development kits - BinaryBots (https://www.binarybots.co.uk/makers.aspx), these are a range of cardboard based kits (so far a robot and a UFO) with electronic components for example LEDs; sensors and buzzers,  depending on the kits. What makes the kits interesting though is they are designed to be controlled by either by a BBC Micro:bit or a CodeBug.

This blog documents, briefly, an initial play with the UFO kit (see below) using a Micro:Bit for control. 


The UFO model came together readily, the instructions were fairly easy to follow. Personally, a feature I especially liked about the model was the LEDs being both on the top and bottom of it - increasing its usefulness. CBiS EducationThey have also provided a webpage / portal with some example projects and code. 




My first project I built, was to pulse the LEDs on and off (one set of LEDs on Pin 0, the other on Pin 1). Pin 2 was connected to the buzzer, so produce a low buzz every few seconds. The code below is written using the Block Editor (https://www.microbit.co.uk/create-code)

The video below shows the LEDs pulsing. I do need to decorate the UFO though!




Looking forward to playing with it further.

Related
http://robotsandphysicalcomputing.blogspot.co.uk/2016/08/ufo-detects-light.html


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.

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.

Who wants to produce AI produced cartoon strips

Question: How easy is it produce a comic/cartoon using genetative AI? Let's start with  using ChatGPT4o to produce cartoons. The idea wa...