Lesson II

Ok, now you know how to get the controls on the brick to do different things when you press them. Lets try editing the code and adding more functionality to it.

In this lesson we’re going to learn how to change the lights on the brick itself. We start by using the brick buttons template and adding the four lines you see here in bold.

Brick Lights

What does it do, type it in and find out. Assuming you got it all right when you press the different buttons on the brick the LED lights on it will change. You have a three more colors than shown here, namely GREEN, ORANGE and BLACK. The latter turns the LED off.

Let’s add a little spice to our program by add a random number generator.

We need first import a couple of standard PYTHON libraries into our code. One for time and one for generating random numbers. Add these two lines under the “from” statements.

Then add these lines under that to define a subroutine. A subroutine that will generate a random number and return a random color associated with it.

Having defined a subroutine to generate a random color, we can then use it in our conditional if statements.

Image for post
Image for post

This will return a random color every time you press the UP, DOWN buttons. Unfortunately you’ve only got five colors on LED panel, all of which I named I already mentioned.

Written by

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store