-
Notifications
You must be signed in to change notification settings - Fork 0
/
RC_Car_Receiver.ino
64 lines (52 loc) · 1.54 KB
/
RC_Car_Receiver.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
RF24 radio(7, 8); // Set CE and CSN pins
const byte address[6] = "696942069EA"; // Address of the transmitter NRF24L01 module
int motor1Pin1 = 2; // L298N motor driver input 1
int motor1Pin2 = 3; // L298N motor driver input 2
int motor2Pin1 = 4; // L298N motor driver input 3
int motor2Pin2 = 5; // L298N motor driver input 4
struct JoystickData {
int xAxis;
int yAxis;
};
JoystickData joystickData;
void setup() {
Serial.begin(9600);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
radio.begin();
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_HIGH); // Set power amplifier level to high
radio.startListening();
}
void loop() {
if (radio.available()) {
radio.read(&joystickData, sizeof(joystickData));
driveMotors();
}
}
void driveMotors() {
int speed = map(joystickData.yAxis, 0, 1023, -255, 255);
int rotation = map(joystickData.xAxis, 0, 1023, -255, 255);
int leftSpeed = speed + rotation;
int rightSpeed = speed - rotation;
if (leftSpeed >= 0) {
analogWrite(motor1Pin1, leftSpeed);
analogWrite(motor1Pin2, 0);
} else {
analogWrite(motor1Pin1, 0);
analogWrite(motor1Pin2, -leftSpeed);
}
if (rightSpeed >= 0) {
analogWrite(motor2Pin1, rightSpeed);
analogWrite(motor2Pin2, 0);
} else {
analogWrite(motor2Pin1, 0);
analogWrite(motor2Pin2, -rightSpeed);
}
}