Hi
I am trying to get two arduino units to measure the light level in two locations and then transmit the values back wirelessly to a receiver arduino which will then read the values and determine what LED lights should be lit.
I have managed to get one transmitter to work with the receiver but not sure how to get another one in there. Im struggling what to do next. I have pasted the code below and it may be incomplete in some areas. Im just wondering how you are able to have two transmitters transmitting to one receiver. Also I do not need to send data back, one way communication is fine as I have code on the receiver if no signal is being received red led lights will flash, and when there is data green led lights will be on.
Receiver code
/* YourDuinoStarter Example: nRF24L01 Receive Joystick values
- WHAT IT DOES: Receives data from another transceiver with
2 Analog values from a Joystick or 2 Potentiometers
Displays received values on Serial Monitor
- SEE the comments after "//" on each line below
- CONNECTIONS: nRF24L01 Modules See:
http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 9
4 - CSN to Arduino pin 10
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
- V1.00 11/26/13
Based on examples at http://www.bajdi.com/
Questions: terry@yourduino.com */
/*-----( Import needed libraries )-----*/
#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[2] = {0xE8E8F0F0E1LL,0xE8E8F0F0E2LL}; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
//int joystick[2]; // 2 element array holding Joystick readings
int lightLevel[2];
int lightLevel2[2];
int redLed = 2;
int greenLed = 4;
int redLed2 = 7;
int greenLed2 = 8;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
delay(1000);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe[0]);
radio.openReadingPipe(2,pipe[1]);
radio.startListening();
pinMode(redLed, OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(redLed2, OUTPUT);
pinMode(greenLed2,OUTPUT);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
//while (!done)
{
// Fetch the data payload
radio.read( lightLevel, sizeof(lightLevel) );
Serial.print("Light level ");
Serial.println(lightLevel[0]);
//Serial.print(" Y = ");
// Serial.println(joystick[1]);
if ( lightLevel[0] < 500)
{
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
delay(500);
}
if (lightLevel[0] > 501);
{
digitalWrite(greenLed, HIGH);
digitalWrite(redLed,LOW);
delay(500);
}
}
}
else
{
Serial.println("No radio available");
digitalWrite(greenLed,LOW);
digitalWrite(redLed,HIGH);
digitalWrite(greenLed2,LOW);
digitalWrite(redLed2,HIGH);
delay(500);
digitalWrite(redLed,LOW);
digitalWrite(redLed2,LOW);
delay(500);
}
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
//NONE
//*********( THE END )***********
Transmitter code for one ( the other one will be exactly the same except one will have lightValue and the other lightValue2.
/* YourDuinoStarter Example: nRF24L01 Transmit Joystick values
- WHAT IT DOES: Reads Analog values on A0, A1 and transmits
them over a nRF24L01 Radio Link to another transceiver.
- SEE the comments after "//" on each line below
- CONNECTIONS: nRF24L01 Modules See:
http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 9
4 - CSN to Arduino pin 10
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
-
Analog Joystick or two 10K potentiometers:
GND to Arduino GND
VCC to Arduino +5V
X Pot to Arduino A0
Y Pot to Arduino A1
- V1.00 11/26/13
Based on examples at http://www.bajdi.com/
Questions: terry@yourduino.com */
/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
#define LIGHTLEVEL A0
//#define JOYSTICK_Y A1
// 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 joystick[2]; // 2 element array holding Joystick readings
///int lightLevel[2];
int light1 = A0;
int light2 = A1;
int light1val = 1;
int light2val = 2;
int lightLevel[2];
//const int lightLevel = A0;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
Serial.begin(38400);
radio.begin();
radio.openWritingPipe(pipe);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
//lightLevel = analogRead(A0);
//Serial.print("Light level: ");
//Serial.println(LIGHTLEVEL,DEC);
//delay(500);
// joystick[0] = analogRead(JOYSTICK_X);
// joystick[1] = analogRead(JOYSTICK_Y);
/// lightLevel[1]=analogRead(LIGHTLEVEL);
light1val = analogRead(light1);
lightLevel[0] = 1023 - light1val;
Serial.print("Light reading 1: ");
Serial.println(lightLevel[0]);
light2val = analogRead(light2);
lightLevel[1] = light2val;
Serial.print("Light reading 2: ");
Serial.println(lightLevel[1]);
delay(500);
// radio.write( joystick, sizeof(joystick) );
radio.write(lightLevel, sizeof(lightLevel));
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
//NONE
//*********( THE END )***********
