Try using map() before servo.write() and print the value returned. Is it what you expect ?
modified it to this (moving the map and adding an extra serial print)
#include "SPI.h"
#include "RF24.h"
#include "nRF24L01.h"
#include <Servo.h>
Servo servo;
#define CE_PIN 9
#define CSN_PIN 10
#define INTERVAL_MS_SIGNAL_LOST 1000
#define INTERVAL_MS_SIGNAL_RETRY 250
RF24 radio(CE_PIN, CSN_PIN);
const byte address[6] = "00001";
//NRF24L01 buffer limit is 32 bytes (max struct size)
struct payload {
byte pot;
};
payload payload;
unsigned long lastSignalMillis = 0;
void setup()
{
Serial.begin(9600);
servo.attach(2);
radio.begin();
//Append ACK packet from the receiving radio back to the transmitting radio
radio.setAutoAck(false); //(true|false)
//Set the transmission datarate
radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS)
//Greater level = more consumption = longer distance
radio.setPALevel(RF24_PA_MIN); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX)
//Default value is the maximum 32 bytes1
radio.setPayloadSize(sizeof(payload));
//Act as receiver
radio.openReadingPipe(0, address);
radio.startListening();
}
void loop()
{
unsigned long currentMillis = millis();
if (radio.available() > 0)
{
radio.read(&payload, sizeof(payload));
Serial.println("Received");
Serial.print("Pot:");
Serial.println(payload.pot);
map(payload.pot,0,255,0,180);
servo.write(payload.pot); //moving servo according to pot
Serial.println("Position:");
Serial.println(payload.pot);
lastSignalMillis = currentMillis;
}
if (currentMillis != 0 && currentMillis - lastSignalMillis > INTERVAL_MS_SIGNAL_LOST)
{
lostConnection();
}
}
void lostConnection()
{
Serial.println("We have lost connection, preventing unwanted behavior");
delay(INTERVAL_MS_SIGNAL_RETRY);
}
exactly the same problem but this has also revealed that the map command isn't mapping as the payload.pot value is the same before and after mapping.
I'm really out of my depth here...
The point of modifying the sketch to print the value was to see whether there was a problem with the map() function or your use of it
In this case it is the latter
map(payload.pot, 0, 255, 0, 180);
This line of code returns a value which you ignore. Try this instead
Serial.println(payload.pot);
byte position = map(payload.pot, 0, 255, 0, 180);
servo.write(position); //moving servo according to pot
Serial.println("Position:");
Serial.println(position);
lastSignalMillis = currentMillis;
ok that is now giving the correct value - ie 255 is converted to 180
Still the underlying problem that if the servo is at 255 and we send a low value it will move downwards but if we then send a high value it will not move upwards.
Was it moving in both directions while testing it with servo example code?
How is your servo powered and what servo you have?
Please post the Tx sketch that you are using to send data
going back to basics uploading servo sweep i stumbled into a note which lead me to some posts poiting out that the RF-Nano modules use a different chipset and run at 1/4 the speed of a normal nano. Using the workaround suggested there of adding
cli(); // This next section of code is timing critical, so interrupts are disabled
// Start the timed Sequence for configuring the clock prescaler
CLKPR = 0x80;
CLKPR = 0x01;
sei(); // Enable interrupts
into the setup section now means that the servo moves forwards and backwards as needed.
The only issue now is speed - whilst the transmitted data is updated several times per second the servo itself move very slowly and very juddery.
How is the servo powered ?
directly from the 6v supply, not from the board power.
What is supplying the 6V ?
Presumably the 6V supply and the Nano share a common GND connection
yes, a 6v block of C-Cells powering both the board and the servo
Where did you see this ?
i've been tinkering but can't seem to get any difference in how the servo reacts (slowly and jerky) any pointers or thoughts?
How is 6V connected to the Nano? Not sure about your Nano, but 6V to Vin on a classic Nano is a source of erratic operation, as it's too low for the regulator. Servo movements drag the 6V down momentarily,and the regulator can crap out.
bog standard servo on an RF Nano (which uses the LGT chip!)
The TX / RX is all sorted and working fine, position data (generated by a potentiometer so is linear data) received many times per second.
The servo itself has quite a delay on moving when it receives the position and twitches bounces back and forth a lot as it moves to the position. I'm completely out of my depth here and am struggling to find a way to make the servo more responsive and move smoother. but as a tinkerer and tweaker rather than a coder I'm struggling with how to smooth out the movement and improve the response time?
#include "SPI.h"
#include "RF24.h"
#include "nRF24L01.h"
#include <Servo.h>
Servo servo;
#define CE_PIN 9
#define CSN_PIN 10
#define INTERVAL_MS_SIGNAL_LOST 1000
#define INTERVAL_MS_SIGNAL_RETRY 250
RF24 radio(CE_PIN, CSN_PIN);
const byte address[6] = "00001";
//NRF24L01 buffer limit is 32 bytes (max struct size)
struct payload {
byte pot;
};
payload payload;
unsigned long lastSignalMillis = 0;
void setup() {
cli(); // This next section of code is timing critical, so interrupts are disabled
// Start the timed Sequence for configuring the clock prescaler
CLKPR = 0x80;
CLKPR = 0x01;
sei(); // Enable interrupts
Serial.begin(115200);
servo.attach(2);
radio.begin();
//Append ACK packet from the receiving radio back to the transmitting radio
radio.setAutoAck(false); //(true|false)
//Set the transmission datarate
radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS)
//Greater level = more consumption = longer distance
radio.setPALevel(RF24_PA_MIN); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX)
//Default value is the maximum 32 bytes1
radio.setPayloadSize(sizeof(payload));
//Act as receiver
radio.openReadingPipe(0, address);
radio.startListening();
}
void loop() {
unsigned long currentMillis = millis();
if (radio.available() > 0) {
radio.read(&payload, sizeof(payload));
Serial.println(payload.pot);
byte position = map(payload.pot, 0, 255, 0, 90); //map full potentiometer turn to 90degree servo movement
servo.write(position); //moving servo according to pot
Serial.println("Position:");
Serial.println(position);
lastSignalMillis = currentMillis;
}
if (currentMillis != 0 && currentMillis - lastSignalMillis > INTERVAL_MS_SIGNAL_LOST) {
lostConnection();
}
}
void lostConnection() {
Serial.println("We have lost connection, preventing unwanted behavior");
delay(INTERVAL_MS_SIGNAL_RETRY);
}
Twitchy servos often a symptom of power issues.
Schematic required. Pen on envelope, take photo, nothing fancy required.
New topic, same old problem??
4x c cells powering board and direct to servo rather than expecting board to power directly
See my comment, other thread. I've requested thread merge.