29
Point the infrared transmitter towards the infrared receiver. When you press the collision sensor for the
first time, the LED is on. When you press the collision sensor a second time, the buzzer will sounds.
When you press the collision sensor a third time, the led goes out and the buzzer no longer beeps.
What will you see
Infrared Emitter code:
1. Import the IR library and declare the pin of the collision sensor.
2. Declare three arrays to store the data we want to send.
3. Setup the collision sensor as input.
4. Create a function to scan if the collision sensor is pressed.
5. If the collision sensor is pressed for the first time, the data LED1 is sent.
6. If the collision sensor is pressed for the second time, the data LED2 is sent.
7. If the collision sensor is pressed for the third time, the data OFF is sent.
Infrared Receiver code:
1. Import IR library and declare led, buzzer and IR receiver pins.
2. Set the led and buzzer as outputs and initialize the IR receiver and serial monitor.
3. Receive data.
4. If the seventh bit of the data is equal to 1, turn on the LED.
5. If the seventh bit of the data is equal to 2, make the buzzer work.
6. If the seventh bit of the data is equal to 3, turn off the LED and stop the buzzer.
Code overview
Code usage
Import the IR library. Calling this library file can greatly simplify the complicated process when the
infrared transmitter and infrared receiver send and receive data. It enables us to implement infrared
data transmission and reception with simple functions.
IR library: #include <IRSendRev.h>
The complete connection is as follows.
Содержание Crowtail Deluxe Kit
Страница 47: ......