Ok, I'm going nuts here. With the code as it is written bellow. I can use the right joystick to control right motor forward works ok. Reverse starts fast then slows down. That's an easy fix. left motor still does nothing.
Serial output is exactly as expected.
/*
---- Receiver Code ----
Mert Arduino Tutorial & Projects (YouTube)
Please Subscribe for Support
*/
#include <Servo.h> //the library which helps us to control the servo motor
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem (nRF24L)
//define our L298N control pins
//Motor Left
const int enA = 10; //enA for speed control
const int IN1 = 2; // IN1
const int IN2 = 3; // IN2
//Motor RIght
const int enB = 5; //enB for speed control
const int IN3 = 4; // IN3
const int IN4 = 6; // IN4
int LeftMSpeed; // interger to capture speed
int RightMSpeed; // to capture R speed
int LMotor;
int RMotor;
int joystick[8]; //The element array holding the data from joysticks
int fail = 0; // for when we lose radio range
int servo_pos;
bool newData = false; //
int leftAdcVal;
int leftMotorVal;
int rightAdcVal;
int rightMotorVal;
//define the servo name
Servo myServo;
RF24 radio(7, 8); /*This object represents a modem connected to the Arduino.
Arguments 5 and 10 are a digital pin numbers to which signals
CE and CSN are connected.*/
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.
void setup() {
Serial.begin(115200); // for outputting debuggin information to serial monitor
delay(3000);
Serial.println("Nrf24l01 Receiver Starting");
pinMode(enB, OUTPUT); // RIgnt Motor speed PWM
pinMode(enA, OUTPUT); //Left Motor Speed PWM
pinMode(IN3, OUTPUT); //Right Forward
pinMode(IN1, OUTPUT); //Left Forward
pinMode(IN2, OUTPUT); //Left Backward
pinMode(IN4, OUTPUT);//Right Backward
//define the servo input pins
myServo.attach(14); //A0
radio.begin(); //it activates the modem.
radio.openReadingPipe(1, pipe); //determines the address of our modem which receive data.
radio.startListening(); //enable receiving data via modem
}
void loop() {
if (radio.available() ) {
radio.read( joystick, sizeof(joystick) ); // Fetch the data payload
leftAdcVal = joystick[0]; // range 0 to 1023
leftMotorVal = leftAdcVal - 443; // range -124 to +124
rightAdcVal = joystick[2]; // range 0 to 1023
rightMotorVal = rightAdcVal - 443; // range -124 to +124
analogWrite(enA, leftMotorVal);
analogWrite(enB, rightMotorVal);
Serial.print("LMotor =");
Serial.print(leftMotorVal);
Serial.print(" RMotor =");
Serial.println(rightMotorVal);
}
LMotor = joystick[0]; // range 319 to 567
// leftMotorVal = leftAdcVal - 443; // range -64 to +64
// subtracting 513 (rather than 512) ensures that no later value will exceed 255
if (LMotor < 504) {//This is backwards
// LeftMSpeed = map(LMotor, 504, 414, 0, 255); //366
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
// analogWrite(enA, LeftMSpeed);
}
else if (LMotor > 504 && RMotor < 511) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
else if (LMotor > 511) {//This is forwards
// LeftMSpeed = map(LMotor, 511, 604, 0, 255);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
// analogWrite(enA, LeftMSpeed);
}
RMotor = joystick[2];
if (RMotor < 505) { //this is backwards
// RightMSpeed = map(RMotor, 505, 366, 0, 255);
//Set Right motor backwards
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
// analogWrite(enB, RightMSpeed);
}
else if (RMotor > 506 && RMotor < 512) {
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else if (RMotor > 512) { //this is forwards
// RightMSpeed = map(RMotor, 512, 660, 0, 255);
//Set Left motor forwards
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
// analogWrite(enB, RightMSpeed);
}
// for the servo motor
servo_pos = map(joystick[7], 0, 1024, 0, 255);
myServo.write(servo_pos);
}