I am using two nrf24I01 chips to connect a brush motor driver setup (using a L293D integrated circuit to power two motors). To test them, I am having one send "Hello World" messages to the motor one to test connectivity, and, frustratingly, it is only printing "Hello World" to the serial monitor every so often. Every ten seconds, maybe, with the other arduino sending messages at 1Hz. I suspect it is because my code is long enough to make it "miss" the packet because it was busy telling the motors how fast to go and such with analogWrite() and other housekeeping type stuff. But how are you supposed to make Arduinos do interesting things if they can't handle multiple tasks in series? Shouldn't there be a small queue in the nrf chip itself, which is what it queries when I call if(radio.available())? Anyhow, the code is below. Is there a better way to do this?
EDIT2: Fixed it. It changed out the maniacbug library with the TMRh20 one using the library manager in the IDE. Before I installed it I had to delete the library using this guide: How do I remove a library from the arduino environment? - Stack Overflow
The first two answers are good, I looked at the second.
Receiver/motor driver:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10);//housekeeping
const byte rxAddr[6] = "00001";//housekeeping
// rear motor variables
int enablePin = 11;
int in1Pin = 8;
int in2Pin = 7;
boolean forward = true;
//front motor variables
int enablePin2 = 2;
int in3Pin = 32;
int in4Pin = 34;
//test variables
boolean go = true;
void setup() {
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(enablePin2, OUTPUT);
while (!Serial);
Serial.begin(9600);
Serial.println("Serial on");
radio.begin();
radio.openReadingPipe(0, rxAddr);
Serial.println("Radio on");
radio.startListening();
Serial.println("Radio Listening");
}
void loop() {
if(radio.available())
{
char text[32] = {0};
radio.read(&text, sizeof(text));
Serial.println(text);
if(go){
//setSpeed(200);
//setDirection(forward);
setSpeed2(200);//like setSpeed for second motor
setDirection2(forward);//like setDirection for second motor
}else{
setSpeed(0);
forward = !forward;//change the direction!
setSpeed2(0);
forward = !forward;
}
}
}
void setSpeed(int speed) {
if(speed >= 0 && speed < 256){
analogWrite(enablePin, speed);
}
}
void setDirection(boolean clockwise){
digitalWrite(in1Pin, ! clockwise);
digitalWrite(in2Pin, clockwise);
}
transmitter code:
/*
* Author: Dan Pollak
* MEGA2560 because it is more portable. Probably.
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
const byte rxAddr[6] = "00001";
void setup()
{
radio.begin();
radio.setRetries(15, 15);
radio.openWritingPipe(rxAddr);
radio.stopListening();//because it is a transmitter, not a receiver
//Serial.begin(9600);
}
//FOR MEGA2560
void loop()
{
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
delay(1000);
}