hello everyone,
I am trying to program an arduino robot which i build using the NRF24L01 transceiver and this motor driver : https://www.aliexpress.com/item/2-channel-DC-motor-driving-module-Positive-inversion-PWM-speed-mini-motor-dual-h-bridge-stepper/32811507123.html?spm=a2g0s.9042311.0.0.zBqTl0
what I am trying to do is controlling the robot using a joystick potentiometers for forward, backward, left and right with the same stick.
The robot has 2 motorized wheels and 1 castor, so the steering is by differential driver
I managed to make the wireless connection between the 2 NRF24L01 modules and receiving the potentiometers values from both pots of the joystick. I managed too, to make the motors go forward alone and backward alone but i did not manage to make them turn while pushing the throttle forward or backward at the same time ! The results I need to reach is to hit the throttle in an analog form and make the robot go forward gradually with speed controlling and and same for turning like an RC car. So I need help please with the software which i am not able so far to write. Below is the Receiver side which is the problem, the transmitter side is successfully written and all it does is sending the values of each of the two potentiometers so please help me write the software for motor forward backward left and right :
#include <SPI.h>
//#include <nRF24L01.h>
#include <RF24.h>
int msgRX[2]; //Message to be transmitted, can contain up to 2 array elements, 2 bytes
int ackMessage[2]; //Acknowledgment message, means the message that will be received from the receiver or the car, 1 element for the moment
RF24 radio(7, 8); // CE, CSN
//const byte address[6] = "00001";
//Defining the radio variables and values
const uint64_t pipe = 0xE8E8F0F0E1LL; //pipe address
const rf24_datarate_e dataRate = RF24_250KBPS; //Data rate defined in the documentations, RF24_250KBPS, RF24_1MBPS or RF24_2MBPS
const int motorPin1 = 3;
const int motorPin2 = 10;
const int motorPin3 = 5;
const int motorPin4 = 6;
int Throttle;
int Steering;
int Throttlemap;
int Motor2Steering;
void setup() {
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
//Serial.begin(9600);
radio.begin();
radio.setDataRate(RF24_250KBPS);
//radio.openReadingPipe(0, address);
radio.openReadingPipe(1, pipe);
//radio.setPALevel(RF24_PA_MAX);//Power Amplifier (PA) level to one of four levels RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
radio.enableAckPayload();
radio.startListening();
}
void loop() {
//ackMessage[1] = 200;
//radio.writeAckPayload(1, ackMessage, sizeof(ackMessage));
while (radio.available()) {
radio.read(msgRX, sizeof(msgRX));
Throttle = msgRX[0]; //middle throttle value is 121
Steering = msgRX[1]; //middle steering value is 125
//FORWARD or BACKWARD
if (Throttle <= 121 && Steering == 125) {
Throttlemap = map(Throttle, 0, 121, 255, 0);
// RIGHT MOTOR
analogWrite(motorPin1, Throttlemap); // Right Motor forward
analogWrite(motorPin2, 0);
// LEFT MOTOR
analogWrite(motorPin3, Throttlemap); // Left Motor forward
analogWrite(motorPin4, 0);
} else if (Throttle > 121 && Steering == 125) {
Throttlemap = map(Throttle, 121, 255, 0, 255);
analogWrite(motorPin1, 0);
analogWrite(motorPin2, Throttlemap);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, Throttlemap);
} else if (Throttle > 121 && Steering == 125){
} else if (Throttle == 121 && Steering == 125){
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, 0);
}
}
}