hi fellows
im trying to send values of 4 LDRs to another arduino using NRFby storing values in an array
but the code doesnt seems working.
transmitter:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/-----( Declare Constants and Pin Numbers )-----/
#define CE_PIN 9
#define CSN_PIN 10
// NOTE: the "L" at the end of the constant is "Long" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/-----( Declare objects )-----/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/-----( Declare Variables )-----/
int ldrlt = 0; //LDR top left - BOTTOM LEFT <--- BDG
int ldrrt = 1; //LDR top rigt - BOTTOM RIGHT
int ldrld = 2; //LDR down left - TOP LEFT
int ldrrd = 3; //ldr down rigt - TOP RIGHT
//---------------------------------------------------------
int sensor[4];
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
// horizontal.attach(6);
// vertical.attach(5);
// horizontal.write(180);
// vertical.write(20);
delay(1000);
}
void loop() {
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
sensor[0] = lt;
sensor[1] = rt;
sensor[2] = ld;
sensor[3] = rd;
radio.write(&sensor, sizeof(sensor));
if (!radio.write(&sensor, sizeof(sensor)))
{
Serial.println("IT didnt work");
}
else {
Serial.print("S1 = ");
Serial.print(sensor[0]);
Serial.print(" S2 = ");
Serial.print(sensor[1]);
Serial.print(" S3 = ");
Serial.print(sensor[2]);
Serial.print(" S4 = ");
Serial.print(sensor[3]);
Serial.println("");
}
delay(10);
}
Reciever:
// include the library code:
#include <Servo.h> //Servo library
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/-----( Declare Constants and Pin Numbers )-----/
#define CE_PIN 9
#define CSN_PIN 10
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/-----( Declare objects )-----/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/-----( Declare Variables )-----/
int ldrlt = 0; //LDR top left - BOTTOM LEFT <--- BDG
int ldrrt = 1; //LDR top rigt - BOTTOM RIGHT
int ldrld = 2; //LDR down left - TOP LEFT
int ldrrd = 3; //ldr down rigt - TOP RIGHT
//---------------------------------------------------------
int sensor[4];
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, pipe);
radio.startListening();
// horizontal.attach(6);
// vertical.attach(5);
// horizontal.write(160);
// vertical.write(20);
delay(3000);
}
void loop() {
if ( radio.available() )
{
int sensor[4];
// Read the data payload until we've received everything
bool done = false;
// Fetch the data payload
radio.read(&sensor,sizeof(sensor) );
Serial.print("S1 = ");
Serial.print(sensor[0]);
Serial.print(" S2 = ");
Serial.print(sensor[1]);
Serial.print(" S3 = ");
Serial.print(sensor[2]);
Serial.print(" S4 = ");
Serial.print(sensor[3]);
Serial.println("");
int lt = sensor[0];
int rt = sensor[1];
int ld = sensor[2];
int rd = sensor[3];
}
delay(100);
}