Create an IR-controlled car using Arduino, an HW-130 motor driver, and an IR remote control. The HW-130 motor driver efficiently handles the motors, while the IR remote allows directional control.
#include <IRremote.h>
#include <AFMotor.h>
const int RemotePin = A5;
// Initialize IR Receiver (new library usage)
//AF_DCMotor motor1(1);
//AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
void setup() {
Serial.begin(9600);
// Start the IR receiver with LED feedback enabled
IrReceiver.begin(RemotePin, ENABLE_LED_FEEDBACK);
// motor1.setSpeed(100); if you want to use it
// motor2.setSpeed(100); if you want to use it
motor3.setSpeed(250);
motor4.setSpeed(250);
}
void loop() {
// Check if the IR receiver has received a signal
if (IrReceiver.decode()) {
// Get the raw code from the remote
unsigned long code = IrReceiver.decodedIRData.decodedRawData;
Serial.print("Received IR code: 0x");
Serial.println(code, HEX);
// Compare the received code with known button codes
if (code == 0xB946FF00) { // Press UP Button
Forward();
} else if (code == 0xEA15FF00) { // Press Down Button
Backward();
} else if (code == 0xBB44FF00) { // Press Left Button
Left(); //
} else if (code == 0xBC43FF00) { // Press Right Button
Right(); //
} else if (code == 0xBA45FF00) { // Stop
Stop();
}
// Ready for the next code
IrReceiver.resume();
}
}
void Backward() {
motor4.run(BACKWARD);
motor3.run(BACKWARD);
//motor2.run(BACKWARD);
//motor1.run(BACKWARD);
Serial.println("Moving Backward");
}
void Forward() {
motor4.run(FORWARD);
motor3.run(FORWARD);
// motor2.run(FORWARD);
// motor1.run(FORWARD);
Serial.println("Moving Forward");
}
void Stop() {
motor4.run(RELEASE);
motor3.run(RELEASE);
//motor2.run(RELEASE);
//motor1.run(RELEASE);
Serial.println("Stop Motors");
}
void Left() {
motor4.run(FORWARD);
motor3.run(BACKWARD);
Serial.println("Moving Left");
}
void Right() {
motor4.run(BACKWARD);
motor3.run(FORWARD);
Serial.println("Moving Left");
}