2.3 Colorful Balls

In this project, we will make the RGB LEDs display different colors.

Clicking on different colored balls on the stage area will cause the RGB LED to light up in different colors.

../_images/4_color.png

You Will Learn

  • The principle of RGB LED

  • Copy sprites and select different costumes

  • Three primary colors superimposed

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

3 in 1 Starter Kit

380+

3 in 1 Starter Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

SunFounder R3 Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

RGB LED

BUY

Build the Circuit

An RGB LED packages three LEDs of red, green, and blue into a transparent or semitransparent plastic shell. It can display various colors by changing the input voltage of the three pins and superimpose them, which, according to statistics, can create 16,777,216 different colors.

../_images/4_rgb.png ../_images/rgb_circuit.png

Programming

1. Select sprite

Delete the default sprite, then choose the Ball sprite.

../_images/4_ball.png

And duplicate it 5 times.

../_images/4_duplicate_ball.png

Choose different costumes for these 5 Ball sprites and move them to the corresponding positions.

Note

Ball3 sprite costume color needs to be manually changed to red.

../_images/4_rgb1.png

2. Make RGB LEDs light up in the appropriate color

Before understanding the code, we need to understand the RGB color model.

The RGB color model is an additive color model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors.

Additive color mixing: adding red to green yields yellow; adding green to blue yields cyan; adding blue to red yields magenta; adding all three primary colors together yields white.

../_images/4_rgb_addition.png

So the code to make the RGB LED light yellow is as follows.

../_images/4_yellow.png

When the Ball sprite (yellow ball) is clicked, we set pin 11 high (red LED on), pin 10 high (green LED on) and pin 9 low (blue LED off) so that the RGB LED will light yellow.

You can write codes to other sprites in the same way to make the RGB LEDs light up in the corresponding colors.

3. Ball2 sprite (light blue)

../_images/4_blue.png

4. Ball3 sprite (red)

../_images/4_red.png

5. Ball4 sprite (green)

../_images/4_green.png

6. Ball5 sprite (purple)

../_images/4_purple.png