Lesson 33: Colour mixer
Mix the light
- An RGB LED looks like one LED, but it has three inside: Red, Green and Blue.
- Each one can be dimmed with
analogWrite, from 0 (off) to 255 (fully on). - Your eye mixes the three lights, so with different amounts you can make almost any colour.
Words for this lesson
| Word | Meaning |
|---|---|
| RGB | red, green, blue: the three colours of light |
| mix | put together |
| PWM | switching a pin on and off very fast to dim an LED |
| fade | change slowly |
Light colours
| Red | Green | Blue | You see |
|---|---|---|---|
| 255 | 0 | 0 | red |
| 255 | 255 | 0 | yellow |
| 0 | 255 | 255 | cyan |
| 255 | 0 | 255 | magenta |
| 255 | 255 | 255 | white |
- Mixing light is not like mixing paint: red and green light make yellow.
In the exam
A screen pixel works like your RGB LED: an amount of red, green and blue. The number of bits for each pixel is the colour depth.
- With 8 bits for each colour (0 to 255, as in
analogWrite), a pixel needs 3 × 8 = 24 bits, and there are 256 × 256 × 256 ≈ 16.7 million colours. - 0478/12/F/M/25 Q2(a): state what is meant by the colour depth of an image: the number of bits used to store the colour of each pixel.
- 0478/12/M/J/25 Q2(c): an image has a colour depth of 32 bits; it is changed to 64 bits. More bits per pixel: more colours can be shown, and the file gets bigger.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
An RGB LED has three small LEDs inside: red (pin 9), green (pin 10) and blue (pin 11). Mixing their light makes other colours. Make purple: red fully on (255), green off (0) and blue half on (128), with analogWrite. Then make the function setColour(r, g, b) do it, so one line sets any colour.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 4/5 · 280 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Now a smooth change from red to green, like a light that shows "go" slowly. In a for loop, let i go from 0 to 255: red gets 255 - i and green gets i, with delay(8) each time (about 2 seconds). Blue stays off. At the end the LED stays green.
คลิก Run เพื่อดูผลลัพธ์ที่นี่