Programming the Rainbow

Starting programming

Once they have connected to the Vanguard Shell of their Vanguard Rainbow kit, we encourage learners to immediately type…

setPixel(0, red)

…and see the first pixel turn red. This immediately prompts feverish experimentation with colors and positions to achieve particular results. Most mainstream colors are predefined. Pixel numbers range from 0 to 7 inclusive.

This simple foundation is a catalyst to introduce key concepts through the interactive shell, like loops, function definitions, named values and collections.

See instructions to connect to the shell over Wifi or over USB

Further simple commands

The Vanguard Rainbow kit is shipped with a main.py startup routine on the Vanguard’s internal filesystem. It configures the pixel strip, defines  useful values and commands, flashes a rainbow, then hands over control to the built-in python shell.

Once connected to the shell, learners can issue commands like the following;

  • red : reports the value of red; [255,0,0]
  • setPixel(1, red) : sets the second pixel to red
  • clearPixels() : turns off all pixels
  • yellow = [255,255,0] : assigns a color value to the name yellow
  • setPixel(2, yellow) : sets the third pixel to the color yellow
  • setPixel(3, [255,255,0]) : sets the fourth pixel to the color yellow
  • red = hueToRgb(0) : red is at the start of the ‘rainbow’ color wheel
  • red = hueToRgb(1) : red is also at the end of the color wheel
  • yellow = hueToRgb(0.1666667) : yellow is part-way through the color wheel

More complex commands

You can create groups of commands which are repeated (a for loop) or which you can invoke with a simple name (a function). Note the colon at the end of the block definitions below, followed by indented commands which are part of the loop or function block. You must remove all indentation from the line which follows the commands in your block. This indicates the block has finished.

You can program simple loops like this…

for pos in range(0, 8):
    setPixel(pos, hueToRgb(pos/8))

You can define a function to set all the pixels at once…

def setAll(color):
    for pos in range(0, 8): 
        setPixel(pos, color) 

…and then run the function you just defined like…

setAll(green)

Prepared Routines

A collection of prepared functions are available…

  • paintSpectrum() : colors the 8 pixels using a rainbow ‘colour wheel’ using hue_to_rgb() with the first pixel at hueOffset and hueSeparation between each pixel, defaulting to 0, 1/8
  • rotateSpectrum() : repeats paintSpectrum() forever, but incrementing the starting hue by hueChange after changeDelay seconds, defaulting to 0.02, 0.02 (press CTRL+C to halt the routine and resume the shell).

You can see all the names defined by the startup routine by hitting the TAB key from the shell prompt (the chevrons >>>).

For example if you hit TAB immediately after powerup, you will see this list;

MicroPython v1.9.3-8-g63826ac5c on 2017-11-01; ESP module with ESP8266
Type "help()" for more information.
>>>
ai              setPixel        accept_handler  showPixels
Pin             pixels          red             teal
sleep           darken          white           webrepl
NeoPixel        WS2811          bdev            RBG
WS2812          black           replpath        glimpseSpectrum
addr            hueToRgb        hsbToRgb        fillSpectrum
orange          numPixels       pink            blue
RGB             rotateSpectrum  purple          changeDelay
os              hueSeparation   hueChange       clearPixels
wheel           socket          startPixels     __name__
green           darkFactor      paintSpectrum   hueOffset
PL9823          uos             gc              yellow
GRB             s

See the source code for our Neopixel library and for the Rainbow project’s default main.py

Changing the Startup Regime

Learners can create their own regime and replace our main.py startup routine with their own file. They can use this example to get started, which paints a rainbow on the neopixel strip…

from vgkits.project.rainbow.paint import *
for pos in range(0, 8):
    setPixel(pos, hueToRgb(pos/8))

Configuring a new module

The modules we ship are ‘flashed’ with new firmware as documented here. For example running vanguard brainwash vanguard+rainbow puts the following onto the board …

  • the latest version of Micropython , providing the Python3 interpreter and shell
  • a collection of libraries supporting classroom exercises with neopixels
  • a startup routine which launches the wifi shell and flashes a rainbow on power up

The rainbow flash proves the devices are powered and properly configured before returning control to the shell for learners to code. This process takes less than a second from power-up. The shell has immediate access to colors and pixel operations and tab-autocompletion to help learners know what procedures and values are available to them.

Be the first to comment

Leave a Reply

Your email address will not be published.


*