Disco
Invent Logo

Your challenge now is to create a dancing 'disco robot'. With precise control of the timings of your code and the movements of the motors, you can make your robot dance along with a favourite song.

Disco
Dancing robot - movements

We can only play short sounds on our robot speaker and the sound quality isn't great, so you'll be using a favourite song from the internet and creating dance moves!

You'll mainly be using the movement blocks, the delay block and loop blocks to create your movements and you'll need to do lots of tests to make minor changes of your delays to perfectly synchronise with your music track.

LEDs

Let's now create some disco lights, for this you need to attach the obstacle sensor.

  • There are 8 LEDs (labelled 0 to 7) on the obstacle sensor.
  • Turning them all on brightly will create an impressive light show but it will drain your batteries more quickly.
  • You can even create a happy or sad face by turning on just some of the lights!

You can set the LEDs numbered 0-7 using the facenp list. A list is a variable that can store multiple values. In this example our list stores the colours each of the 8 LEDs.

In this example I set LED 0 to the colour red.

Try this yourself before moving to the next task.

red = (255,0,0)
facenp[0]=red
facenp.write()
Colours

All colours are represented as numbers. This is called RGB as the 3 numbers in brackets indicates the amount of red, green and blue the LED will show.

Look at this code to see how to create the colours.

Try making more colours by changing the 3 numbers. Remember the numbers must be in the range 0-255.

Now lets see how to use these colours to switch on all the LEDs

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)

facenp[0]=green
facenp.write()
Lists

The facenp list is used to control each individual LED. Lists can also be used to store strings. Take a look at this example.

The items in the list start at 0. If you want the to get "candycane" out of the mySweets list, you use mySweets[2]. Similaly, "bubblegum" is mySweets[0].

mySweets = ["bubblegum", "chocolate", "candycane"]
All LEDs

Using the code from the previous example, try lighting up all the LEDs on your robot in snazzy colours!


All LEDs Code

In this example, I made all the LEDs on the robot green, and just a single one blue.

Try experimenting with lots of different colours!

green = (0,255,0)
blue = (0,0,255)
facenp[0]=green
facenp[1]=green
facenp[2]=green
facenp[3]=green
facenp[4]=green
facenp[5]=blue
facenp[6]=green
facenp[7]=green
facenp.write()
Flashing Ring

Now lets try and have a moving light on the ring. We will have a white ring with a red LED moving around.

We want this code to run run forever, so use the while loop you used previously to do this task.

In each loop set an LED to red. Then after a delay, set the colour back to white.

Hint: Use a variable to keep track of which LED you are going to control. During each loop we only need to control a single LED.

Look at the next page if you get stuck!

Flashing Ring code

Before we start the loop, we need to set a variable "LEDcount" to 0. This will keep track of the LED we need to control.

During each loop we need to set current LED to red, then set it back to white after a short delay. This will create the effect that the red light is moving in a circle.

At the end of the loop we need to add 1 to "LEDcount" so on the next loop we are controlling the next LED, we need to be careful though. The highest LED number is 7, so we need to use an if statement to check this number has not been reached. When it has, we set the number back to 0.

import time
from invent import *

red = (255,0,0)
white = (255,255,255)


LEDcount = 0
while True:
  facenp[LEDcount]=red
  facenp.write()
  time.sleep(0.5)
  facenp[LEDcount]=white
  facenp.write()
  LEDcount+=1
  if LEDcount>7:
    LEDcount = 0
Rainbow ring

Lets make the flashing ring better by turning it into a rainbow!

Start by creating a ring of rainbow coloured LEDs.

To do this use the code we wrote previously, but change it to make each LED a different colour of the rainbow.

To get a selection of different colours, check out this website: https://www.rapidtables.com/convert/color/hex-to-rgb.html

Rainbow Ring Code

Here is the initial rainbow code!

Here we simply set the LEDs to every colour of the rainbow.

Now lets make it spin!

import time
from invent import *

red = (255,0,0)
orange = (255,102,0)
yellow = (255,255,51)
green = (0,153,0)
blue = (51,255,255)
indigo = (0,0,153)
violet = (102,0,204)
pink = (204,51,204)

facenp[0]=red
facenp[1]=orange
facenp[2]=yellow
facenp[3]=green
facenp[4]=blue
facenp[5]=indigo
facenp[6]=violet
facenp[7]=pink
facenp.write()
    
Moving Rainbow Ring

To make the rainbow spin, each LED needs to be set with the colour that came before it. There needs to be a delay between the colours switching so the rainbow does not spin too quickly.

Hint: Use a list to keep track of the LED numbers which should be assigned to each colour.

This is quite tricky! Check out the next page if you get stuck!

Moving rainbow Ring Code

In this example an array is used to keep track of the LED number which should be set to each colour.

Each LED is set during the loop

A delay of half a second leaves the colours set so they do not change too quickly.

Every LED in the list then needs to be incremented so the next loop will shift all the colours along by 1.

See if you can understand how this works. It is a super hard piece of code! Otherwise feel free to copy and paste the code onto your robot and get a flashing rainbow!

import time
from invent import *

red = (255,0,0)
orange = (255,102,0)
yellow = (255,255,51)
green = (0,153,0)
blue = (51,255,255)
indigo = (0,0,153)
violet = (102,0,204)
pink = (204,51,204)



LEDcount = [0,1,2,3,4,5,6,7]
while True:
  facenp[LEDcount[0]]=red
  facenp[LEDcount[1]]=orange
  facenp[LEDcount[2]]=yellow
  facenp[LEDcount[3]]=green
  facenp[LEDcount[4]]=blue
  facenp[LEDcount[5]]=indigo
  facenp[LEDcount[6]]=violet
  facenp[LEDcount[7]]=pink
  facenp.write()
  time.sleep(0.5)
  
  for i in range(len(LEDcount)):
    LEDcount[i] += 1
    if LEDcount[i]>7:
      LEDcount[i] = 0