Last active
November 14, 2023 21:21
-
-
Save ShreyanJain9/b4f734f05df43c2727558c13892d9305 to your computer and use it in GitHub Desktop.
Arduino RC Car Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ROBOT_CONTROL_H | |
#define ROBOT_CONTROL_H | |
#include <ArxContainer.h> | |
#include <IRremote.hpp> | |
/*! | |
* @brief Alias for uint8_t to enhance code readability. | |
*/ | |
using uint8_t = u8; | |
/*! | |
* @brief Macro to enhance readability when using a range-based for loop. | |
*/ | |
#define in : | |
/*! | |
* @brief Macro to create a const auto reference for range-based for loops. | |
*/ | |
#define each const auto& | |
/*! | |
* @brief Macro to create a range-based for loop. | |
*/ | |
#define foreach(__loopover) for(const auto& __loopover) | |
/*! | |
* @brief Define the IR receiver pin. | |
*/ | |
#define IR_RECEIVE_PIN 8 | |
/*! | |
* @brief Define constants for forward and backward movements. | |
*/ | |
#define FORWARD_MOVEMENT 1, 0 | |
#define BACKWARD_MOVEMENT 0, 1 | |
/*! | |
* @brief The pin you write to to move the left wheel forwards. | |
*/ | |
#define leftForwardPin 2 | |
/*! | |
* @brief The pin you write to to move the left wheel backwards. | |
*/ | |
#define leftBackwardPin 3 | |
/*! | |
* @brief The pin you write to to move the right wheel forwards. | |
*/ | |
#define rightForwardPin 4 | |
/*! | |
* @brief The pin you write to to move the right wheel backwards. | |
*/ | |
#define rightBackwardPin 5 | |
#define SP0 25 // Speed buttons | |
#define SP1 69 | |
#define SP2 70 | |
#define SP3 71 | |
#define SP4 68 | |
#define SP5 64 | |
#define SP6 67 | |
#define SP7 7 | |
#define SP8 21 | |
#define SP9 9 | |
#define LEFTEYE_COMMAND 55 | |
#define RIGHTEYE_COMMAND 56 | |
#define LEFT_EYE_PIN 6 | |
#define RIGHT_EYE_PIN 7 | |
int* leftEyeState = &0; | |
int* rightEyeState = &0; | |
/*! | |
* @brief Function to control the motors. | |
* | |
* @param leftforward PWM value for the left forward motor. | |
* @param leftbackward PWM value for the left backward motor. | |
* @param rightforward PWM value for the right forward motor. | |
* @param rightbackward PWM value for the right backward motor. | |
*/ | |
void motor(int leftforward, int leftbackward, int rightforward, int rightbackward); | |
/*! | |
* @brief Map motor names to their corresponding pins. | |
*/ | |
const arx::map<String, int> motorPins { | |
{"leftForward", leftForwardPin}, | |
{"leftBackward", leftBackwardPin}, | |
{"rightForward", rightForwardPin}, | |
{"rightBackward", rightBackwardPin} | |
}; | |
/*! | |
* @brief The remote button to move forward. | |
*/ | |
#define FORWARD 24 | |
/*! | |
* @brief The remote button to move backward. | |
*/ | |
#define BACKWARD 82 | |
/*! | |
* @brief The remote button to turn left. | |
*/ | |
#define TURN_LEFT 8 | |
/*! | |
* @brief The remote button to turn right. | |
*/ | |
#define TURN_RIGHT 90 | |
/*! | |
* @brief The remote button to stop the car. | |
*/ | |
#define STOP 28 | |
/*! | |
* @brief Default speed for motor control. | |
*/ | |
int speed = 255; | |
/*! | |
* @brief Setup function to initialize pins and IR receiver. | |
*/ | |
void setup(); | |
/*! | |
* @brief Function to execute movement commands based on IR input. | |
* | |
* @param command The IR command received. | |
*/ | |
void pushWheel(int command); | |
/*! | |
* @brief Main loop to handle IR input and control the robot. | |
*/ | |
void loop(); | |
/*! | |
* @brief Function to get the speed to set the car to. | |
* | |
* @param command The IR command received. | |
* @returns The speed of the car. | |
*/ | |
int speedButtons(int command); | |
/*! | |
* @brief Function to set the speed of the car. | |
* | |
* @param speed The speed to set the car to, on a scale of 1 to 10. | |
*/ | |
void setSpeed(int speed); | |
/*! | |
* @brief Function to control the eyes. | |
* | |
* @param eyePin The pin to write to. | |
* @param pinState The state of the pin. 0 for off, 1 for on. Pointer to an int. | |
*/ | |
void eyes(int eyePin, int* pinState); | |
#endif // ROBOT_CONTROL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "IRRemote.h" | |
void motor(int leftforward, int leftbackward, int rightforward, int rightbackward) { | |
analogWrite(leftForwardPin, leftforward*speed); | |
analogWrite(leftBackwardPin, leftbackward*speed); | |
analogWrite(rightForwardPin, rightforward*speed); | |
analogWrite(rightBackwardPin, rightbackward*speed); | |
} | |
void setup() { | |
for(each pin in motorPins) | |
pinMode(pin.second, OUTPUT); | |
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver | |
Serial.begin(9600); | |
} | |
void eyes(int eyePin, int* pinState) { | |
if (*pinState) { | |
digitalWrite(eyePin, LOW); | |
*pinState = 0; | |
} else { | |
digitalWrite(eyePin, HIGH); | |
*pinState = 1; | |
} | |
} | |
void pushWheel(int command) { | |
switch(command) { | |
case FORWARD: | |
motor(FORWARD_MOVEMENT, FORWARD_MOVEMENT); | |
break; | |
case BACKWARD: | |
motor(BACKWARD_MOVEMENT, BACKWARD_MOVEMENT); | |
break; | |
case TURN_LEFT: | |
motor(FORWARD_MOVEMENT, BACKWARD_MOVEMENT); | |
break; | |
case TURN_RIGHT: | |
motor(BACKWARD_MOVEMENT, FORWARD_MOVEMENT); | |
break; | |
case STOP: | |
motor(LOW, LOW, LOW, LOW); | |
break; | |
default: | |
setSpeed(command); | |
break; | |
} | |
} | |
int speedButtons(int command) { | |
switch(command) { | |
case SP0: return 1; | |
case SP1: return 2; | |
case SP2: return 3; | |
case SP3: return 4; | |
case SP4: return 5; | |
case SP5: return 6; | |
case SP6: return 7; | |
case SP7: return 8; | |
case SP8: return 9; | |
case SP9: return 10; | |
} | |
} | |
void setSpeed(int command) { | |
swtich(command){ | |
case LEFTEYE_COMMAND: sendEye(command); | |
break; | |
case RIGHTEYE_COMMAND: sendEye(command); | |
break; | |
default: speed = static_cast<int>((((speedButtons(command))*10.0)/100)*255); | |
break; | |
} | |
} | |
void sendEye(int command) { | |
switch(command) { | |
case LEFTEYE_COMMAND: | |
eyes(LEFT_EYE_PIN, leftEyeState); | |
break; | |
case RIGHTEYE_COMMAND: | |
eyes(RIGHT_EYE_PIN, rightEyeState); | |
break; | |
} | |
} | |
void loop() { | |
if (IrReceiver.decode()) { | |
uint16_t command = IrReceiver.decodedIRData.command; | |
Serial.println(command); | |
pushWheel(command); | |
IrReceiver.resume(); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment