The Arduino Inventor's Guide (26 page)

BOOK: The Arduino Inventor's Guide
7.68Mb size Format: txt, pdf, ePub
ads

Each time you move your project into a new room or lighting situation, recalibrate your photoresistor by pressing the reset button. When you recalibrate your photoresistor, make sure that it’s reading the actual lighting conditions of your room and that you’re not accidentally shadowing it in any way.

CREATE MORE COLORS WITH ANALOGWRITE()

You’re not restricted to just the colors you’ve seen so far with the RGB LED. By mixing gradations of those colors, you can make cerulean, orange, powder pink, or any of the other thousands of combinations.
But you can’t create these colors by just turning the different standard colors on an RGB LED on and off, so you need a way to turn red on just a bit and add a hint of blue and green to create, for example, pink.

Create Analog Signals with PWM

To use the LED in this way, you need to use analog values rather than digital values. In the Electronics Primer, we talked about the difference between analog and digital (page
10
). A digital value can only be on or off, like a normal light switch. An analog signal has an infinite number of values, so it works on a scale, like a dimmer switch.

The problem is that the Arduino is a digital device, which means that it can only turn things on and off. To get it to output a value somewhere between on and off, you use a technique called
pulse width modulation (PWM)
to emulate an analog signal with digital values. The Arduino does this by turning a digital pin on and off extremely fast and then varying the relative amount of time the signal is
HIGH
(on) compared to the amount of time the signal is
LOW
(off) to create a signal that appears to be analog. The longer a pin is at a high value, the greater the analog value of the signal. This is sometimes also called varying the
duty cycle
.
Figure 5-17
shows some duty cycles with varying pulse widths; the analog value of the pulse at 75 percent is higher than the value of the 25 percent duty cycle.

FIGURE 5-17:
Duty cycle signals showing different widths of the pulse

That’s great, but not all Arduino pins have the ability to use PWM. On a standard Arduino, only certain GPIO pins—that is, pins 3, 5, 6, 9, 10, and 11—are PWM compatible. These pins are noted with a tilde (~) on the board, highlighted in
Figure 5-18
.

FIGURE 5-18:
PWM pins on the standard RedBoard

Anytime you want to control something with varying values, like the brightness of an LED, the speed of a motor, or the tone of a buzzer, you’ll need to use a PWM pin to fake an analog signal. Here, we’ll use it to control the brightness of each color of the RGB LED in order to mix the colors. For this project, you have the RGB LED red, green, and blue pins already hooked up to PWM pins 11, 10, and 9, respectively, so you don’t have to change any of your wiring, only your code.

Mix Colors with analogWrite()

To utilize these PWM powers, you need to use the
analogWrite()
function, which writes a PWM value to a pin. The
analogWrite()
function accepts two parameters: the pin number that you want to control and a PWM value to write, which is always a range from 0 to 255. The value of
0
is completely off, and
255
is completely on. Let’s write a simple sketch that demonstrates the
analogWrite()
function.

  
void
setup
()
  {

   
pinMode
(9,
OUTPUT
);
  }
  
void
loop
()
  {

   
analogWrite
(9, 2);
  }

First, as with any other pin, you need to specify how you’re going to use the GPIO pin using the
pinMode()
function. You pass the pin number, and, since the LED is an output, you pass
OUTPUT
as you have in past projects

. To set a PWM value, you use the
analogWrite()
function to set an analog value between 0 and 255. In the example,
pin 9 (the blue anode of the RGB LED) is set to an analog value of
2

. Upload this sketch to your Arduino, and you should have a dim blue colored RGB LED. The
analogWrite()
value of 2 uses PWM to turn on this LED for about 0.7 percent or 2/255 of the time. Before moving on, try changing the PWM value and reuploading a few times to get a feel for the different values and their intensity.

Now that you have the hang of the
analogWrite()
function, try using it to mix colors.
Listing 5-3
creates a blink pattern with different, more interesting colors than those used so far.

LISTING 5-3:
Multicolored blink

  
void
setup
()
  {

   
pinMode
(11,
OUTPUT
);   
//red
    
pinMode
(10,
OUTPUT
);   
//green
    
pinMode
(9,
OUTPUT
);    
//blue
  }
  
void
loop
()
  {

   
analogWrite
(11, 153);  
//dark orchid purple
    
analogWrite
(10, 50);
    
analogWrite
(9, 204);
    
delay
(1000);

   
analogWrite
(11, 155);  
//pale cerulean
    
analogWrite
(10, 196);
    
analogWrite
(9, 226);
    
delay
(1000);

   
analogWrite
(11, 255);  
//cadmium yellow
    
analogWrite
(10, 246);
    
analogWrite
(9, 0);
    
delay
(1000);
  }

First, add the other two color pins using the
pinMode()
function

. Then, cycle through three different colors by setting different analog values to the three pins. This sets the brightness level of each specific color, changing how much of that color is added to the final mix.

The first color set creates a bluish purple

, the second creates a dusty pale blue

, and the final set creates a bright yellow

. Play around with
analogWrite()
to create different colors.

Find RGB Values with Color Picker

You created some pretty specific colors before, but it can be hard to predict which RGB values will give you certain colors. An easy way of
finding out is to use a color-picker tool on the web. There are plenty of them out there, but we recommend
https://www.colorpicker.com/
. This tool gives you the RGB values for colors you pick from a palette, as shown in
Figure 5-19
.

FIGURE 5-19:
The color-selector tool from
https://www.colorpicker.com/
.

The three numbers you want to use are labeled R, G, B for the three colors Red, Green, and Blue. Ignore the top three H, S, B boxes; they refer to another common method for specifying colors called
HSB (hue, saturation, and brightness)
. This is a useful technique for many color mixing and graphic design applications, but it’s not as useful when you have direct control over the three colors red, green, and blue.

The Custom-Color Night-Light Code

With the RGB knowledge in hand, now you can easily modify your Night-Light code to include your custom color by replacing your
digitalWrite()
function with
analogWrite()
.
Listing 5-4
shows changes to the Night-Light code, with the color values set to the teal color we selected in
Figure 5-19
.

LISTING 5-4:
Final Night-Light code with
analogWrite()
instead of
digitalWrite()
commands

int calibrationValue;
int lightValue;
void setup()
{
  
pinMode(9, OUTPUT);
  
pinMode(10, OUTPUT);
  
pinMode(11, OUTPUT);
  
calibrationValue = analogRead(A0);
}
void loop()
{
  
lightValue = analogRead(A0);
  
if(lightValue < calibrationValue - 50)
  
{
    
analogWrite
(11, 66);   
//red
    
analogWrite(1
0, 166);  
//green
    
analogWrite(9
, 199);   
//blue
  
}
  
else
  
{
    
analogWrite
(11, 0);    
//red off
    
analogWrite
(10, 0);    
//green off
    
analogWrite
(9, 0);     
//blue off
  
}
}

With that, your Night-Light prototype is done! If you don’t like the teal we selected, use the color-selector tool to find a color you prefer, update your
analogWrite()
functions with its RGB value, and then re-upload your sketch before moving on. Visit
http://99colors.net/color-names/
if you want to browse through some fun color suggestions.

Now that you have the code working, it’s time to get creative and build the enclosure and lampshade.

BUILD THE NIGHT-LIGHT ENCLOSURE

We suggest cardstock for this project’s enclosure rather than cardboard (as it produces cleaner edges and is easier to work with) and a vellum material or translucent paper for the shade. Let’s get building.

Cardstock Construction

We’ll show you a basic Night-Light design to get you started, but we encourage you to get creative by customizing the design later. Or, if you’re feeling confident, you can design your own Night-Light enclosure from scratch without using our templates at all.

Cut Out the Parts

This project has two templates: one for the structure of the Night-Light and one for the shade. The shade can be made from any material similar to printer paper in thickness, but we’ve found that a fully translucent material like vellum works best. If you have a printer, you can open the templates in
Figure 5-20
from this book’s resource files and print them directly to your material to cut out.

FIGURE 5-20:
Enclosure templates for the Night-Light (not full size)

BOOK: The Arduino Inventor's Guide
7.68Mb size Format: txt, pdf, ePub
ads

Other books

Death Canyon by David Riley Bertsch
Leave Me Alone by Murong Xuecun
Dreamwood by Heather Mackey
Love at Large by Jaffarian;others
Night Chill by Jeff Gunhus
His Mistress By Christmas by Alexander, Victoria