Hardware Reference
In-Depth Information
//Chip 1 (SS 10) Resiter 1 is Yellow
//Chip 2 (SS 9) Register 0 is Green
//Chip 2 (SS 9) Register 1 is the Speaker
void setReg(int SS, int reg, int level)
{
digitalWrite(SS, LOW); //Set the given SS pin low
SPI.transfer(reg); //Choose the register to write to
SPI.transfer(level); //Set the LED level (0-128)
digitalWrite(SS, HIGH); //Set the given SS pin high again
}
void loop()
{
tone(SPEAKER, freq); //Set speaker to given frequency
for (int i=0; i<=128; i++)
{
setReg(SS1, REG0, i);
setReg(SS1, REG1, i);
setReg(SS2, REG0, i);
setReg(SS2, REG1, i);
delay(10);
}
delay(300);
for (int i=128; i>=0; i--)
{
setReg(SS1, REG0, i);
setReg(SS1, REG1, i);
setReg(SS2, REG0, i);
setReg(SS2, REG1, i);
delay(10);
}
delay(300);
freq = freq+100;
if (freq > 2000) freq = 100;
}
Load this program onto your Arduino, and in addition to the lights chang-
ing intensity, the speaker will change volume. On each cycle, the frequency
is incremented by 100Hz until it reaches 2000Hz. This is controlled by the if
statement at the end of loop() . The for loops that are controlling LED brightness
and volume do not need to change at all from what you wrote in ListingĀ 9-1,
because speaker volume is being controlled by the same potentiometer action
that is controlling the LEDs.
Search WWH ::




Custom Search