Hi everyone !I need some help with my programming, 3 days ago I started to play with arduino and C language for a school project but now I am stuck.
I am trying to command an Neopixel ring (16 LED's) with an NRF24L01 when a PIR sensor is activated but this will only work if an contact is closed (garage door open). I made it working individually (1 transmitter, 1 receiver) but when I try to use 2 transmitter I cannot make the pipe (communication correctly) and I also have to make an condition that if TRUE (garage door open) it will enter and light up my neopixel, if not the led remains off.
If you have any tips,help I am here, thank you.
This is what I have :
Sensor Transmitter [NANO 1]
//Include needed Libraries at beginning
#include "nRF24L01.h" //NRF24L01 library
#include "RF24.h"
#include "SPI.h"
#define sensorPin 2 // Sensor switch is connected to Pin 8 on NANO
int etatSensor[1] = {000}; // Used to store value before being sent through the NRF24L01
RF24 radio(9,10); // NRF24L01 used SPI pins + Pin 9 and 10 on the NANO
const uint64_t pipes[3] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0E2LL, 0xF0F0F0F0E3LL }; // Needs to be the same for communicating between 2 NRF24L01
int buttonState = 0; // etat de la sortie du capteur
const int ledPin = 4; // la LED du Arduino
void setup(void){
{
pinMode(ledPin, OUTPUT); //la broche de la LED est mise en sortie
pinMode(sensorPin, INPUT); //la broche du capteur est mise en entree
}
pinMode(sensorPin, INPUT_PULLUP); // Define the arcade switch NANO pin as an Input using Internal Pullups
digitalWrite(sensorPin,HIGH); // Set Pin to HIGH at beginning
radio.begin(); // Start the NRF24L01
radio.openWritingPipe(pipes[2]); // Get NRF24L01 ready to transmit
radio.startListening()
}
void loop(void)
{
buttonState = digitalRead(sensorPin);//lecture du capteur
if (buttonState == HIGH) //si quelquechose est detecte
{
digitalWrite(ledPin, HIGH); //on allume la LED
}
else //sinon
{
digitalWrite(ledPin, LOW); //on eteint la LED
}
if (digitalRead(ledPin) == LOW){ // If Switch is Activated
etatSensor[0] = 111;
radio.write(etatSensor, 1); // Send value through NRF24L01
}
else {
etatSensor[0] = 000;
radio.write(etatSensor, 1);
}
}
Contact transmitter [NANO 2]
// NRF24L01 Module Tutorial - Code for Transmitter using Arduino NANO
//Include needed Libraries at beginning
#include "nRF24L01.h" //NRF24L01 library
#include "RF24.h"
#include "SPI.h"
#define SwitchPin 8 // Arcade switch is connected to Pin 8 on NANO
bool etatBtn[1] = {000}; // Used to store value before being sent through the NRF24L01
RF24 radio(9, 10); // NRF24L01 used SPI pins + Pin 9 and 10 on the NANO
const uint64_t pipes[3] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0E2LL, 0xF0F0F0F0E3LL };// Needs to be the same for communicating between 2 NRF24L01
void setup(void) {
pinMode(SwitchPin, INPUT_PULLUP); // Define the arcade switch NANO pin as an Input using Internal Pullups
digitalWrite(SwitchPin, HIGH); // Set Pin to HIGH at beginning
radio.begin(); // Start the NRF24L01
radio.openWritingPipe(pipes[1]); // Get NRF24L01 ready to transmit
radio.startListening()
}
void loop(void) {
if (digitalRead(SwitchPin) == LOW) { // If Switch is Activated
etatBtn[0] = 111;
radio.write(etatBtn, 1); // Send value through NRF24L01
}
else {
SentMessage[0] = 000;
radio.write(&etatBtn, 1);
}
}
Receiver [UNO]
//Include needed Libraries at beginning
#include "nRF24L01.h" // NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"
#include "FastLED.h" // FastLED library for WS2812 RGB Stick http://fastled.io/
#define NUM_LEDS 16 // Number of leds on stick
#define LED_PIN 8 // Digital In (DI) of RGB Stick connected to pin 8 of the UNO
CRGB leds[NUM_LEDS]; // FastLED Library Init
bool etatBtn, etatSensor ;
RF24 radio(9, 10); // NRF24L01 used SPI pins + Pin 9 and 10 on the UNO
const uint64_t pipes[3] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0E2LL, 0xF0F0F0F0E3LL }; // Needs to be the same for communicating between 2 NRF24L01
void setup(void) {
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS); // Setup FastLED Library
FastLED.clear(); // Clear the RGB Stick LEDs
// Light up starting LED's
for (int x = 0; x != NUM_LEDS; x++) {
leds[x] = CRGB::Red;
}
FastLED.setBrightness(50);
FastLED.show();
radio.begin(); // Start the NRF24L01
radio.openReadingPipe(1, pipes[1]);// Get NRF24L01 ready to receive
radio.openReadingPipe(2, pipes[2]);
radio.startListening(); // Listen to see if information received
pinMode(LED_PIN, OUTPUT); // Set RGB Stick UNO pin to an OUTPUT
}
void loop(void) {
while (radio.available())
{
// radio.read(ReceivedMessage, 1); Read information from the NRF24L01
radio.read(&etatBtn, 1);
radio.read(&etatSensor, 1);
if (etatBtn[0] == 111) // Indicates switch is pressed
{
if (etatSensor[0] == 111) // Indicates sensor detect
{
for (int x = 0; x != NUM_LEDS; x++)
{
leds[x] = CRGB::Green;
FastLED.show();
}
}
else
{
for (int x = 0; x != NUM_LEDS; x++)
{
leds[x] = CRGB::Red;
FastLED.show();
}
}
delay(10);
}