#include <IRremote.h>
// Define the pins for the DC motors
#define MOTOR_A_FORWARD 3
#define MOTOR_A_BACKWARD 4
#define MOTOR_B_FORWARD 5
#define MOTOR_B_BACKWARD 6
// Create an instance of the IR receiver
IRrecv irrecv(7); // Pin where the IR receiver is connected
decode_results results;
// Setup function to initialize the motors and IR receiver
void setup() {
// Initialize motor pins as outputs
pinMode(MOTOR_A_FORWARD, OUTPUT);
pinMode(MOTOR_A_BACKWARD, OUTPUT);
pinMode(MOTOR_B_FORWARD, OUTPUT);
pinMode(MOTOR_B_BACKWARD, OUTPUT);
// Start the IR receiver
irrecv.enableIRIn();
}
// Loop function to continuously check for IR signals
void loop() {
if (irrecv.decode(&results)) {
// Check the received IR code and control motors accordingly
switch (results.value) {
case 0xFF6897: // Example code for "Forward"
moveForward();
break;
case 0xFF30CF: // Example code for "Backward"
moveBackward();
break;
case 0xFF18E7: // Example code for "Left"
turnLeft();
break;
case 0xFF7A85: // Example code for "Right"
turnRight();
break;
case 0xFF10EF: // Example code for "Stop"
stopMotors();
break;
default:
break;
}
irrecv.resume(); // Receive the next value
}
}
// Function to move the car forward
void moveForward() {
digitalWrite(MOTOR_A_FORWARD, HIGH);
digitalWrite(MOTOR_A_BACKWARD, LOW);
digitalWrite(MOTOR_B_FORWARD, HIGH);
digitalWrite(MOTOR_B_BACKWARD, LOW);
}
// Function to move the car backward
void moveBackward() {
digitalWrite(MOTOR_A_FORWARD, LOW);
digitalWrite(MOTOR_A_BACKWARD, HIGH);
digitalWrite(MOTOR_B_FORWARD, LOW);
digitalWrite(MOTOR_B_BACKWARD, HIGH);
}
// Function to turn the car left
void turnLeft() {
digitalWrite(MOTOR_A_FORWARD, LOW);
digitalWrite(MOTOR_A_BACKWARD, HIGH);
digitalWrite(MOTOR_B_FORWARD, HIGH);
digitalWrite(MOTOR_B_BACKWARD, LOW);
}
// Function to turn the car right
void turnRight() {
digitalWrite(MOTOR_A_FORWARD, HIGH);
digitalWrite(MOTOR_A_BACKWARD, LOW);
digitalWrite(MOTOR_B_FORWARD, LOW);
digitalWrite(MOTOR_B_BACKWARD, HIGH);
}
// Function to stop the motors
void stopMotors() {
digitalWrite(MOTOR_A_FORWARD, LOW);
digitalWrite(MOTOR_A_BACKWARD, LOW);
digitalWrite(MOTOR_B_FORWARD, LOW);
digitalWrite(MOTOR_B_BACKWARD, LOW);
}