If-Else Statements On Individual Parts Of An Array

I am working on a project but am having some trouble with one part of it. I have 2 Nano's communicating via NRF24 modules. One module is a master with 6 switches and the other is a slave with 6 LEDs.

The code works no problem sending an array from the master to the slave and turning the LED's on and off. My problem comes in when I'm trying to use analogRead to conditionally control each of the corresponding LEDs (using a pot to simulate a current sensor). When the pot value gets above a certain level, the LED turns off.

Maybe I'm trying to do this the hard way, is there I way I can just bypass the entire array and pull the pin low?

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

RF24 radio(9,10);

const int analogInPin = A0;
const int analogInPin1 = A1;

const uint8_t button_pins[] = { 2,3,4,5,6,7 };
const uint8_t num_button_pins = sizeof(button_pins);

const uint8_t led_pins[] = { 2,3,4,5,6,7 };
const uint8_t num_led_pins = sizeof(led_pins);

const uint64_t pipe = 0xA0A0A0A0A0BL;

uint8_t button_states[num_button_pins];
uint8_t led_states[num_led_pins];

void setup(void)
{
  int i = num_led_pins;
  Serial.begin(115200);
  radio.begin();
  radio.startListening();

   
    while(i--)
    {
      pinMode(led_pins[i],OUTPUT);
      led_states[i] = LOW;
      digitalWrite(led_pins[i],led_states[i]);
    }

}

void loop(void)
{
  int buttonState;

  if (true)
  {
    if ( radio.available() )
    {
      while (radio.available())
      {
        radio.read( button_states, num_button_pins );

        int i = num_led_pins;        
       while(i--)
        {
          if(analogRead(analogInPin) > 500)
          {digitalWrite(led_pins[0], LOW);}
          else{buttonState = analogRead(button_states[1]);
          digitalWrite(led_pins[0],button_states[0]);}

          if(analogRead(analogInPin1) > 500)
          {digitalWrite(led_pins[1], LOW);}
          else
          {
       buttonState = analogRead(button_states[1]);
           digitalWrite(led_pins[1],button_states[1]);
          }
        }    
      }
    }
  }
}

This code works for the first LED, but then when I put the second analog input above 500, both turn off.

else{buttonState = analogRead(button_states[1]);

this line confuses me. IS button_states[1] holding a pin number or a reading? analogRead expects a pin number and will read from that pin. Is the other nano telling you which pin to read?

digitalWrite(led_pins[0],button_states[0]);}

Here on the very next line you are using that same array as a button state and not a pin.

So which is it?

You were right, it was very confusing. I had adapted the RF24 libraries LED Remote sketch. Here are the more slimmed down versions of the code.

MASTER:

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

RF24 radio(9,10);

const uint8_t button_pins[] = { 2,3,4,5,6,7 };
const uint8_t num_button_pins = sizeof(button_pins);
uint8_t button_states[num_button_pins];

const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void)
{
  Serial.begin(115200);
  radio.begin();
  radio.openWritingPipe(pipe);

    int i = num_button_pins;
      pinMode(button_pins[i],INPUT);
}

void loop(void)
{
    int i = num_button_pins;
    bool different = true;
    while(i--)
    {
      uint8_t state =  digitalRead(button_pins[i]);
      if ( state != button_states[i] )
      {
        different = true;
        button_states[i] = state;
      }
    }
      i = num_button_pins;
      bool ok = radio.write( button_states, num_button_pins );
  }

SLAVE:

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

RF24 radio(9,10);

const int analogInPin = A0;
const int analogInPin1 = A1;

const uint8_t button_pins[] = { 2,3,4,5,6,7 };
const uint8_t num_button_pins = sizeof(button_pins);

const uint8_t led_pins[] = { 2,3,4,5,6,7 };
const uint8_t num_led_pins = sizeof(led_pins);

const uint64_t pipe = 0xE8E8F0F0E1LL;

uint8_t led_states[num_led_pins];
uint8_t button_states[num_button_pins];

void setup(void)
{
  int i = num_led_pins;
  Serial.begin(115200);
  radio.begin();
  radio.startListening();
  radio.openReadingPipe(1, pipe);

   
    while(i--)
    {
      pinMode(led_pins[i],OUTPUT);
      led_states[i] = LOW;
      digitalWrite(led_pins[i],led_states[i]);
    }

}

void loop(void)
{
  int buttonState;

      while (radio.available())
      {
        radio.read( button_states, num_button_pins );

        int i = num_led_pins;        
       while(i--)
        {
          digitalWrite(led_pins[i],button_states[i]);
          }
        }        
  }

The goal is for a value over say, 500 on the first analog input pin to turn the first LED pin low. Is it possible to place the analog values in an array then call the array in a single if-else?

if(analog array > specified value)

  • digitalWrite(led out array low)*
    else
  • normal led function*

Sure you can access one element of an array. That’s pretty much what arrays are for. I’m not seeing the confusion.

Nabors:
if(analog array > specified value)

  • digitalWrite(led out array low)*
    else
  • normal led function*
    [/quote]
    You have to place this if loop within for loop so it will be something like this:
    ```
    *for(x=0;x<TotalLengthofArray;x++){

if(analog array [x] > specified value)
  digitalWrite(led out array low)
else
  normal led function
}*

```