Hardware Reference
In-Depth Information
call two i les: “20.wav” and “4.wav”. After the i lename is created, playfile()
is called with the i lename passed as a String .
playfile() is declared on line 80. It takes a single parameter, a String , the name
of the i le to be opened. On line 82, a const int is declared, which is the amount of
data to be copied from the wave i le per pass. On the next line, a buffer is created;
this is the container where data will be placed from the i le on the SD card. On
line 84, another variable is created; this is again the i lename, but as a char array;
the SD.open() function does not accept strings, only chars .
On line 89, the sketch attempts to open the i le on the SD card. If it fails, it
prints out a message on the serial port and then halts execution. If the sketch
does open the i le, it carries on.
On line 99, a while loop is created, and loops until there is no more data left
to read in the i le. This is done with the File.available() function, which
returns the number of bytes that can be read from the i le. On line 102, the
i le is read in blocks of sizeof(buffer) into buffer . On line 105, a variable
is declared and contains the value 1023. This is used on the next line, where
the Audio library prepares the samples with the Audio.prepare() function. It
takes the local buffer called buffer , the size of that buffer, and the volume to be
applied; in this case, 1023, or the highest volume possible. The i nal step is to
write the local buffer into the Audio buffer with the function Audio.write() .
This function takes the same parameters as the Audio.prepare() function, with
the exception of the volume. When the while loop is i nished, the i le is closed,
and the function returns.
Exercise
This application measures the temperature from a single source. You could
modify the sketch to retrieve the temperature from an inside sensor, as well
as the temperature from outside. You could also add a humidity sensor or an
ultraviolet sensor. By pressing a button, you could know that the outside tem-
perature is 38-degrees Celsius, the humidity is 20 percent, and the UV index is
8, but inside you have a comfortable 24 degrees.
Not everyone uses Celsius; you could modify the sketch to use Fahrenheit, and
even use the EEPROM to store your setting, making this a sketch that you can
use worldwide. You could even create your own shield with sensor connectors,
an SD slot, and an audio jack integrated directly onto the shield.
 
Search WWH ::




Custom Search