I have been working on a 3d printed rc boat using two dc motors and a nrf24lo1 module. I can't seem to get the code to work and it is getting very frustrating.
here is the transmitter code:
#include "RF24.h"
#include <SPI.h>
RF24 radio(7, 8);
byte addresses[][6] = {"1Node","2Node"};
int buttonPinL = 3;
int buttonPinR = 4;
int motorL;
int motorR;
void setup() {
Serial.begin(9600);
Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setDataRate(RF24_2MBPS);
radio.setChannel(124);
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
}
void loop() {
//*********************************************************
if(digitalRead(buttonPinL)==HIGH)
{
byte motorL = 1;
while(digitalRead(buttonPinL)==HIGH);
}
else
{
byte motorL = 0;
}
//************************************************************
if(digitalRead(buttonPinR)==HIGH)
{
byte motorR = 1;
while(digitalRead(buttonPinR)==HIGH);
}
else
{
byte motorR = 0;
}
//***************************************************************
uint8_t data[2];
data[0] = motorL;
data[1] = motorR;
(radio.write( data, sizeof data));
Serial.print("Sent: ");
Serial.print(motorL);
Serial.print(' ');
Serial.println(motorR);
}
and here is the receiver code:
#include "RF24.h"
#include <SPI.h>
RF24 radio(7, 8);
byte addresses[][6] = {"1Node", "2Node"};
int leftMotor = 3;
int rightMotor = 4;
void setup()
{
Serial.begin(9600);
Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setDataRate(RF24_2MBPS);
radio.setChannel(124);
radio.openReadingPipe(1, addresses[1]);
radio.startListening();
pinMode(leftMotor, OUTPUT);
pinMode(rightMotor, OUTPUT);
}
void loop()
{
unsigned char data[2];
if (radio.available())
{
radio.read(data, sizeof data);
int motorL = data[0];
int motorR = data[1];
Serial.print("Recieved: ");
Serial.print(motorL);
Serial.print(' ');
Serial.println(motorR);
//*********************************************************
if ((motorL)==1)
{
digitalWrite(leftMotor, HIGH);
while((motorL)==1);
}
else{
digitalWrite(leftMotor, LOW);
}
//*********************************************************
if ((motorR)==1)
{
digitalWrite(rightMotor, HIGH);
while((motorR)==1);
}
else{
digitalWrite(rightMotor, LOW);
}
//*********************************************************
}
}
A schematic/circuit diagram would be more useful. If you're trying to drive those motors direct from digital pins that's never going to work and will probably damage the Arduino.
If not, how are you driving them? Do they work when you write a simple test code that just writes directly to pins 3 and 4?
int leftMotor = 3;
void setup() {
// put your setup code here, to run once:
pinMode(leftMotor, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(leftMotor, HIGH);
delay(1000);
digitalWrite(leftMotor, LOW);
delay(1000);
}
this is the code that I used to test the motors and they twitched a bit but didn't spin
If the motors only need to turn in one direction you could use MOSFETs. If they need to go forward/reverse then an H-bridge motor driver is what you need. There are thousands of examples of both online.
Hi,
Do you have a DMM?
If so can you please measure the battery voltage when you command a TX?
You appear to be using the NRF24l01 plus.
You would be best to keep ALL other wires away from the antenna, and point it up out of the box.
Any objects around the antennas will cause detuning of the antenna.
Hi,
Possibly time to get a cheap one with just the basics measurements.
Is that NRF with its own adapter PCB?
If so are you feeding it 5V or 3V3 at the input pins.