RF24 and Floor Mat

Hello, I'm working with two arduinos, each with an RF24. One arduino has a Floor Mat attached on pin 4, and each has an LED attached on pin 7. (Sorry in advance for the lack of information in the pin numbers in the code.)

This is the input code:

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

#define CE_PIN 9
#define CSN_PIN 10

const uint64_t pipe = 0xE8E8F0F0E1LL; //Define transmit type

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

int adult[1]; //1 element array holding mat information

void setup(){
  pinMode(4, INPUT_PULLUP);
  //Mat (pin 4) is an input and is using the pull-up resistor.
  pinMode(7, OUTPUT);
  //LED (pin 13) is an output.
  radio.openWritingPipe(pipe);
}

void loop(){
  //Mat state is a variable
  int sensorVal = digitalRead(4);

  if (sensorVal == HIGH) {
    //Not pressed
    digitalWrite(7, LOW);
    
  }
  else {
    //Pressed
    digitalWrite(7, HIGH);
    adult[0] = 111;
    radio.write(adult, 1);
  }
}

And this is the output code:

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

#define CE_PIN 9
#define CSN_PIN 10

const uint64_t pipe = 0xE8E8F0F0E1LL;  //Define transmit pipe

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

int adult[1];

void setup()
{
  Serial.begin(9600);
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
  pinMode(7, OUTPUT);
}

void loop()
{
  if ( radio.available() )
  {
    bool done = false;
    while (!done)
    {
      radio.read( adult, 1 );
      if (adult[0] == 111)
      {
        digitalWrite(7, HIGH);
        Serial.println(adult[0]);
        delay(5000);
      }
      else
      {
        digitalWrite(7, LOW);
        Serial.println(adult[0]);
        delay(5000);
      }
    }
  }
  else
  {
    Serial.println("No radio available");
  }
}

The serial monitor should read "111" if the output gets data from the input, I believe. The serial monitor is completely blank. I'm not sure what to do. (I'm new to Arduino and the Forums, so I'm sorry in advance for my ineptitude with this.)

You want the serial to send the contents of array location "adult[0]". This is initialised as zero by the compiler and your program does not change it. You test for "adult[0]==111 " but this will never happen.

You also use "adult" and "adult[0]" as separate variables.

Don't use delay () as all processing stops during that time.

Weedpharma

Okay. Do you mind explaining how to fix the issues?

Why are you using an array with one element? Why are you using "adult" as a variable and array?

You need to sit down and analyse what you are trying to do.

Write down in plain language what you are trying to achieve then look at the code.

Weedpharma

This

radio.write(adult, 1);

should be

radio.write(adult, 2);

because an int comprises 2 bytes.

Likewise for reading

@weedpharma, because of the inconsistent way C/C++ identifies things it is easier to use an array[] with a single item and refer to the the array by name without parentheses in the write() and read() commands. AFAIK C/C++ treats an array name as the address of the first element whereas it treats a variable name differently - god knows why.

...R

Robin2:
@weedpharma, because of the inconsistent way C/C++ identifies things it is easier to use an array[] with a single item and refer to the the array by name without parentheses in the write() and read() commands. AFAIK C/C++ treats an array name as the address of the first element whereas it treats a variable name differently - god knows why.

...R

Thanks for the explanation. It did not look right.

Weedpharma

Thanks guys. I think my main problem here is that I'm not sure how to send pushbutton info (or, in this case, floor mat info) across the RF24s. Is there a simpler way to do it?

Just figured it out. I'll post the new code when I have it working for sure. Your advice here helped, thank you.

I did not figure it out. I was wrong.

Transmitter

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

#define CE_PIN 9
#define CSN_PIN 10

const uint64_t pipe = 0xE8E8F0F0E1LL; //Define transmit pipe

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

int adult[1]; //1 element array holding mat information

void setup(){
  pinMode(4, INPUT_PULLUP);
  //Mat (pin 4) is an input and is using the pull-up resistor.
  pinMode(7, OUTPUT);
  //LED (pin 13) is an output.
  radio.openWritingPipe(pipe);
}

void loop(){
  //Mat state is a variable
  int sensorVal = digitalRead(4);
  adult[0] = sensorVal;

    radio.write(adult, 2);
    Serial.println(adult[0]);
  
}

Reciever

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

#define CE_PIN 9
#define CSN_PIN 10

const uint64_t pipe = 0xE8E8F0F0E1LL;  //Define transmit pipe

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

int adult[1];

void setup()
{
  Serial.begin(9600);
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
  pinMode(7, OUTPUT);
}

void loop()
{
  int adultState;
  adultState = digitalRead(adult[0]);
  
  if ( radio.available() )
  {
    bool done = false;
    while (!done)
    {
      radio.read( adult, 2 );
     switch (adultState)
     {
      case 0:
        Serial.println("Pressure");
        break;
      case 1:
        Serial.println("No Pressure");
        break;
     }

The Serial monitor still shows nothing. I'm not sure what to do.

weedpharma:
This is initialised as zero by the compiler and your program does not change it.

I have a feeling this has something to do with it but I'm not sure what, or how to fix it.

There is something really crazy (thoughtless ?) in this (from the Rx code)

void loop()
{
  int adultState;
  adultState = digitalRead(adult[0]);
 
  if ( radio.available() )
  {
    bool done = false;
    while (!done)
    {
      radio.read( adult, 2 );
     switch (adultState)
     {
      case 0:
        Serial.println("Pressure");
        break;
      case 1:
        Serial.println("No Pressure");
        break;
     }
    }
   }
 }

You have an array adult[] which is intended to receive the value from the Tx
The Tx can send either 0 or 1 because it is sending the result of a digitalRead()

Then (for some inexplicable reason) you read the value of the pin indexed by the value in adult[0] - ie reading the value of pin 0 or pin 1

Following that you receive the value from the Tx (which goes into adult[0]

And then you completely ignore the received value and base your SWITCH on the value returned from reading either pin 0 or pin 1

...R

Oh, oops! I was trying to read the value of adult[0]. How do I do that in a switch statement?

beetleLaunch:
Oh, oops! I was trying to read the value of adult[0]. How do I do that in a switch statement?

How do you think?

switch(adult[0])

...R

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

#define CE_PIN 9
#define CSN_PIN 10

const uint64_t pipe = 0xE8E8F0F0E1LL;  //Define transmit pipe

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

int adult[1];

void setup()
{
  Serial.begin(9600);
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
  pinMode(7, OUTPUT);
}

void loop()
{
  int adultState;
  adultState = digitalRead(adult[0]);
  
  if ( radio.available() )
  {
    bool done = false;
    while (!done)
    {
      radio.read( adult, 2 );
     switch (adult[0])
     {
      case 0:
        Serial.println("Pressure");
        digitalWrite(7, HIGH);
        break;
      case 1:
        Serial.println("No Pressure");
        digitalWrite(7, LOW);
        break;
     }
    }
  }
  else
  {
    Serial.println("No radio availible");
  }
}

The LED is not lighting and there's no information on the serial monitor still.

This line

radio.read( adult, 2 );

should be

done = radio.read( adult, 2 );

The way your code is written it gets into an infinite loop because there is nothing to change the variable done

You need to be more careful copying from the examples. :slight_smile:

...R

The example had that, I just took that part out because the examples suggested that it be taken out. When I had it in, I had an error message:

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

output_v2.ino: In function 'void loop()':
output_v2:32: error: void value not ignored as it ought to be
void value not ignored as it ought to be

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

I can add what you suggested, but how do I prevent the error message?

beetleLaunch:
I can add what you suggested, but how do I prevent the error message?

if you get the error message post the program and the error message.

...R

Rx

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

#define CE_PIN 9
#define CSN_PIN 10

const uint64_t pipe = 0xE8E8F0F0E1LL;  //Define transmit pipe

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

int adult[1];

void setup()
{
  Serial.begin(9600);
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
  pinMode(7, OUTPUT);
}

void loop()
{
  int adultState;
  adultState = digitalRead(adult[0]);
  
  if ( radio.available() )
  {
    bool done = false;
    while (!done)
    {
      done = radio.read( adult, 2 );    //<----------------------------- This is the line of error.
     switch (adult[0])
     {
      case 0:
        Serial.println("Pressure");
        digitalWrite(7, HIGH);
        break;
      case 1:
        Serial.println("No Pressure");
        digitalWrite(7, LOW);
        break;
     }
    }
  }
  else
  {
    Serial.println("No radio availible");
  }
}

And the error message:

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

output_v2.ino: In function 'void loop()':
output_v2:32: error: void value not ignored as it ought to be
void value not ignored as it ought to be

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

Here is the Tx code now:

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

#define CE_PIN 9
#define CSN_PIN 10

const uint64_t pipe = 0xE8E8F0F0E1LL;  //Define transmit pipe

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

int adult[1];

void setup()
{
  Serial.begin(9600);
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
  pinMode(7, OUTPUT);
}

void loop()
{
  int adultState;
  adultState = digitalRead(adult[0]);
  
  if ( radio.available() )
  {
    bool done = false;
    while (!done)
    {
      done = radio.read( adult, 2 );
     switch (adult[0])
     {
      case 0:
        Serial.println("Pressure");
        digitalWrite(7, HIGH);
        break;
      case 1:
        Serial.println("No Pressure");
        digitalWrite(7, LOW);
        break;
     }
    }
  }
  else
  {
    Serial.println("No radio availible");
  }
}

Still nothing happening. I don't get the error message though. I just had the wrong library.

beetleLaunch:
Still nothing happening. I don't get the error message though. I just had the wrong library.

There is a lot of unstated information in that line.

Do you mean that the error message for your RX code has gone away?
What change to the library have you made?

You have to tell us these things explicitly - I cannot guess.

I am working on an RF24 project at the moment and my code compiles with done = radio.read(. I am using IDE 1.5.6 - I don't know if that matters.

AND ... it is quite impossible to use the line while (!done) if there is no means of changing the value of done

...R

I did not change the library, the one I had was from a somewhat questionable source. I found another version of it, which I replaced the one I had with. The code is working fine now. I can post the finished code if you want. Thank you.