Hi there. I'm trying to get a basic R/C tank setup going for my son. The setup will have one Servo in the mix too.
I started out by downloading and trying to work with the code from here, Even though the tank in the video seems to work as expected, when I read through the code I found a few flaws and my controller did not work as expected.
The biggest problem with this code was that there was no allowance for speed control. The motors were simply on/off for. For the project I am working on, I want to implement the INA pins on the L298N to control motor speed.
After I discovered that problem in the code, I went hunting for a further example to work from and found another person who had shared his code. This person used a very different method but also used an ESC instead of the L298N and only one motor. I was still able to gather some knowledge from this and try to piece together some code that would do what I want.
Somewhere I messed up. I have been trying to get this to work for a week now. I used the getting started page to make sure my radios could communicate with the hello world sketch.
I used a read potentiometer sketch to find out that the pots in the Airtronics radio i'm working with have a min. Value of around 320 and a max of around 577 it varies depending on the input I check. I also found I have to find the "rest" value, where the joysticks ended up when they were centered. This was not the center of the range so I modified the range a bit to make this "rest place" the middle of the range.
Here's the code I ended up with. The problem is that the motors don't move at all and, while the servo moves, it does not move as expected. When I push the joystick for it to the far left I can get it the servo to move slowly but as soon as I take it out of that corner it moves to the extreme opposite end. I feel I can get the servo fixed with mapping the value from the joysticks properly, so that's not my big concern right now. What the servo action does tell me is that the signal is coming through form the radio and I messed up the code on the receiver side.
code for transmitter.
/*
---- Transmitter Code ----
Mert Arduino Tutorial & Projects (YouTube)
Please Subscribe for Support
*/
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem
//define the input pins
int JOYSTICK0_X = A0; // Throttle input
int TJOYSTICK0_X = A5; // Throttle trim future use
int JOYSTICK0_Y = A3; // Rudder future use
int TJOYSTICK0_Y = A7; // Rudder trim
int JOYSTICK1_X = A1; // Elevator input
int TJOYSTICK1_X = A4; // elevator trim future use
int JOYSTICK1_Y = A2; // ailerons
int TJOYSTICK1_Y = A6; // aileron trim
int aux_switch0 = 2; // switches as labeled on controler
int aux_switch1 = 3;
int trainer_switch = 4;
//define variable values
int joystick[8]; // array containing 8 joystick readings.
int data[1];
RF24 radio(7,8); //7 and 8 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 Arduino.
void setup(void){
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
radio.begin(); //it activates the modem.
radio.openWritingPipe(pipe); //sets the address of the receiver to which the program will send data.
}
void loop(){
joystick[0] = analogRead(JOYSTICK0_X); // read Throttle input put into array slot 0
joystick[1] = analogRead(JOYSTICK0_Y); // Rudder to array
joystick[2] = analogRead(JOYSTICK1_X); // Elevator to array
joystick[3] = analogRead(JOYSTICK1_Y); //...
joystick[4] = analogRead(TJOYSTICK0_X);
joystick[5] = analogRead(TJOYSTICK0_Y);
joystick[6] = analogRead(TJOYSTICK1_X);
joystick[7] = analogRead(TJOYSTICK1_Y);
radio.write( joystick, sizeof(joystick) );
}
Code for Reciever
#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 A
const int RightMotorSpeed = 10; //enA for speed control
const int RightMotorForward = 2; // IN1
const int RightMotorBackward = 3; // IN2
//Motor B
const int LeftMotorSpeed = 5; //enB for speed control
const int LeftMotorForward = 4; // IN3
const int LeftMotorBackward = 6; // IN4
int LeftMSpeed; // interger to capture speed
int RightMSpeed; // to capture R speed
int BackSpeed;
int ForSpeed;
int rTurnSpeed;
int lTurnSpeed;
int LMotor;
int RMotor;
int joystick[8]; //The element array holding the data from joysticks
int fail = 0; // for when we lose radio range
//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(RightMotorSpeed, OUTPUT);
pinMode(LeftMotorSpeed, OUTPUT);
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
//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
LMotor = map(joystick[0], 439, 567, 0, 255); // map Joystick0_X
if(LMotor > 0 && LMotor <123); { //this is backwards
//Set Left motor backwards
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
analogWrite(LeftMSpeed, LMotor); // PWM signal for left motor speed
Serial.print("LMotor = ");
Serial.print(joystick[0]);}
if(LMotor > 127 && LMotor < 255); { //this is forwards
//Set Left motor forwards
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
analogWrite(LeftMSpeed, LMotor);
Serial.print("LMotor = ");
Serial.print(joystick[0]);}
}
{
RMotor = map(joystick[2], 491, 525, 0, 255); // map Joystick1_X
if(RMotor > 0 && RMotor < 123); { //this is backwards
//Set Right motor backwards
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
analogWrite(RightMSpeed, RMotor);
Serial.print("RMotor = ");
Serial.print(joystick[2]);}
if(RMotor > 127 && RMotor <255); { //this is forwards
//Set Left motor forwards
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
analogWrite(RightMSpeed, RMotor);
Serial.print("RMotor = ");
Serial.print(joystick[0]);}
}
}
else
{
Serial.println("No radio available");
}
// for the servo motor {
myServo.write(joystick[5]);
}
The serial output on receiver side shows data coming in through the respective joysticks. I know there are a lot of joysticks I'm not using, but I do plan to use this transmitter for other projects in the future.