Remote controlled gear shifting of heavy equipment

This is my one of the project for remote controlled gear shifting of heavy equipment using NRF24L01,ARDUINO UNO AND JOYSTICK SHIELD(7button)

Reciever Side

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <Servo.h>
/*-----( Declare Constants and Pin Numbers )-----*/
 // Create a Radio
#define CE_PIN 9
#define CSN_PIN 10 
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
const int servo1 = 6; // first servo
int servoVal; // variable to read the value from the analog pin
Servo myservo1; // create servo1
RF24 radio(CE_PIN, CSN_PIN);
/*-----( Declare Variables )-----*/
int joystick[8];  // 8 element array holding Joystick readings Xangle and 7 button state
int joystickX;
int joystickY;
int led1 = 2; // digital out put
int led2 = 3; // digital out put
int led3 = 4; // digital out put
int led4 = 5; // digital out put
int led5 = A4; // digital out put
int led6 = A1; // digital out put

void setup()   /****** SETUP: RUNS ONCE ******/
{
  
  myservo1.attach(servo1); // attaches the servo1 /analog output
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT); 
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
}                             //--(end setup )-
    
void loop ()   /****** LOOP: RUNS CONSTANTLY ******/
{
   
  radio.startListening();
  if ( radio.available() )
  {
      radio.read( &joystick, sizeof(joystick) );
      joystickX = map(joystick[0],0,1023,179,0); // turn value of 0-1023 to 0-180 degrees
      int upbut = joystick[1];
      int rvesbut = joystick[2];
      int downbut = joystick[3];
      int fwdbut = joystick[4];
      int nuetral = joystick[5];
      
      
  if (joystick[1]==LOW)
  {
     digitalWrite(led5, LOW);
     
     digitalWrite(led1, LOW);
     digitalWrite(led2, HIGH);
     digitalWrite(led6, HIGH);
  }
      else if (joystick[2]==LOW)
  {
        digitalWrite(led5, LOW);
        
        digitalWrite(led2, LOW);
        digitalWrite(led1, HIGH);
        digitalWrite(led6, HIGH);
  }
  
  else if (joystick[3]==LOW)
  {
       digitalWrite(led3, LOW);
  }
  else if (joystick[4]==LOW)
  {
       digitalWrite(led4, LOW);
  }
    
     else if (joystick[5]==LOW)
  {
       digitalWrite(led6, LOW);
       digitalWrite(led1, HIGH);
       digitalWrite(led2, HIGH);
  }
      
  else
  {
       digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
       digitalWrite(led5, HIGH);
  }
       servoVal = joystickX; // scale it to use it with the servo (result between 0 and 180)
       delay(50);
       myservo1.write(servoVal); // sets the servo position according to the scaled value
  }
  }

Transmitter Side

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10
#define JOYSTICK_X A0
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[8];  //  element array holding X ANGLE Joystick reading and 7 buttons
int upbut = 2;
int fwdbut = 3;
int downbut = 4;
int rvesbut = 5;
int nuetral = 8;
void setup()   /****** SETUP: RUNS ONCE ******/
{
 radio.begin();
 radio.openWritingPipe(pipe);
  pinMode(upbut,INPUT_PULLUP);//declare pinMode for all buttons and initial state
  digitalWrite(upbut,LOW);
  pinMode(rvesbut,INPUT_PULLUP);
  digitalWrite(rvesbut,LOW);
  pinMode(downbut,INPUT_PULLUP);
  digitalWrite(downbut,LOW);
  pinMode(fwdbut,INPUT_PULLUP);
  digitalWrite(fwdbut,LOW);
  pinMode(nuetral,INPUT_PULLUP);
  digitalWrite(nuetral,LOW);//end pinMode and digitalWrite
  
 }//--(end setup )---
                    
void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
 
  radio.stopListening();
  joystick[0] = analogRead(JOYSTICK_X);   // servo motor1 input
  joystick[1] = digitalRead(upbut);      //digital input
  joystick[2] = digitalRead(rvesbut);   //digital input
  joystick[3] = digitalRead(downbut);    // digital input
  joystick[4] = digitalRead(fwdbut);    // digital input
  joystick[5] = digitalRead(nuetral);     // digital input                                                                
  radio.write( &joystick, sizeof(joystick) );
 }//--(end main loop )---

Its working satisfactory.

Now i want to insert one more input button that is joystick[6].if joystick[6] input state ‘LOW’ all function will execute otherwise all inputs (joystick[0] to joystick[5]) will wait for input joystick[6] become ‘LOW’

Simply we can say vehicle Key switch.if key switch get ON, all operation will ‘ON’ otherwise ‘OFF’

please let me know what are the changes has to be done on existing programm.

all inputs (joystick[0] to joystick[5]) will wait for input joystick[6] become 'LOW'

That is silly. Inputs do NOT wait. YOU (make the Arduino) ignore the values when joystick[6] is not LOW.

You only need to change the transmitter side to not transmit anything if the joystick[6] value is not LOW.

PaulS: That is silly. Inputs do NOT wait. YOU (make the Arduino) ignore the values when joystick[6] is not LOW.

You only need to change the transmitter side to not transmit anything if the joystick[6] value is not LOW.

not clear.please explain

not clear.please explain

What is not clear?

Suppose that I want to kick your ass all over the place, but I'm not supposed to kick you unless you are wearing a blue shirt.

There is NOTHING you can do about me wanting to kick you. All you can do is decide whether to wear a blue shirt, or not.

You do NOT make inputs wait until some other input is in the desired state. The other inputs will do whatever the hell they feel like doing.

Think about your television. You can NOT make NBC stop broadcasting because YOU turned your TV off. When your TV is off, you don't care what NBC is broadcasting. It is only when your TV is on that you can't stand the fact that there is President Dump doing something else stupid, with NBC right there with their cameras focused on the twit.

if(joystick[6] == LOW)
{
   // Send the data
}
else
{
   // It does NOT matter what the other inputs are doing
}

PaulS: What is not clear?

Suppose that I want to kick your ass all over the place, but I'm not supposed to kick you unless you are wearing a blue shirt.

There is NOTHING you can do about me wanting to kick you. All you can do is decide whether to wear a blue shirt, or not.

You do NOT make inputs wait until some other input is in the desired state. The other inputs will do whatever the hell they feel like doing.

Think about your television. You can NOT make NBC stop broadcasting because YOU turned your TV off. When your TV is off, you don't care what NBC is broadcasting. It is only when your TV is on that you can't stand the fact that there is President Dump doing something else stupid, with NBC right there with their cameras focused on the twit.

if(joystick[6] == LOW)
{
   // Send the data
}
else
{
   // It does NOT matter what the other inputs are doing
}

yes its working..but problem is that push button is momentary switch once i removed the fingure from swtich,its become 'HIGH' all execution stopped.

Simply we can say vehicle Key switch.if key switch get ON, all operation will 'ON' otherwise 'OFF'

I have NEVER heard of a vehicle key switch that was momentary contact.

In any case, it IS possible to use the state change detection example principal, and use a momentary contact switch as a latching switching.

Do something, like set a boolean variable, goodToGo (initialized to false), to true when the switch becomes pressed. Send the data when goodToGo is true.

You can extend this to toggle the state of goodToGo when the switch becomes pressed, so that pressing it once makes goodToGo true, and pressing it again makes goodToGo false.

PaulS: .

You can extend this to toggle the state of goodToGo when the switch becomes pressed, so that pressing it once makes goodToGo true, and pressing it again makes goodToGo false.

sir,please give me an example

PaulS:
I have NEVER heard of a vehicle key switch that was momentary contact.

In any case, it IS possible to use the state change detection example principal, and use a momentary contact switch as a latching switching.

Do something, like set a boolean variable, goodToGo (initialized to false), to true when the switch becomes pressed. Send the data when goodToGo is true.

You can extend this to toggle the state of goodToGo when the switch becomes pressed, so that pressing it once makes goodToGo true, and pressing it again makes goodToGo false.
[/quote

Problem solved using boolean…

modified Transmitter code

#include <SPI.h>

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

/-----( Declare Constants and Pin Numbers )-----/
#define CE_PIN  9
#define CSN_PIN 10
#define JOYSTICK_X A0
const uint32_t debounceTime = 5;
const uint8_t key    = 7; 
const uint8_t Onled  = A4;
const bool switchOn  = false;   
const bool switchOff = true;

bool lastState  = switchOff;
bool newState    = switchOff;
bool toggleState = false;
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/-----( Declare Variables )-----/
int joystick[9];  //  element array holding X ANGLE Joystick reading and 7 buttons
int upbut = 2;
int fwdbut = 3;
int downbut = 4;
int rvesbut = 5;
int nuetral = 8;

void setup()  /****** SETUP: RUNS ONCE ******/
{
  radio.begin();
  radio.openWritingPipe(pipe);
  pinMode(Onled, OUTPUT);
  pinMode(upbut,INPUT_PULLUP);//declare pinMode for all buttons and initial state
  digitalWrite(upbut,LOW);
  pinMode(rvesbut,INPUT_PULLUP);
  digitalWrite(rvesbut,LOW);
  pinMode(downbut,INPUT_PULLUP);
  digitalWrite(downbut,LOW);
  pinMode(fwdbut,INPUT_PULLUP);
  digitalWrite(fwdbut,LOW);
  pinMode(nuetral,INPUT_PULLUP);
  digitalWrite(nuetral,LOW);//end pinMode and digitalWrite
  pinMode(key,INPUT_PULLUP);
  digitalWrite(key,LOW);
 
}//–(end setup )—

void loop()
  {

newState = digitalRead( key );

if( lastState != newState ) // state changed
  {
    delay( debounceTime );
    lastState = newState;
   
    // push on, push off
    if( newState == switchOn && toggleState == false )
    {
      toggleState = true;
      digitalWrite( Onled, HIGH );
   
    }
    else if( newState == switchOn && toggleState == true )
    {
      toggleState = false;
      digitalWrite( Onled, LOW );
     
    }

}  // if state changed
if (digitalRead(Onled) == HIGH){ 
  begin();
}
else {loop();}
}

void begin()  /****** LOOP: RUNS CONSTANTLY ******/
{

radio.stopListening();
 
  joystick[0] = analogRead(JOYSTICK_X);  // servo motor1 input
  joystick[1] = digitalRead(upbut);      //digital input
  joystick[2] = digitalRead(rvesbut);  //digital input
  joystick[3] = digitalRead(downbut);    // digital input
  joystick[4] = digitalRead(fwdbut);    // digital input
  joystick[5] = digitalRead(nuetral);    // digital input                                                               
  radio.write( &joystick, sizeof(joystick) );
  loop();
}//–(end main loop )—






Thnx