My Account

Wish List (0)


The PiBorg store is closed and we are unable to ship orders.
You will not be able to checkout any orders at this time.

DiddyBorg v2 - Examples - Disco Mode

Written by in Build, DiddyBorg V2 - Build on .

Add some flair to your DiddyBorg v2 with lights.

Robots are great for doing tasks or remote control, but we also want a robot that looks good. The DiddyBorg v2 looks pretty awesome already so how can we improve on it? How about making use of the RGB LED fitted inside.

In this example we will make the LED cycle the whole spectrum of colours :)

Parts

All we need for this example is the DiddyBorg v2 itself, no other bits are necessary :)

Get the example

The example is part of the standard set of DiddyBorg v2 examples installed during the getting started instructions: bash <(curl https://www.piborg.org/installer/install-diddyborgv2.txt)

How it works

The example sequence works by first generating a list of numbers between 0 and 299. It uses these values to determine what colour the LED should show:

  • 0 to 99 is between red and green
  • 100 to 199 is between green and blue
  • 200 to 299 is between blue and red

Each value is then used to generate a mix between the two colours for that range. Values which are closer to each end are closer to the corresponding end colour. For example:

  • 0 is 100% red, 0% green → RED
  • 49 is 50% red, 50% green → YELLOW
  • 149 is 50% green, 50% blue → CYAN
  • 224 is 75% blue, 25% red → PURPLE

As we cycle through the values this produces a colour spectrum:

Running with other examples

You should be able to run this example at the same time as our other examples but there is a catch, most of our examples already control the LED! You can get around this problem by modifying the other script so it does not control the LED at all. This is fairly simple to do, you just need to comment out any of the lines which change the LED in any way, for example changing

TB.SetLeds(red, green, blue)

to

#TB.SetLeds(red, green, blue)

The commands you need to comment out are:

  • TB.SetLed1 Changes the ThunderBorg LED colour
  • TB.SetLed2 Changes the ThunderBorg lid LED colour (not on DiddyBorg v2)
  • TB.SetLeds Changes the LED colour for both LEDs
  • TB.SetLedShowBattery Enables or disables the LED battery monitoring mode

Programming a new sequence

To change the LED pattern you can change the code inside the loop which works out the colour levels. For example this is the standard pattern which cycles through the spectrum:

        # Loop over a set of different hues:
        for hue in range(300):
            # Get hue into the 0 to 3 range
            hue /= 100.0
            # Decide which two channels we are between
            if hue < 1.0:
                # Red to Green
                red = 1.0 - hue
                green = hue
                blue = 0.0
            elif hue < 2.0:
                # Green to Blue
                red = 0.0
                green = 2.0 - hue
                blue = hue - 1.0
            else:
                # Blue to Red
                red = hue - 2.0
                green = 0.0
                blue = 3.0 - hue
            # Set the chosen colour for both LEDs
            TB.SetLeds(red, green, blue)
            # Wait a short while
            time.sleep(0.01)

and here is a changed version which makes the LED pulse red instead:

        # Loop over a set of different brightness levels:
        for brightness in range(200):
            # Get hue into the 0 to 2 range
            brightness /= 100.0
            # Decide if we are increasing or decreasing
            if brightness < 1.0:
                # Increasing
                red = brightness
                green = 0.0
                blue = 0.0
            else:
                # Decreasing
                red = 2.0 - brightness
                green = 0.0
                blue = 0.0
            # Set the chosen colour for both LEDs
            TB.SetLeds(red, green, blue)
            # Wait a short while
            time.sleep(0.01)

You can build up sequences as long or short as you like. You can also change the speed by changing the value given to the time.sleep function, larger values run slower and smaller values run faster.

Run once

Go to the DiddyBorg v2 code directory: cd ~/diddyborgv2 and run the script: ./diddy2LedWave.py

Run at startup

Open /etc/rc.local to make an addition using: sudo nano /etc/rc.local Then add this line just above the exit 0 line: /home/pi/diddyborgv2/diddy2LedWave.py & Finally press CTRL+O, ENTER to save the file followed by CTRL+X to exit nano. Next time you power up the Raspberry Pi it should start the script for you :)

Full code listing - diddy2LedWave.py

#!/usr/bin/env python
# coding: Latin-1

# Import library functions we need
import ThunderBorg
import time
import sys

# Setup the ThunderBorg
TB = ThunderBorg.ThunderBorg()     # Create a new ThunderBorg object
#TB.i2cAddress = 0x15              # Uncomment and change the value if you have changed the board address
TB.Init()                          # Set the board up (checks the board is connected)
if not TB.foundChip:
    boards = ThunderBorg.ScanForThunderBorg()
    if len(boards) == 0:
        print 'No ThunderBorg found, check you are attached :)'
    else:
        print 'No ThunderBorg at address %02X, but we did find boards:' % (TB.i2cAddress)
        for board in boards:
            print '    %02X (%d)' % (board, board)
        print 'If you need to change the I²C address change the setup line so it is correct, e.g.'
        print 'TB.i2cAddress = 0x%02X' % (boards[0])
    sys.exit()

# Disable the colour by battery level
TB.SetLedShowBattery(False)

# Loop over the sequence until the user presses CTRL+C
print 'Press CTRL+C to finish'
try:
    while True:
        # Loop over a set of different hues:
        for hue in range(300):
            # Get hue into the 0 to 3 range
            hue /= 100.0
            # Decide which two channels we are between
            if hue < 1.0:
                # Red to Green
                red = 1.0 - hue
                green = hue
                blue = 0.0
            elif hue < 2.0:
                # Green to Blue
                red = 0.0
                green = 2.0 - hue
                blue = hue - 1.0
            else:
                # Blue to Red
                red = hue - 2.0
                green = 0.0
                blue = 3.0 - hue
            # Set the chosen colour for both LEDs
            TB.SetLeds(red, green, blue)
            # Wait a short while
            time.sleep(0.01)
except KeyboardInterrupt:
    # User has pressed CTRL+C, set the LEDs to battery monitoring mode
    TB.SetLedShowBattery(True)
    print 'Done'
Last update: Aug 02, 2019

Related Article

Related Products

Comments

Leave a Comment

Leave a Reply

The product is currently Out-of-Stock. Enter your email address below and we will notify you as soon as the product is available.