xii Contents in Detail
6
n u m B e r s, V A r i A B l e s, A n d A r i t h m e t i C
In this chapter you will
Generate random numbers
Create electronic dice
Learn about binary numbers
Use shift-register integrated circuits (ICs) to get more digital output pins
Test your knowledge of binary numbers with a quiz
Learn about arrays of variables
Display numbers on seven-segment LED modules
Learn how to use the modulo math function
Create a digital thermometer
Learn about bitwise arithmetic
Create fixed and moving images on LED matrix displays
You will learn a wide variety of useful new functions that will create more project options, including random number generation, new kinds
of math functions, and variable storage in ordered lists called arrays. Furthermore, you will learn how to use LED display modules in numeric and matrix form to display data and simple images. Finally, we put all that together to create a game, a digital thermometer, and more.
generating random numbers
The ability for a program to generate random numbers can be very useful in games and effects. For example, you can use random numbers to play a dice or lottery game with the Arduino, to create lighting effects with LEDs, or to create visual or auditory effects for a quiz game with the Arduino. Unfortunately, the Arduino can’t choose a purely random number by itself. You have to help it by providing a seed, an arbitrary starting number used in the calculations to generate a random number.
Using Ambient Current to Generate a Random Number
The easiest way to generate a random number with the Arduino is to write a program that reads the voltage from a free (disconnected) analog pin (for example, analog pin zero) with this line in void setup():
Even when nothing is wired to an analog input on the Arduino, static electricity in the environment creates a tiny, measurable voltage. The amount of this voltage is quite random. We can use this measure of ambient voltage as our seed to generate a random number and then allocate it to an integer variable using the random(lower, upper) function. We can use the parameters lower and upper to set the lower and upper limits of the range for the random number. For example, to generate a random number between 100 and 1,000, you would use the following:
int a = 0; a = random(100, 1001);
We’ve used the number 1,001 rather than 1,000 because the 1,001 upper limit is exclusive, meaning it’s not included in the range.
That said, to generate a random number between 0 and some number, you can just enter the upper limit. Here’s how you would generate a random number between 0 and 6:
The example sketch in Listing 6-1 would generate random numbers between 0 and 1,000, as well as numbers between 10 and 50:
// Listing 6-1 int r = 0; void setup()
{
randomSeed(analogRead(0));
Serial.begin(9600);
}
void loop()
{
Serial.print("Random number between zero and 1000 is: ");
r = random(0, 1001); Serial.println(r);
Serial.print("Random number between ten and fifty is: ");
r = random(10, 51); Serial.println(r); delay(1000); }
Listing 6-1: Random number generator
Figure 6-1 shows the result displayed on the Serial Monitor.
Figure 6-1: Output from Listing 6-1
Now that you know how to generate random numbers, let’s put that knowledge to good use by creating an electronic die.
project #15: creating an electronic die
Our goal is to light one of six LEDs randomly to mimic the throw of a die. We’ll choose a random number between 1 and 6, and then turn on the corresponding LED to indicate the result. We’ll create a function to select one of six LEDs on the Arduino randomly and to keep the LED on for a certain period of time. When the Arduino running the sketch is turned on or reset, it should rapidly show random LEDs for a specified period of time and then gradually slow until the final LED is lit. The LED matching the resulting randomly chosen number will stay on until the Arduino is reset or turned off.
The Hardware
To build the die, we’ll need the following hardware:
Six LEDs of any color (LED1 to LED6)
One 560 W resistor (R1)
Various connecting wires
One medium-sized breadboard
Arduino and USB cable
The Schematic
Because only one LED will be lit at a time, a single current-limiting resistor can go between the cathodes of the LEDs and GND. Figure 6-2 shows the schematic for our die.
The Sketch
Here’s the sketch for our die:
// Project 15 - Creating an Electronic Die
void setup()
{
randomSeed(analogRead(0)); // seed the random number generator for ( int z = 1 ; z < 7 ; z++ ) // LEDs on pins 1-6 are output
{
pinMode(z, OUTPUT);
}
}
void randomLED(int del)
{ int r;
r = random(1, 7); // get a random number from 1 to 6 digitalWrite(r, HIGH); // output to the matching LED on digital pin 1-6
if (del > 0)
{ delay(del); // hold the LED on for the delay received
} else if (del == 0)
{
do // the delay entered was zero, hold the LED on
forever {} while (1);
}
digitalWrite(r, LOW); // turn off the LED }
void loop()
{ int a;
// cycle the LEDs around for effect for ( a = 0 ; a < 100 ; a++ )
{
randomLED(50);
}
// slow down for ( a = 1 ; a <= 10 ; a++ )
{
randomLED(a * 100);
}
// and stop at the final random number and LED
randomLED(0); }
Here we use a loop in void setup() to activate the digital output pins. The function randomLED() receives an integer that is used in the delay() function at to keep the LED turned on for the selected time. If the value of the delay received at is 0, then the function keeps the LED turned on indefinitely, because we use
at , which loops forever, because 1 is always 1.
To “roll the die,” we reset the Arduino to restart the sketch. To create a decreasingly slow change in the LEDs before the final value is displayed, at we first display a random LED 100 times for 50 milliseconds each time. Next, we slow it down by increasing the delay between LED flashes from 100 to 1,000 milliseconds, with each flash lasting 100 milliseconds. The purpose of this is to simulate the “slowing down” of a die before it finally settles on a value, at which point the Arduino displays the outcome of the roll by keeping one LED lit with this last line:
Modifying the Sketch
We can tinker with this project in many ways. For example, we could add another six LEDs to roll two dice at once, or perhaps display the result using only the built-in LED by blinking it a number of times to indicate the result of the roll. Use your imagination and new skills to have some fun!
Do'stlaringiz bilan baham: |