Hardware Reference
In-Depth Information
//Read byte of data
data = Serial.read();
//If the button is pressed, play tone and turn LED on
if (data == '1')
{
digitalWrite(onLED, HIGH);
digitalWrite(offLED, LOW);
tone(SPEAKER, freq);
}
//If the button is not pressed, turn the sound and light off
else if (data == '0')
{
digitalWrite(onLED, LOW);
digitalWrite(offLED, LOW);
noTone(SPEAKER);
}
}
}
The first if() statement in loop() checks the elapsed time since it last ran.
If it's been at least 100ms, it's time to switch the lights and frequency. By checking
the current states, you can alternate values for the light and frequency. You set the
offLED when the other light gets turned on. At the end of the if() statement, the
previous time is set to the present time so that the process can be repeated.
The second large if() statement in loop() checks incoming serial data. When
a '0' is received, everything gets turned off. When there is a '1' , the light and
speaker turn on according to the values set earlier in loop() .
NOTE WatchademovideoofthewirelessArduinodoorbellat www.explorin-
garduino.com/content/ch11 .YoucanalsofindthisvideoontheWileywebsite
shownatthebeginningofthischapter.
Summary
In this chapter, you learned about the following:
There are a wide range of available XBee models.
You must convert between 5V and 3.3V logic levels to use an XBee with
most Arduinos.
You can configure XBee using either X-CTU on Windows, or the terminal
on Linux and Mac.
 
Search WWH ::




Custom Search