Programming question

Hi ,
I was wondering is there a better way to do this code of TX to send high/Low of (2) buttons pressed from one arduino to the other? and receive them on the other side.

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"

// Define variables


RF24 radio(9, 10);
// Controller Address
const uint64_t Controller_1 = 0xE8E8F0F0E1LL;
uint8_t Command[4];//store 4 value in array - for 2 values it would be [0] and [1]
int rpin = 2;
int lpin = 3;

void setup()
{
  Serial.begin(9600);
  printf_begin();
  pinMode(lpin, INPUT); // A Button
  pinMode(rpin, INPUT); // B Button
  radio.begin();
  //speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
  radio.setDataRate(RF24_2MBPS);
  radio.setPALevel(RF24_PA_HIGH);
  radio.setChannel(3);
  radio.openWritingPipe(Controller_1);
  radio.setPayloadSize(32);
  radio.setCRCLength(RF24_CRC_16);
  radio.printDetails();
}

void loop()
{
  radio.stopListening();
  radio.powerUp();
  if (digitalRead(lpin) == LOW)
  {
    Serial.println("lswitch LOW");
    Command[0] = 0;
    radio.write(Command, 0);
  }
  else
  {
    Serial.println("lswitch HIGH");
    Command[1] = 1;
    radio.write(Command, 1);
  }

    if (digitalRead(rpin) == LOW)
  {
    Serial.println("rswitch LOW");
    Command[2] = 0;
    radio.write(Command, 0);
  }
  else
  {
    Serial.println("rswitch HIGH");
    Command[3] = 1;
    radio.write(Command, 1);
  }
  radio.powerDown();
}

destiny2008:
I was wondering is there a better way to do this code

Does it work?

Is there something about its performance that you don't like ?

...R

performance and not 100% working , looking for something simple straight forward if possible

Do you have your button pins pulled up (or down)?

down... shorting to 5V - they will eventually be replaced by input voltage of 12Vdc of ground on pins 2 and 3

coming from a car

You are using 4 bytes (32 bits) to send 4 bits of data. Yes, there is a better way. bitSet() bears looking into.

so I came up with this works 95% but glitchy as far as the buttons noisy while pressed...

tx

int Left_Joy_Button = LOW;
int Right_Joy_Button = LOW;


RF24 radio(9, 10);
// Controller Address
const uint64_t Controller_1 = 0xE8E8F0F0E1LL;
uint8_t command[2];//store 4 value in array - for 2 values it would be [0] and [1]
int rpin = 2;
int lpin = 3;

void setup()
{
  Serial.begin(9600);
  printf_begin();
  pinMode(lpin, INPUT); // A Button
  pinMode(rpin, INPUT); // B Button
  radio.begin();
  //speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
  radio.setDataRate(RF24_2MBPS);
  radio.setPALevel(RF24_PA_HIGH);
  radio.setChannel(3);
  radio.openWritingPipe(Controller_1);
  radio.setPayloadSize(32);
  radio.setCRCLength(RF24_CRC_16);
  radio.printDetails();
}

void loop()
{  
  delay(0);
  Check_Buttons();
}

void Check_Buttons()
{
  radio.stopListening();
  radio.powerUp();
  // Check Joystick Switches
  Left_Joy_Button = digitalRead(rpin);
  Right_Joy_Button = digitalRead(lpin);

  if (Right_Joy_Button == HIGH)
  {
    command[0] = 1; // Command to turn on right indicator
    Serial.print("R");
  }
  else {
    command[0] = 0; // Turn off Indicators
  }

  if (Left_Joy_Button == HIGH)
  {
    command[1] = 1; // Command to turn on right indicator
    Serial.print("L");
  }
  else {
    command[1] = 0; // Turn off Indicators
  }
  Send_Data();
}

// Function to send the data
void Send_Data()
{
  radio.write(command, sizeof(command));
  radio.powerDown();
}

rx:

// Variables used in the program
int Data_Val_1 = 0; // Stores the data value
int Data_Val_2 = 0; // Stores the data value
int Data_Val_3 = 0; // Stores the data value
int Data_Val_4 = 0; // Stores the data value

//*****************************************************************************
long OnTime = 250;   // milliseconds of on-time
long OffTime = 250;  // milliseconds of off-time
int ledState1 = 0;

long OnTime1 = 250;   // milliseconds of on-time
long OffTime1 = 250;  // milliseconds of off-time
int ledState2 = 0;
//*****************************************************************************

RF24 radio(9, 10);
// Controller Addresses
const uint64_t Controller_2 = 0xE8E8F0F0E1LL;
uint8_t Command[2];//store 1 value in array - for 2 values it would be [0] and [1]
int buttona;
int buttonb;


const int led_A_Interval = 0;
const int led_B_Interval = 0;
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousLed_A_Millis = 0;
unsigned long previousLed_B_Millis = 0;

unsigned long previousButtonMillis = 0; // time when button press last checked
unsigned long previousButtonMillis1 = 0; // time when button press last checked

const int blinkDuration = 200; // number of millisecs that Led's are on - all three leds use this
const int buttonInterval = 10; // number of millisecs between button readings

void setup()
{
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.setDataRate(RF24_2MBPS);
  radio.setPALevel(RF24_PA_HIGH);
  radio.setChannel(3);
  radio.openReadingPipe(1, Controller_2);
  radio.setPayloadSize(32);
  radio.setCRCLength(RF24_CRC_16);
  radio.startListening();
  radio.printDetails();
  pinMode(2, OUTPUT); // Front Work lights
  pinMode(3, OUTPUT); // Rear Work Lights
  pinMode(4, OUTPUT); // Rear Work Lights
  pinMode(5, OUTPUT); // Rear Work Lights
  pinMode(6, OUTPUT); // Rear Work Lights
  pinMode(7, OUTPUT); // Rear Work Lights

}

void loop()
{
  currentMillis = millis();
  if (radio.available())
  {
    //Dump the payloads until we've gotten everything
    bool done = false;
    while (!done)
    {
      done = radio.read(Command, sizeof(Command));
      //Serial.println(radio.read(Command, sizeof(Command)));

    }
    LED();
    //LED_Control();
    //LED_Control2();
  }
}

void LED()
{

  buttona = Command[0]; // Store the controller left
  buttonb = Command[1]; // Store the controller left

  if (buttona == 0)
  {
    digitalWrite(2, LOW);
    Serial.println(buttona);
  }
  if (buttona == 1)
  {
    digitalWrite(2, HIGH);
    Serial.println(buttona);
  }
    if (buttonb == 0)
  {
    digitalWrite(3, LOW);
    Serial.println(buttonb);
  }
    if (buttonb == 1)
  {
    digitalWrite(3, HIGH);
    Serial.println(buttonb);
  }
}

Is it a good idea to power the radio up and down so fast? Why not leave it on?

You should either debounce the buttons in software or put a capacitor across each.

what capacitor value , any schematic to give an idea?

payloadsize 32 bytes to send 2 bits.. I's send one byte (or two, if you dont want to handle bits)

destiny2008:
what capacitor value , any schematic to give an idea?

i am noob at this , all i am looking for is to send value 0 and 1 (high and Low) x 2 buttons.. 4 bits.

I am actually using currently Pull-down resistor setup per your link

If you look through the blog for the part that says

Hardware debounce

you will see the circuit and very short text that goes with it. Capacitor used is 1uF and saves you learning debounce code.

In the blog is also the valuable lesson about detecting pin change (transition) rather than simply up or down.

Also note that the hardware debounce schematic needs pin 8 to be pinMode( 8, INPUT_PULLUP );

Reason is that the AVR has internal pullup resistors 20K to 50K enabled by the mode. The switch goes straight to ground safely. I use this to make jumpers be buttons, ground them on the UNO USB port, the metal box.

  delay(0);

Glitchy, eh?

You can do:

command[1] = digitalRead(lpin);

And similar.

PaulS:

  delay(0);

Glitchy, eh?

Took delay out , same thing, no diffrence

Yes.

destiny2008:
down... shorting to 5V - they will eventually be replaced by input voltage of 12Vdc of ground on pins 2 and 3

coming from a car

You might want to use opto-isolators considering how variable/dirty car power is. They're cheap and effective.

finally rounded it up to this ... but the transmit is not stable at all in many scenarios , when i keep pressing on button 2 it triggers button 3 with it randomly .. in the serial print and through leds

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"

int joystick[2];

RF24 radio(9, 10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
// The various roles supported by this sketch
typedef enum { role_transmit = 1, role_Receive } role_e;

// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "transmit", "Receive"};

// The role of the current running sketch
role_e role = role_Receive;
//role_e role = role_transmit;


void setup(void)
{
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
//  radio.setChannel(3);
//  radio.setPayloadSize(32);
//  radio.setCRCLength(RF24_CRC_16);
  radio.printDetails();


  if ( role == role_transmit )
  {
    Serial.println("Transmit Mode");
    radio.openWritingPipe(pipe);
    radio.stopListening();
    pinMode(2, INPUT);
    pinMode(3, INPUT);

  }
  if ( role == role_Receive )
  {
    Serial.println("Receive Mode");
    radio.openReadingPipe(1, pipe);
    radio.startListening();
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
//    digitalWrite(2, LOW);
//    digitalWrite(3, LOW);
//    digitalWrite(4, LOW);
//    digitalWrite(5, LOW);
//    digitalWrite(6, LOW);
//    digitalWrite(7, LOW);

  }
}


void loop(void)
{
  if ( role == role_transmit )
  {
    joystick[0] = digitalRead(2);
    joystick[1] = digitalRead(3);
    radio.write( joystick, sizeof(joystick) );

  }

  if ( role == role_Receive )
  {
    if ( radio.available() )
    {
      // Dump the payloads until we've gotten everything
      bool done = false;
      while (!done)
      {
        // Fetch the payload, and see if this was the last one.
        done = radio.read( joystick, sizeof(joystick) );
        if (joystick[0] == 1)
        {
          LED();
          //Serial.println("ButtonA");
        }
        if (joystick[1] == 1)
        {
          DEL();
          //Serial.println("ButtonB");
        }
      }
    }
  }
}
void LED() {
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(1);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);


}
void DEL() {
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(1);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
}