RC Car using NRF24L01 , L293D with DC Motor

Hello,

i'm working to make RC Controlled by Arduino UNO by wireless connected through NRF24L01 at both the Ends
i'm using L293D to control the motor Speed and Direction

able to control the motors direction from TX UNO through RF,
but problem is when there is no input from TX, motor connected in RX UNO should stop, but it is not stopping. Continuously rotating on last command

here is code, kindly advise where i'm doing mistake

RX UNO code

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

// nRF24L01(+) radio attached using Getting Started board 
RF24 radio(9,10);

// Network uses that radio
RF24Network network(radio);

// Address of our node
const uint16_t this_node = 0;

// Address of the other node
const uint16_t other_node = 1;

char message;
// map motor poles to Arduino pins
#define Front_Motor_Pole1 3     // Front Motor Pins
#define Front_Motor_Pole2 4     // Front Motor Pins
#define Rear_Motor_Pole1 7      // Rear Motor Pins  
#define Rear_Motor_Pole2 8      // Rear Motor Pins

// map L293d motor enable pins to Arduino pins
#define Front_Enable_Pin 5   // Front Motor Speed Control Pin
#define Rear_Enable_Pin 6    // Rear Motor Speed Control Pin


int FrontMotor_Speed =  200;   // Front Motor speed
int RearMotor_Speed  =  250;   // Rear Motor speed


void setup(void)
{
  Serial.begin(57600);
  Serial.println("Enter your message");
  // set mapped L293D motor1 and motor 2 enable pins on Arduino to output (to turn on/off motor1 and motor2 via L293D)
  pinMode(Front_Enable_Pin, OUTPUT);    // Enable 
  pinMode(Rear_Enable_Pin, OUTPUT);

  // set mapped motor poles to Arduino pins (via L293D)
  pinMode(Front_Motor_Pole1 , OUTPUT);
  pinMode(Front_Motor_Pole2, OUTPUT);
  pinMode(Rear_Motor_Pole1, OUTPUT);
  pinMode(Rear_Motor_Pole2 , OUTPUT);
  
  
  
  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
}

void loop(void)
{
 
  // Pump the network regularly
  network.update();
  // Is there anything Data Ready from Other Node 
 while( network.available() )
  {
    // If so, grab it and print it out
    RF24NetworkHeader header;
    network.read(header,&message,sizeof(message));
    Serial.println(message);

			if( message == 'w')   // If Received 'w' then Call Forward Function
			 {
				  Forward();
			 }
			if(message == 'x')    // If Received 'x' then Call Reverse Function
			 {
				  Reverse();
			 }
			if( message = 'a')    // If Received 'a' then Call Left Function
			 {
			   Left_Turn();
			 }
		    if (message = 'd')    // If Received 'd' then Call Right Function
			 {
			  Right_Turn();
			 }
 } 
  	  
	  //If No Data received from Other Node - Motor gets Stopped
	  
	  Front_motorstop();
	  Rear_motorstop();

}

// MOTOR FUNCTIONS

void Front_motorstop()     // Stop Front Motor
  {
    digitalWrite(Front_Motor_Pole1, LOW);
    digitalWrite(Front_Motor_Pole2, LOW);
  }
void Rear_motorstop()    // Stop Rear Motor
  {
    digitalWrite(Rear_Motor_Pole1, LOW);
    digitalWrite(Rear_Motor_Pole2, LOW);
  }

void Forward()    // Move Forward
  {
    digitalWrite(Rear_Motor_Pole1, HIGH);
    digitalWrite(Rear_Motor_Pole2, LOW);
  }
void Reverse()   // Move Backward
  {
    digitalWrite(Rear_Motor_Pole1, LOW);
    digitalWrite(Rear_Motor_Pole2, HIGH);
  }
void Left_Turn()  // Turn Left
  {
    digitalWrite(Front_Motor_Pole1, LOW);
    digitalWrite(Front_Motor_Pole2, HIGH);
  }
void Right_Turn()  // Turn Right
  {
    digitalWrite(Front_Motor_Pole1, HIGH);
    digitalWrite(Front_Motor_Pole2, LOW);
  }

TX UNO Code

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

const int Front_buttonPin = 2;  // Front Button
const int Back_buttonPin  = 3;  // Back Button
const int Left_buttonPin  = 4;  // Left Button
const int Right_buttonPin = 5;  // Right Button


// nRF24L01(+) radio attached using Getting Started board 
RF24 radio(9,10);
char byteRead;
char message;
int i=0;

// Network uses that radio
RF24Network network(radio);

// Address of our node
const uint16_t this_node = 1;   // Address of JoyStick This Node(Transmitter)

// Address of the other node
const uint16_t other_node = 0;  // Address of Car Other Node (Receiver)

void setup(void)
{
  pinMode(Front_buttonPin, INPUT);  // Enable Buttons
  pinMode(Back_buttonPin, INPUT);   // Enable Buttons
  pinMode(Left_buttonPin, INPUT);   // Enable Buttons
  pinMode(Right_buttonPin, INPUT);  // Enable Buttons

  Serial.begin(57600);
  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
}

void loop(void)
{
  // Pump the network regularly
  network.update();
 if(digitalRead(Front_buttonPin) == 1)    // IF Front Button is Pressed - "w" is sent to Receiver
 {
       Serial.println("Front");
       byteRead = 'w';
       RF24NetworkHeader header(/*to node*/ other_node);
       bool ok = network.write(header,&byteRead,sizeof(&byteRead));
 
 }
 
 if(digitalRead(Back_buttonPin) == 1)     // IF Back Button is Pressed - "x" is sent to Receiver
 {
       Serial.println("Back");           
       byteRead = 'x';
       RF24NetworkHeader header(/*to node*/ other_node);
       bool ok = network.write(header,&byteRead,sizeof(&byteRead));
 }

 if(digitalRead(Left_buttonPin) == 1)      // IF Left Button is Pressed - "a" is sent to Receiver
{
       Serial.println("LeftTurn");
       byteRead = 'a';
       RF24NetworkHeader header(/*to node*/ other_node);
       bool ok = network.write(header,&byteRead,sizeof(&byteRead));

 } 

 if(digitalRead(Right_buttonPin) == 1)   // IF Right Button is Pressed - "d" is sent to Receiver
{
       Serial.println("RightTurn");
       byteRead = 'd';
       RF24NetworkHeader header(/*to node*/ other_node);
       bool ok = network.write(header,&byteRead,sizeof(&byteRead));
 }
}

Thanks
Jay

when there is no input from TX, motor connected in RX UNO should stop, but it is not stopping

What does the message variable contain when the connection drops ? Try setting it to a value that does not make the motors run when that happens.

@UKHeliBob Thanks for your reply

To save battery I am not sending any message when buttons are not pressed
Trying to identify the connect drop by checking the network data availability

while( network.available() )

Kindly advise :slight_smile:

You know when the network is available and read the data into the message variable and act on it. That must mean that you know when the network is not available, so when that happens set the message variable to something that is not one of your car commands.

Thanks for your prompt response
You can see in my code to stop the motor at end of while statement

	  //If No Data received from Other Node - Motor gets Stopped
	  
	  Front_motorstop();
	  Rear_motorstop();

Given how unreliable the nRF24L01+ transceiver communication is, it would probably be best to have the sender continue to send the current control command at regular intervals and have the receiver time out and bring the vehicle to a halt if the sequence of commands stops.

PeterH:
Given how unreliable the nRF24L01+ transceiver communication is,

Where has that notion come from? I have never found it unreliable.

The pair of programs in this link are derived from a model train control system. In the full version I send a message 10 times per second and if the receiver fails to receive 10 messages in succession it stops the train.

...R

How do I connect nrf24l01 transceiver and a L293D motor shield on the same arduino uno r3 board for all three to work simultaneously?

Cross-post
Thread locked.