Hardware Reference
In-Depth Information
You might also want to consider adding some analog sensors to this later to
experiment with using light, movement, and sound to control the output from
your audiovisual display system.
ModifyingtheSoftware
To get started with this setup, make some simple modifications to your previ-
ous program for controlling the LEDs. Add a pin variable for the speaker, as
well as a variable to set to the frequency of the speaker. (You'll have it change
throughout the program to keep things exciting.) Inside loop() , you can option-
ally add some iterators that increase the speaker frequency on each run through
the loop. You can use the exact same setLed() function as before to set the
speaker volume, but the name is now a bit misleading, so you might want to
rename the function for clarity. In the complete code shown in Listing 9-2, it
has been renamed to setReg() .
Listing 9-2: LED and Speaker Volume SPI Digital Potentiometer Control—LED_speaker.ino
//Changes LED brightness using voltage input instead of PWM
//Controls speaker volume and tone
//Include SPI library
#include <SPI.h>
const int SPEAKER=8; //Speaker Pin
int freq = 100;
//When using the SPI library, you only have to worry
//about picking your slave selects
//By default, 11 = MOSI, 12 = MISO, 13 = CLK
const int SS1=10; //Slave Select Chip 1
const int SS2=9; //Slave Select Chip 2
const byte REG0=B00000000; //Register 0 Write command
const byte REG1=B00010000; //Register 1 Write command
void setup()
{
//Set pin directions for SS
pinMode(SS1, OUTPUT);
pinMode(SS2, OUTPUT);
//Initialize SPI
SPI.begin();
}
//This will set one pot to the specififed level
//Chip 1 (SS 10) Register 0 is Red
Search WWH ::




Custom Search