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);
}
}
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, 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.
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.
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?
#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
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.