Array usage

Hi,
I am using
digitalread on input 2,3,4 on 1 arduino
digitalwrite on another arduino to trigger led 2,3,4. in consequence.
i want to trigger input 2 on A and light 2 on B and so on. if I press 3 led 3 should light at the other end.
i can do it by sending a message each time input is triggered , but cannot figure out by using Array. i am trying to use nrf24l01 led_remote example (trying to modify)
because i want to have 1 program transceiver.

any help on code example?
the idea:
On the Tx read the pins in the array using a for loop. If any of them have changed since the last time they were read then send the array.

On the Rx, when the array is received read the values using a for loop and set the state of the LED pins

Where are you stuck ?

On the Tx read the pins in the array using a for loop. If any of them have changed since the last time they were read then send the array.

On the Rx, when the array is received read the values using a for loop and set the state of the LED pins

@OP

1. When we read the signals of DPin-2, 3, and 4 of UNO-A, save them on sparate variables, we get the following:

bool x2 = digitalRead(2);
bool x3 = digitalRead(3);
bool x4 = digitalRead(4);

2. An array is a 'packet' of similar things, where they together and one after another. Let us put the variable of Step-1 in the following array named myButton.

bool myButton[3];
for(int i= 0, j = 2; i<3, j<5; i++, j++)
{
   myButton[i] = digitalRead(j);   //logic value of DPin-2 (3, 4) goes to array-element myButton[0],... 
}

UK Heli , can you provide that piece of the code ? i am stuck at this part

destiny2008:
UK Heli , can you provide that piece of the code ? i am stuck at this part

You send the array to the NRF. At the receiver, collect the array and write its contents on the Led array.

bool x2 = digitalRead(2);
bool x3 = digitalRead(3);
bool x4 = digitalRead(4);

Why are these variables bools ?
digitalRead() does not return a bool

How will the code look like?
Tx and Rx part

destiny2008:
How will the code look like?
Tx and Rx part

I'm not sure, but it won't include this nonsense from @ GolamMostafa

for(int i= 0, j = 2; i<3, j<5; i++, j++)

AWOL:
I'm not sure, but it won't include this nonsense from @ GolamMostafa

for(int i= 0, j = 2; i<3, j<5; i++, j++)

LOL :slight_smile: UK HELI had the right idea just missing code example

My Code till now ...

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


RF24 radio(9, 10);
#define NUM_ITEMS 3
// sets the role of this unit in hardware.  Connect to GND to be the 'led' board receiver
// Leave open to be the 'remote' transmitter
const int role_pin = A3;

// Pins on the remote for buttons
const uint8_t button_pins[NUM_ITEMS] = { 2, 3, 4 };
const uint8_t num_button_pins = sizeof(button_pins);
// Pins on the LED board for LED's
const uint8_t led_pins[NUM_ITEMS] = { 2, 3, 4 };
const uint8_t num_led_pins = sizeof(led_pins);

//
// Topology
//

// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;

//
// Role management
//

typedef enum { role_remote = 1, role_led } role_e;

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

// The role of the current running sketch
role_e role;

//
// Payload
//

uint8_t state[num_button_pins];
uint8_t led_states[num_led_pins];

//
// Setup
//

void setup(void)
{
  //
  // Role
  //
  // set up the role pin
  pinMode(role_pin, INPUT);
  digitalWrite(role_pin, HIGH);
  delay(20); // Just to get a solid reading on the role pin
  // read the address pin, establish our role
  if ( digitalRead(role_pin) )
    role = role_remote;
  else
    role = role_led;

  //
  // Print preamble
  //
  Serial.begin(115200);
  printf_begin();
  printf("\n\rBy Paul Transceiver\n\r");
  printf("ROLE: %s\n\r", role_friendly_name[role]);
  radio.begin();
  radio.setPALevel(RF24_PA_HIGH);
  radio.setDataRate(RF24_1MBPS);
  radio.setCRCLength(RF24_CRC_16);
  //
  // Setup and configure rf radio
  //

  if ( role == role_remote )
  {
    radio.openWritingPipe(pipe);
    radio.stopListening();

    for (int i = 0; i < NUM_ITEMS; i++)
    {
      pinMode(button_pins[i], INPUT);
      digitalWrite(button_pins[i], LOW);
    }

  }
  else if ( role == role_led )
  {
    radio.openReadingPipe(0, pipe);
    radio.startListening();

    for (int i = 0; i < NUM_ITEMS; i++)
    {
      pinMode(led_pins[i], OUTPUT);
      digitalWrite(led_pins[i], LOW);
    }
  }

  radio.printDetails();
}

//
// Loop
//

void loop(void)
{
  if ( role == role_remote )
  {
    for (int i = 0 ; i < NUM_ITEMS ; ++i)
    {
      uint8_t state = digitalRead(button_pins[i]);
      if (state == HIGH)
      {
        printf("Now sending...");
        bool ok = radio.write( state, button_pins[i] );
        if (ok)
          printf("ok\n\r");
        else
          printf("Check Radio Receiver is ON\n\r");
        // Try again in a short while
      }
      delay(20);
    }
  }

  //*************************************************************************
  //*************************************************************************

  if ( role == role_led )
  {
    int i = num_button_pins;
    uint8_t state = digitalRead(button_pins[i]);
    // if there is data ready
    if (radio.available())
    {
      // Dump the payloads until we've gotten everything
      radio.read( state, num_button_pins );
      // Spew it

      for (int i = 0 ; i < NUM_ITEMS ; ++i)
      {
        digitalWrite(led_pins[i], state);
      }
    }
  }
}

...and?

AWOL:
...and?

not working as expected!

UKHeliBob:

bool x2 = digitalRead(2);

bool x3 = digitalRead(3);
bool x4 = digitalRead(4);



Why are these variables bools ?
digitalRead() does not return a bool

Logical common sense -- I am reading 1-bit data from DPin-2; so, I like to save it in 1-bit variable. Do you you think that it does not work for me?

SM73.png

The digitalRead(); returns HIGH or LOW. 0x51 is HIGH compare to 0x03. 1 is HIGH compare to 0. bool for boolean -- a boolean variable can have either of the two states HIGH or LOW; so, it is a two valued variable. The declaration of bool x2; is not challenged by the Arduino IDE/Compiler; so, I am going ahead.

SM73.png

I like to save it in 1-bit variable

There ain't no such thing.

Nice thought though

AWOL:
There ain't no such thing.

Nice thought though

It is not there; but, it has not been challenged by the Arduino IDE/Compiler. Even the following codes that you have termed as 'n...' (I don't want to repeat that word as that vocabulary goes in the very wrong way in our local mindset)' are compiled, uploaded, and executed; it provides fantastic results.

bool myButton[3];
  for (int i = 0, j = 2; i < 3, j < 5; i++, j++)
  {
    myButton[i] = digitalRead(j);   //logic value of DPin-2 (3, 4) goes to array-element myButton[0],...
  }
  Serial.println(myButton[0], BIN);

Let me reiterate, losing in the process, the vernacular:

{clears throat}

There is no such thing as a single bit variable in C/C++.

for (int i = 0, j = 2; i < 3, j < 5; i++, j++) Again?

destiny2008:
not working as expected!

You are making things look more complicated than they need be by combining two roles in one sketch. I find it better to write separate Tx and Rx programs

Have these on me (based on Robin2's excellent thread simple RF24)

Tx

// SimpleTx - the master or the transmitter

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

//not all #defines used but here for documentation
#define CE_PIN   9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char txNum = '0';

byte pinStates[] = {};

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;

void setup()
{
  Serial.begin(115200);
  Serial.println("SimpleTx Starting");
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.openWritingPipe(slaveAddress);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - prevMillis >= txIntervalMillis)
  {
    send();
    prevMillis = millis();
  }
}

void send()
{
  bool rslt;
  rslt = radio.write( &pinStates, sizeof(pinStates) );
  if (rslt)
  {
    Serial.println("  Acknowledge received");
  }
  else
  {
    Serial.println("  Tx failed");
  }
}

Rx

// SimpleRx - the slave or the receiver

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

//not all #defines used but here for documentation
#define CE_PIN   9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

byte dataReceived[3]; // this must match the array in the TX
bool newData = false;

void setup()
{
  Serial.begin(115200);
  Serial.println("SimpleRx Starting");
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();
}

void loop()
{
  getData();
  showData();
}

void getData()
{
  if ( radio.available() )
  {
    radio.read( &dataReceived, sizeof(dataReceived) );
    newData = true;
  }
}

void showData()
{
  if (newData == true)
  {
    Serial.print("Data received ");
    for (int x = 0; x < 3; x++)
    {
      Serial.print(dataReceived[x]);
      Serial.print(" ");
    }
    Serial.println();
    newData = false;
  }
}

This pair of programs sends an array of 3 byte values from Tx to Rx. You need to add the code to read the buttons to populate the array on the Tx and read the array to set the state of the LEDs on the Rx