So I've been testing my new robot circuits and I'm having a weird issue. When the nano is powered from USB, radio comms work fine, but when I switch over to battery power, I get a no connection error/the connection is intermittent (I hate that word). I'm using a special USB hub that can be switched off to disconnect the 5V line.
This is the code:
//By IceChes under MIT License
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
RF24 radio(10, 9); // CE, CSN of the RF nano
Servo esc; //Attach ESC as a servo
Servo drive1;
Servo drive2;
int num[32];
const byte address[6] = "00001"; //Channel 1
int escWrite;
int drive1Write;
int drive2Write;
unsigned long timer;
unsigned long lastSuccess;
void setup() {
Serial.begin(9600);
esc.attach(5, 1000, 2000); //Timings for ESC
drive1.attach(3, 1000, 2000);
drive2.attach(2, 1000, 2000);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX); //Amplify it
radio.startListening();
}
void loop() {
timer = millis();
if (radio.available()) {
drive1.attach(3, 1000, 2000);
drive2.attach(2, 1000, 2000);
esc.attach(5, 1000, 2000); //Timings for ESC
radio.read(&num, sizeof(num)); //Get packet and assign values
Serial.println(num[0]);
Serial.println(num[1]);
Serial.println(num[2]);
Serial.println(num[3]);
escWrite = map(num[0], 2, 1000, 0, 180); //Add speed limit to the ESC (50% here)
esc.write(escWrite);
if (num[1] > 700 || num[1] < 400) {
drive1Write = map(num[1], 2, 1000, 180, 0);
}
else {
drive1Write = 91;
}
if (num[2] > 700 || num[2] < 400) {
drive2Write = map(num[2], 2, 1000, 180, 0);
}
else {
drive2Write = 91;
}
drive1.write(drive1Write);
drive2.write(drive2Write);
lastSuccess = timer;
}
else { //Brake
if (timer - lastSuccess >= 1000) {
for (int escStop = escWrite; escStop >= 0; escStop--){
esc.write(escStop);
delay(30);
}
esc.detach();
drive1.detach();
drive2.detach();
Serial.println("no connection");
}
}
}
I am having the same problem with Robin2's test. Could this simply be noise? Or is something weirder happening?
I can't sketch up one right now but the NRF module is in one of those breakout boards with a 3.3V regulator. That's being powered from the ICSP header because the 5V pin is being fed by the BEC, which can supply up to 3A (or at least it's supposed to). No tank caps, but I can add one.
If there is a break in transmission for longer than 1000ms you want to stop the robot?
Show also the transmitter code.
Do you get the same problem if the robot is running without this special USB hub with switchable power? Maybe flash a led on a spare pin to give an error signal since you then won't have the serial monitor.
//By IceChes under MIT license
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int num[3]; //Configure packet to send
int left;
int right;
int value;
int invert;
RF24 radio(9, 10); // CE, CSN of RF nano
const byte address[6] = "00001"; //Channel one
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MAX); //All of the power
radio.stopListening();
}
void loop() {
value = analogRead(A0); //Read joysticks and pots
left = analogRead(A1);
right = analogRead(A3);
num[0] = value; //Assign values
num[1] = left;
num[2] = right;
radio.write(&num, sizeof(num));
}
EDIT - I am editing the code to both flip pin 7 high on disconnect and fix the array bug.
I'd probably add say a 50ms delay into the loop() of the transmitter to prevent it flooding the receiver.
On the receiver I'd also try cleaning out the radio's buffers if there is a failure of transmission. Add something like: while (radio.available()) { radio.read(&num, sizeof(num)); }
after printing "no connection " and possibly also consider a more general reset of the radio.
*Required if the regulator is located far from
the power supply filter.
**Although no output capacitor is needed
for stability, it does help transient response.
(If needed, use 0.1-μF, ceramic disc).