Showing posts with label eggbit. Show all posts
Showing posts with label eggbit. Show all posts

Friday 31 December 2021

Top 10 viewed posts 2021 on the Robot and Physical Computing Blog








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 26 December 2021

Hug Avoider 4 - micropython, Eggbot and speech

The last of the posts on the Hug avoider and the 4Tronix's Eggbit



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. In three previous posts I looked at eggbit using microcode to  produce a hug avoider - warns when people at too close.



In this post using the buttons and adding (via Microbit V2 with its speaker) simple speech


1. Buttons

Pins for the buttons
  • pin8 - Green button
  • pin12 - Red button
  • pin14 - Yellow button
  • pin`6 - Blue button

    if pin12.read_digital()==1:
        #Red Button
        blank_it()
    if pin8.read_digital()==1:
        #Green button
        startingMessage()
    if pin14.read_digital()==1:
        #Yellow button
        rainbow()
    if pin16.read_digital()==1:
        #Blue botton
        display.show(Image.ASLEEP)

2. Speech


    mess1 = [
    "This is the hug avoide",
    "please keep back",
]
# Take from https://microbit-micropython.readthedocs.io/en/latest/tutorials/speech.html
    for line in mess1:
        speech.say(line, speed=120, pitch=100, throat=100, mouth=200)
        sleep(500


The speech is difficult to hear but is fun and there are possibly ways to improve this starting with the information on  https://microbit-micropython.readthedocs.io/en/latest/tutorials/speech.html



3. Overall

from microbit import *
from machine import time_pulse_us
import neopixel, speech

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

def startingMessage():
    mess1 = [
    "This is the hug avoide",
    "please keep back",
]
# Take from https://microbit-micropython.readthedocs.io/en/latest/tutorials/speech.html
    for line in mess1:
        speech.say(line, speed=120, pitch=100, throat=100, mouth=200)
        sleep(500)

def buttonplay():
    if pin12.read_digital()==1:
        #Red Button
        blank_it()
    if pin8.read_digital()==1:
        #Green button
        startingMessage()
    if pin14.read_digital()==1:
        #Yellow button
        rainbow()
    if pin16.read_digital()==1:
        #Blue botton
        display.show(Image.ASLEEP)
  
while True:
    buttonplay()
    dist=howfar()
    if dist>30:
        pin2.write_digital(1)
        pin0.write_digital(0)
        display.show(Image.HAPPY)
    else:
        pin2.write_digital(1)
        pin0.write_digital(1)
        blank_it()
        speech.say("back away please", speed=120, pitch=100, throat=100, mouth=200)
        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

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

Saturday 12 June 2021

4tronix Eggbit - cute and wearable - hug avoider

/


The ever-brilliant 4tronix have produced Eggbit https://shop.4tronix.co.uk/collections/microbit-accessories/products/eggbit; a cute, wearable device, with ultrasonic sensors, buttons and LEDs for the microbit - I couldn't resist.

Programming is through Makecode and an extension is available, to download and install. To use the extension, at the time of writing this, go through the add extension option and use the search box to add it  from https://github.com/4tronix/EggBit



So what to do with it? Inspired by social distancing  (or not being a 'hugger')  created it so that when people get too close the RGB LEDs go out - simple but fun. The distance is set at 20cm for testing. The code is shown below.




It was fun to do and perhaps swapping the settings around so the lights come on when people at too close might be more fun or using sound. This is a lovely device to play with and the look encourages play - just look how cute it is! The price is reasonable and the look means it has the potential to encourage a wide range of people to experiment with it.





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

Top posts on this blog in March 2024

The Top 10 viewed post on this blog in March 2024. Covering areas such as small robots, augmented reality, Scratch programming, robots. Micr...