ch of which is one byte in size and ng on the word, es required to represent the sounds in a word is similar to the number of letters e take up two nd leave you with a sentence ck and in
Trang 1CHAPTER 9 SPEECH SYNTHESIZER
Bi
240
245
246
247
159
Trang 2CHAPTER 9 SPEECH SYNTHESIZER
Creating easily understood words using allophones can be a painstaking process E
sentence can take quite a while to put together if you have to sound out every single wor
To save you some time, the speech synthesizer project
ven a short
d individually
page on the Practical Arduino web site has a link
to a file of nearly 2,000 words and their equivalent phonemes All you have to do is look up the word you
ist and copy the associated codes into your software
s to fill it up
ch of which is one byte in size and
ng on the word,
es required to represent the sounds in a word is similar to the number of letters
e take up two
nd leave you with a sentence
ck and insert a delay lay is too long you will llophones, and if the delay is too short make it work fairly reliably, but it's certainly not ideal.By connecting status pins from the SpeakJet to digital inputs on the Arduino it's possible to have your software
y to receive more data, and then drip-feed the commands to it at a rate that can be spoken without overflowing the buffer
shi ect uses L cted to D0, D1, and D2 to visually indicate Ready, Speaking, and Buffer Half Full, respectively Use three additional 1K resistors to link the SpeakJet pins to
Table 9-5 SpeakJet status pin connections to Arduino inputs
want on the l
Variations
Read SpeakJet Status
Because the SpeakJet has a small 64-byte input buffer it doesn't take many command
Remember that the input buffer stores a sequence of allophones, ea
represents a sound within a word or a pause between words Although it varies dependi
the number of allophon
within it However, be aware that commands such as changes to pitch, volume, and rat
bytes in the buffer: one to indicate the parameter, and one to indicate the value
With just 64 bytes available a single sentence can easily fill the buffer a
that's chopped off at the end
One simple way around the problem is to send a few values together as a blo
before sending the next block, but that approach is fraught with danger If the de
hear a pause while the SpeakJet is waiting for the next batch of a
the text will be chopped up with pieces missing where the buffer overflowed With some
experimentation you can probably
automatically detect whether the SpeakJet is read
ital pins, as listed e 9-5
SpeakJet Pin Name Function Arduino Pin
Trang 3CHAPTER 9 SPEECH SYNTHESIZER
161
Figure 9-10 Resistors connecting SpeakJet outputs to Arduino inputs
By reading digital pins 4, 5, and 6 using the Arduino, you can now check whether
read
the SpeakJet is
y to accept more values before sending them through If pin 6 (Ready) is high the SpeakJet has
the SpeakJet is one as far as knowing when
it is safe to send through more data: once the input buffer exceeds half full this line will go high,
st that it should stop sending values for a while By checking for pin 4 to go low again will know when the SpeakJet has enough room in its buffer to accept at least another 32
values
mission to the
eb site
Resources
The Magnevation web site, at www.magnevation.com, has an excellent SpeakJet user manual available for download as a PDF The user manual explains the background behind allophone-based speech
synthesis and the various options available on the SpeakJet, and includes details of all the command
codes that can be sent to it as well as a reference guide for allophones
passed its internal self-test and is ready to receive values Pin 5 will go high only while
actually speaking, and then immediately go low again Pin 4 is the important
indicating to the ho
your Arduino
Example code that uses the Buffer Half Full signal to regulate the rate of data trans
speech synthesizer shield is available on the project page on the Practical Arduino w
Trang 5C H A P T E R 10
Water Flow Gauge
Determining the consumption of a resource that is measured in units of volume can be more tricky than
it sounds Use of resources such as water, gas, and even electricity is typically measured by gauges that determine either instantaneous flow rate or cumulative volume over time Both techniques have
problems Measuring flow rate at frequent intervals allows you to do time-based reporting and generate
a graph of how the flow rate varied over time, but to determine the total consumption by volume across
a specific time period, you then have to integrate the data This means there is the danger of
underreporting usage if your sample rate is slow and usage rapidly fluctuates or spikes Measuring
cumulative volume makes it easy to determine total consumption across a period and is accurate in
terms of total usage, but to generate a flow-rate graph, you then need to calculate the difference between each sample If your recording interval isn't brief enough, any short-term spikes in usage will be
averaged across the recording interval and might not show clearly on the graph
Flow gauges typically output a series of pulses proportional to the instantaneous flow rate, which
means that to interpret them it's necessary to implement a simple frequency counter This is actually the same way many car speedometers work: a wheel sensor outputs a pulse for each rotation of a wheel,
which means the pulse frequency varies proportionally to the vehicle speed The speedometer then
displays a scaled version of the current pulse frequency to show instantaneous speed, while the
odometer displays a scaled cumulative pulse count to show distance traveled Both pieces of
information are based on the same underlying data, but they are recorded and displayed in different
ways
This project uses a flow-rate gauge containing a Hall-effect sensor that outputs a pulse rate
proportional to flow rate Not only is it a useful project in its own right, but it also demonstrates a very
useful technique that you can use in a wide range of projects that need to measure the rate at which
something happens We've also included an LCD module so that the unit can report flow rate and
volume both via the serial connection to a host computer and also directly via the LCD
The example program calculates and displays current flow rate, and also maintains two cumulative counters of the volume that has flowed through the sensor Two pushbuttons allow you to reset the
counters independently This allows you to leave one counter running as a long-term accumulator, and reset the other one occasionally prior to measuring short-term events such as the water consumed by
filling a bath, running an irrigation system, or running a washing machine The required parts are shown
in Figure 10-1, and the complete schematic is in Figure 10-2
163
Trang 6CHAPTER 10 WATER FLOW GUAGE
Parts Required
1 Arduino Duemilanove, Arduino Pro, Seeeduino, or equivalent
1 Prototyping shield
1 Flow-rate gauge, such as a ZD1200 or ZD1202
1 16x2 LCD module, HD44780-compatible
3 1K resistor
1 10R resistor
1 680R resistor
1 LED
2 Momentary-action pushbuttons
Ribbon cable
Three-core cable
Three-way line plug and socket
Source code available from www.practicalarduino.com/projects/water-flow-gauge
Trang 7CHAPTER 10 WATER FLOW GUAGE
Figure 10-2 Schematic of water flow gauge
Instructions
If you don't care about including an LCD or counter reset buttons, the basic circuit for this project is so trivially simple that you don't even need the prototyping shield In fact, all you need is the Arduino, the Hall-effect flow sensor, one 1K resistor, and three male breakaway header pins Just connect the positive and ground lines from the sensor to Arduino 5V and ground, respectively Then connect the sensor
output line to Arduino digital I/O line 2 via the 1K resistor, and you're done The example code that
follows will run perfectly fine and you'll still get values reported via the serial port, but adding an LCD
and counter reset buttons makes this project much more useful as a stand-alone device
Replace Sensor Connector
The flow sensor we used came fitted with a 3-pin plug for which we could not find a matching socket
The simplest solution is to cut off the existing connector and replace it with a new 3-pin line plug We
used a matching pair of automotive-style nylon connectors that are commonly available in many
electronics parts stores for only a few dollars
165
Trang 8CHAPTER 10 WATER FLOW GUAGE
The Hall-effect flow sensor we used has three connections: black (ground), red (power), and brown (signal) Cut off the existing plug, then strip about 5mm of insulation from the leads and solder them to the pins in your new connector We laid them out in the same order as the original connector so ground
is at one end, signal is in the middle, and power is at the other end
Strip back the end of your three-core cable in a similar way and solder the matching connector onto the end, taking note of the color codes so you know which colors in your cable represent the ground, power, and signal leads on the sensor To save confusion, it's best to match up the colors if possible, but
if your cable has different colors in it just match them as best you can and write yourself a note about what connection each color represents This will save you from crawling around under your house later, after the sensor has been installed and you've forgotten which color is which!
The ZD-series sensors are very flexible and can operate on any voltage from 2.4 to 26V They also consume less than 6mA of power so they can easily be run directly from the 5V line of an Arduino
Prepare Prototyping Shield
The two counter reset buttons connect to Arduino digital I/O lines 11 and 12, so fit a pair of 1K resistors with one end of each connected to those lines In a moment we’ll connect the other ends of the resistors
to wires running to the buttons
Also install a 1K resistor with one end connected to digital I/O line 2 The other end of this resistor will ultimately be connected to the sensor output so that the Arduino can read it You can actually dispense with the 1K resistor and connect the sensor directly to the input if you prefer, but it's good to get into the habit of using a low-value resistor in series with inputs to provide a bit of extra protection for the ATMega's pins just in case something nasty happens on the wire running to the sensor It's not so important with connections within a device, but in this case the wire to the input could run some distance to the location where the sensor is installed Long wire runs have more tendency to pick up electrical noise so it's best to play it safe
What you don't see in the schematic is that I/O line 2 will be biased toward 5V using a 20K “pull-up” resistor that's inside the ATMega CPU itself, and can be activated by software That resistor pulls pin 2 toward +5V but still allows it to be explicitly pulled down if an external device, such as the flow sensor used in this project, shorts it to ground This is a common technique when working with digital inputs because it puts the input into a known default “HIGH” state, which can then be changed by forcing the input to ground to assert a “LOW” state In this scenario the resistor is referred to as a “pull-up” resistor because it will tend to pull up the voltage on the pin, and a device can override the high value by
shorting it to ground
The alternative arrangement is to use a “pull-down” resistor, with one end connect to the input and the other end connected to ground A pull-down resistor holds the voltage on the input down at 0V unless a device overrides the low value by shorting it to +5V and taking it high So there are two basic approaches we could take: a pull-up resistor combined with an active-low input, or a pull-down resistor combined with an active-high input At first it might sound more logical to use a pull-down resistor and
an active-high input because we're accustomed to thinking in terms of LOW being off and HIGH being
on However, that won't work in this case because the flow sensor used for this project has what is known as an “open-collector” output This means the output is generally left as an open circuit
(unconnected, or floating value with high resistance), and that output pulses are generated by shorting it
Trang 9CHAPTER 10 WATER FLOW GUAGE
common method of connecting inputs that it's provided inside the ATMega itself, but it's shown on the schematic to demonstrate what happens behind the scenes
Figure 10-3 Open-collector sensor connected to an Arduino
This may all sound confusing at first because the logic is doing the opposite of what you might
expect, but it all works out nicely in the end! Use of a pull-up resistor that is overridden by a device using 0V to indicate an event is an important concept to understand because many devices use open-collector outputs, and interfacing with them is a common requirement in Arduino projects In fact, it's such a
common arrangement that all ATMega digital inputs have internal pull-up resistors fitted to them, but the CPU designers didn't even bother including corresponding pull-down resistors
At this point you could also fit the status LED, with the anode (long) lead connected to +5V The
cathode (short) lead connects to a 680R resistor, which in turn connects to Arduino digital I/O line 13
The result is that the LED will be off if Arduino output 13 is HIGH, and on if it's LOW Many Arduino
models already have an LED connected to pin 13 so you can leave that one off if you like However, if
you're using the shield, the LED on the Arduino will be hidden from view It will also have the opposite logic (LOW to turn off, HIGH to turn on), so the Arduino LED will be off whenever the additional LED is
on, and vice versa (see Figure 10-4)
It may be hard to see in Figure 10-4 because they're so tiny, but this particular prototyping shield
comes with a pair of red and green surface-mount LEDs and matching dropper resistors fitted on the
bottom left of the board so we didn't need to add the LED ourselves We just used one of the LEDs
already on the shield and connected it to line 13, and connected the other one as a power-on indicator
by linking it between ground and 5V via the prefitted dropper resistor
167
Trang 10CHAPTER 10 WATER FLOW GUAGE
Figure 10-4 1K resistors in place for button inputs and sensor input
Prepare LCD Module
For this project we picked quite possibly the most common LCD type ever made: the venerable
HD44780-compatible 16-character by 2-line (16x2) display The HD44780 display controller chip was originally developed by Hitachi and has since been copied by so many manufacturers that it has become
a de facto industry standard, and you can buy displays with a compatible interface just about anywhere HD44780-compatible displays are commonly available in 8x1, 8x2, 16x1, 16x2, 20x2, 20x4, and even 40x4 sizes The larger formats sometimes combine several driver chips within the module, but from the microcontroller's point of view they still behave in the same way with the same interface format They simply let you address more lines and write more characters before running off the end of the screen The HD44780 uses a “parallel” interface so there are a lot of pins to connect Parallel interfaces work
by presenting a complete byte (8 bits) of data to a set of 8 data lines all at once, then pulsing an "Enable" pin to indicate to the module that the byte is ready to read This allows the microcontroller to take whatever time it needs to set each of the data lines to the correct state, and while that is happening the module will totally ignore the state of those pins Then when they're all set to the correct state a short