Hi,
I am working on a transmitter and a receiver using nrf24l01+. Now the receiver has a L293d motor driver and 2 dc motors powered by a couple of 18650s.The motors are toy dc and have a 0.1uf caps on their ends and I have a 10uf cap on the pin 8 and ground of the l293d. The transmitter has 4 buttons.
The rf is powered off 3.3V pin of the arduino and has a 10uf cap between the positive and the ground.
The nrf24l01 works fine, L293d works fine, but together, Now things are messed up.
I am using the Tmrh20 library for the rf.
Now if the delay on the receiver end is around 0-10 ms for the entire loop, the rf receives all packets.
As I add delay(10>delay>20) after the analogWrite(), the radio starts to lose packets. Now as the delay is greater than 20 the radio receives a packet and then stops receiving packets and the motors spins continuously according to the received packet.
If I dont add delay, the motors spin very slow. I add delay, the motor spins fast but I lose the rf.
I even tried new hardware(rf,driver ic and arduino) but still no good result.
Here is the code for the transmitter.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <printf.h>
#define CE_PIN 9
#define CSN_PIN 10
byte address[][6] = {"1Node"};
RF24 radio(CE_PIN,CSN_PIN);
char v;
char val;
char packet;
int up;
int down;
int left;
int right;
/*struct Packet{
char val;
int delayee;
};
Packet packet;*/
void setup() {
Serial.begin(115200);
val='4';
v='0';
//packet.delayee = 100;
pinMode(2,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
radio.begin();
printf_begin();
radio.setChannel(108);
radio.setPALevel(RF24_PA_MIN);
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe( address[0]);
radio.printDetails();
// put your setup code here, to run once:
}
void loop() {
//int timestart =millis();
//Serial.print("Start: ");
//Serial.println(timestart);
up=digitalRead(2);
down=digitalRead(3);
left=digitalRead(4);
right=digitalRead(5);
/*int doneRead = millis();
Serial.print("digitalRead Time: ");
Serial.println(doneRead - timestart);*/
if(up==LOW){
//packet.val = '0';
val = '0';
}
else if(down == LOW){
//packet.val ='1';
val = '1';
}
else if(left == LOW){
// packet.val = '2';
val = '2';
}
else if(right == LOW){
//packet.val = '3';
val = '3';
}
else{
//packet.val ='4';
val = '4';
}
/*Serial.println(packet.val);
radio.write(&val,sizeof(char));
//delay(20);
// Serial.println(millis()-doneRead);
/*if(Serial.available()){
serialByte=Serial.read();
}
else
serialByte='4';
//radio.write(&serialByte, sizeof(char));
if(!radio.write(&serialByte, sizeof(char)))
Serial.println("not sent");
delay(20);*/
/* put your main code here, to run repeatedly:*/
}
The code for the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <printf.h>
#define CE_PIN 7
#define CSN_PIN 8
RF24 radio(CE_PIN,CSN_PIN);
int ip1 = 3;
int ip2 = 5;
int ip3 = 6;
int ip4 = 9;
byte address[][6] = {"1Node"};
char val;
/*struct Packet{
char val;
int delayee;
};
Packet packet;*/
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
pinMode(ip1,OUTPUT);
pinMode(ip2,OUTPUT);
pinMode(ip3,OUTPUT);
pinMode(ip4,OUTPUT);
//Serial.println("SimpleRx Starting");
radio.begin();
printf_begin();
radio.setChannel(108);
radio.setPALevel(RF24_PA_MIN);
radio.setRetries(3,5);
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, address[0]);
radio.startListening();
radio.printDetails();
}
void loop() {
//int start = millis();
//char val = Serial.read();
if (radio.available()){
radio.read(&val,sizeof(char));
delay(30);
}
/* else{
packet.val='4';
// packet.delayee = 50;
}*/
/* int Read = millis();
Serial.println(packet.val);
Serial.println(Read - start);*/
//forward
if(val == '0')
{
//Serial.println("Forward");
analogWrite(ip1,200);
analogWrite(ip2,0);
analogWrite(ip3,200);
analogWrite(ip4,0);
//delay(packet.delayee);
}
//backward
else if (val == '1')
{
//Serial.println("back");
analogWrite(ip1,0);
analogWrite(ip2,200);
analogWrite(ip3,0);
analogWrite(ip4,200);
// delay(packet.delayee);
}
//left
else if (val == '2')
{
//Serial.println("Right");
analogWrite(ip1,200);
analogWrite(ip2,0);
analogWrite(ip3,0);
analogWrite(ip4,0);
//delay(packet.delayee);
}
//right
else if (val == '3')
{
//Serial.println("Left");
analogWrite(ip1,0);
analogWrite(ip2,0);
analogWrite(ip3,200);
analogWrite(ip4,0);
// delay(packet.delayee);
}
else if(val == '4')
{
//Serial.println("Stop");
analogWrite(ip1,0);
analogWrite(ip2,0);
analogWrite(ip3,0);
analogWrite(ip4,0);
//delay(packet.delayee);
}
/*Serial.print("Done loop in ");
Serial.println(millis() - Read);
delay(40);*/
// put your main code here, to run repeatedly:
}