/*
Arduino | general-help
CryCry — 7/1/24 at 2:44 PM
well i used an l298n motor driver and the error is
only one wheel is moving
TODO: add speed control
*/
#include <IRremote.hpp>
enum robotStates {
FWD, // 0, forward
LEFT, // 1, turn left
RIGHT, // 2, turn right
REV, // 3, reverse
STOP // 4, stop
};
// command codes (in wokwi, your codes may be different)
const int CMD_FWD = 2; // +
const int CMD_LEFT = 224; // <<
const int CMD_RIGHT = 144; // >>
const int CMD_REV = 152; // -
const int CMD_STOP = 168; // "Play"
// pin constants
const int IR_RECEIVE_PIN = 10;
// control pins, ENA, IN1, IN2, ENB, IN3, IN4
const int CONTROL_PINS[6] = {9, 8, 7, 3, 6, 5};
// state constants
const int STATE_PINS[5][6] = {
//ENA, IN1, IN2, ENB, IN3, IN4
{HIGH, HIGH, LOW, HIGH, HIGH, LOW}, //Fwd
{HIGH, LOW, HIGH, HIGH, HIGH, LOW}, //Left
{HIGH, HIGH, LOW, HIGH, LOW, HIGH}, //Right
{HIGH, LOW, HIGH, HIGH, LOW, HIGH}, //Rev
{LOW, LOW, LOW, LOW, LOW, LOW} //"Stop" - Motor off
// {HIGH, LOW, LOW, HIGH, LOW, LOW} //"Break" - Motor braked
};
void getCommand() {
if (IrReceiver.decode()) {
int rxValue = IrReceiver.decodedIRData.command;
Serial.print("Recieved code: ");
Serial.print(rxValue);
switch (rxValue) {
case CMD_FWD:
Serial.println("\tForward");
moveRobot(FWD);
break;
case CMD_LEFT:
Serial.println("\tLeft");
moveRobot(LEFT);
break;
case CMD_RIGHT:
Serial.println("\tRight");
moveRobot(RIGHT);
break;
case CMD_REV:
Serial.println("\tReverse");
moveRobot(REV);
break;
case CMD_STOP:
Serial.println("\tStop");
moveRobot(STOP);
break;
default:
Serial.println("\tUndefined");
moveRobot(STOP);
break;
}
IrReceiver.resume();
}
}
void moveRobot(int state) {
for (int index = 0; index < 6; index++) {
digitalWrite(CONTROL_PINS[index], STATE_PINS[state][index]);
}
}
void setup() {
Serial.begin(9600);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
for (int controlPin = 0; controlPin < 6; controlPin++) {
pinMode(CONTROL_PINS[controlPin], OUTPUT);
}
moveRobot(STOP);
Serial.println("Initialized to Stop");
}
void loop() {
getCommand();
}
ENA IN1 IN2
IN3 IN4 ENB