Hello everyone, this is my first time on the forum, hopefully I set this up correctly. I have a project I have been working on, an RC tank using RF24. I have the tank working fine, transmitter is fine, I finally got that sorted and it was a monumental thing for me.
Now, I want to add a "pan/tilt" camera mount on it using two servos. I have the device printed and the servos connected, I am just having a hard time adding in this new servo code. I have been working on this for about a week, and have looked all over for code, I have found examples and got it working, though when I combine it with my existing code it never works.
I have attached what I feel like is my best attempt at combining them. Though with this sketch, power is finiicky and the motors will not even work anymore. Servos will work at the start and then stop.
I will put the backslashes in the code next to the added servo attempt, so you can see what the original is.
Another note, I am trying to get the servos to not retract back to their original position every time they are used. I have found some examples of this but never could transfer it. It is to move a camera, so if it resets back to the middle when I let go of the joystick it is a pain in the behind.
Thank you so much for reading, this thing has been driving me insane. Its my first relatively big Arduino project, I cant give up now!
Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>//////////////////
Servo Tilt;///////////////////
Servo Pan;///////////////////////
#define enA 6
#define in1 7
#define in2 5
#define enB 3
#define in3 4
#define in4 2
RF24 radio(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002"};
char receivedData[32] = "";
int xAxis, yAxis;
int motorSpeedA = 0;
int motorSpeedB = 0;
int joystick[4]; ////////// joystick was [2]
int servoAngleA = 0;/////////
int servoAngleB = 0; ////////////////////
int servo_pin = 8;//////////
int servo_pin2 = A1;/// with motor controller I am low on pins, thus A1,
void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address[1]);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
Tilt.attach(servo_pin); ///////////////////
Pan.attach(servo_pin2);//////////////////
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop() {
if (radio.available()) { // If the NRF240L01 module received data
radio.read( joystick, sizeof(joystick) );
radio.read(&receivedData, sizeof(receivedData));
yAxis = joystick[0];
xAxis = joystick[1];
servoAngleA = joystick[2]; //////////////
servoAngleB = joystick[3]; //////////////
Serial.println(yAxis);
Serial.println(xAxis);
Serial.println(servoAngleA); ////////////
Serial.println(servoAngleB); /////////////
}
if (yAxis < 470) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
if (xAxis < 470) {
int xMapped = map(xAxis, 470, 0, 0, 255);
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
int xMapped = map(xAxis, 550, 1023, 0, 255);
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}
////////////////////////////All of this is new below//////////////////////////////
if (receivedData[2] < 460) {
servoAngleA--;
Tilt.write(servoAngleA);
}
if (servoAngleA <= 10) {
servoAngleA = 10;
}
if (receivedData[2] > 564) {
servoAngleA++;
Tilt.write(servoAngleA);
}
if (servoAngleA >= 160) {
servoAngleA = 160;
}
if (receivedData[3] < 460) {
servoAngleB--;
Pan.write(servoAngleB);
}
if (servoAngleB <= 10) {
servoAngleB = 10;
}
if (receivedData[3] > 564) {
servoAngleB++;
Pan.write(servoAngleB);
}
if (servoAngleB >= 160) {
servoAngleB = 160;
}
}
Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9,10); // CE, CSN
const byte address[][6] = {"00001", "00002"};
char xyData[32] = "";
int joystick[4]; /////////// was at [2]
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address[1]);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
joystick[0] = analogRead(A4);
joystick[1] = analogRead(A3);
joystick[2] = analogRead(A0);/////////////
joystick[3] = analogRead(A1);/////////////////
radio.write( joystick, sizeof(joystick) );
}