SunFounder 3in1 Kit
(continued from previous page)
...
void setup() {
...
//IR remote
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the IR␣
˓→receiver // Start the receiver
Serial.println("REMOTE CONTROL START");
//LED
pinMode(ledPin, OUTPUT);
}
3. When you press the keys on the remote control, the LED will blink and the infrared receiver will know which
key is pressed, and then the car will move according to the corresponding key value.
void loop() {
if (IrReceiver.decode()) {
// Serial.println(results.value,HEX);
String key = decodeKeyValue(IrReceiver.decodedIRData.command);
if (key != "ERROR" && key != lastDecodedValue) {
Serial.println(key);
lastDecodedValue = key; // Update the last decoded value
blinkLED();
if (key == "+") {
speed += 50;
} else if (key == "-") {
speed -= 50;
} else if (key == "2") {
moveForward(speed);
delay(1000);
...
}
IrReceiver.resume(); // Enable receiving of the next value
}
}
• Checks if an IR signal is received and successfully decoded.
• Decodes the IR command and stores it in key using a custom decodeKeyValue() function.
• Checks if the decoded value is not an error and is different from the last decoded value.
• Prints the decoded IR value to the serial monitor.
• Updates the lastDecodedValue with the new decoded value.
• Resumes IR signal reception for the next signal.
4. About the blinkLED() function.
260 Chapter 5. Car Projects