I guess this is a 2 part question: The first one on a specific design problem and the second one is more general:
My Servos fail to move when sending signal succesfully through NRF24L01 from a transmitter Tx which has 2 analog joysticks that write the signal to an arduino UNOtx, the signal is sent and succesfully recieved by arduino UNO on the reciever side Rx. I serialprint the values on the reciever that are suposedly being written on my output pins for the servos which are connected to working 5V supply (Ive tried directly f arduino, and external supply)… Here is the code for my reciever.
I am wondering if someone could help me identify my problem.
Note:
The servos work by themselves when I send wired signal and Servo.h library
Now I am using the SoftwareServo.h library to write on the servos.
My goal is to write these instructions to ESCs and control brushless motors but I am doing it this way first.
Any suggestions?
Thank you very much, help would be very much appreciated
/* NRF24L01 RECEPTION CODE
*
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 8
4 - CSN to Arduino pin 10
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
*/
/*-----( Import needed libraries )-----*/
#include <SoftwareServo.h> // Regular Servo library creates timer conflict!
// FROM RADIOHEAD LIBRARY
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
#define ServoHorizontalPIN 3 //Pin Numbers
#define ServoVerticalPIN 5
#define ServoMIN_H 0 // Don't go to very end of servo travel
#define ServoMAX_H 160 // which may not be all the way from 0 to 180.
#define ServoMIN_V 0 // Don't go to very end of servo travel
#define ServoMAX_V 140 // which may not be all the way from 0 to 180.
/*-----( Declare objects )-----*/
SoftwareServo HorizontalServo;
SoftwareServo VerticalServo; // create servo objects to control servos
// Create an instance of the radio driver
RH_NRF24 RadioDriver;
// Create an instance of a manager object to manage message delivery and receipt, using the driver declared above
RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS);
/*-----( Declare Variables )-----*/
int HorizontalJoystickReceived; // Variable to store received Joystick values
int HorizontalServoPosition; // variable to store the servo position
int VerticalJoystickReceived; // Variable to store received Joystick values
int VerticalServoPosition; // variable to store the servo position
uint8_t ReturnMessage[] = "JoyStick Data Received"; // 28 MAX
// Predefine the message buffer here: Don't put this on the stack:
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
//--------------------------------( SETUP Runs ONCE )-----------------------------------------------------
void setup()
{
/*-----( Set up servos )-----*/
HorizontalServo.attach(ServoHorizontalPIN); // attaches the servo to the servo object
VerticalServo.attach(ServoVerticalPIN); // attaches the servo to the servo object
Serial.begin(115200);
if (!RadioManager.init()) // Initialize radio. If NOT "1" received, it failed.
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
} // END Setup
//--------------------------------( LOOP runs continuously )-----------------------------------------------------
void loop()
{
if (RadioManager.available())
{
// Wait for a message addressed to us from the client
Serial.print(" RADIO MANAGER AVAILABLE : ");
Serial.println();
uint8_t len = sizeof(buf);
uint8_t from;
if (RadioManager.recvfromAck(buf, &len, &from))
//Serial Print the values of joystick
{
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": X = ");
Serial.println(buf[0]);
Serial.print(" Y = ");
Serial.println(buf[1]);
// Send a reply back to the originator client, check for error
if (!RadioManager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
Serial.println("sendtoWait failed");
}// end 'IF Received data Available
}// end 'IF RadioManager Available
{
SoftwareServo::refresh();//refreshes servo to keep them updating
HorizontalJoystickReceived = buf[1]; // Get the values received
VerticalJoystickReceived = buf[0];
// scale it to use it with the servo (value between MIN and MAX)
HorizontalServoPosition = map(HorizontalJoystickReceived, 0, 255, ServoMIN_H , ServoMAX_H);
VerticalServoPosition = map(VerticalJoystickReceived, 0, 255, ServoMIN_V , ServoMAX_V);
Serial.print("HorizontalServoPosition : ");
Serial.print(HorizontalServoPosition);
Serial.print(" VerticalServoPosition : ");
Serial.println(VerticalServoPosition);
// tell servos to go to position
HorizontalServo.write(HorizontalServoPosition);
VerticalServo.write(VerticalServoPosition);
delay(250); // wait for the servo to reach the position
}
}// END Main LOOP