Go Down

Topic: Problem connecting through transceivers (Read 41 times) previous topic - next topic

Hrenke

Hi, can you please help me with my code
I want to connect Arduino Uno to Arduino Nano trough transcievers. Arduino Uno is connected to a sound sensor that reacts to "clapping". Arduino Uno is connected to a relay module that should activate the lamp. I used this site to for pin setup:

https://howtomechatronics.com/tutorials/arduino/arduino-wireless-communication-nrf24l01-tutorial/

Arduino Uno Code:
Code: [Select]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
const byte address[6] = "00001";

int clap = 0;
int sound_sensor =4;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;

void setup() {
 pinMode(sound_sensor, INPUT);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
 int status_sensor = digitalRead(sound_sensor);
 if (status_sensor == 0)
  {
    if (clap == 0)
    {
      detection_range_start = detection_range = millis();
      clap++;
    }
    else if (clap > 0 && millis() - detection_range >= 50)
    {
      detection_range = millis();
      clap++;
    }
  }
  if (millis() - detection_range_start >= 400)
  {
    radio.write(&clap, sizeof(clap));
  }
}



Arduino Nano Code:
Code: [Select]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8);
const byte address[6] = "00001";
boolean status_lights = false;
int relay = 10;
int clap;
void setup() {
  pinMode(relay, OUTPUT);
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();

}

void loop() {
  if ( radio.available()) {
    while (radio.available()) {
      radio.read(&clap, sizeof(clap));
    }
    }
       if (clap == 2)
    {
      if (!status_lights)
      {
        status_lights = true;
       
      }
      else if (status_lights)
      {
        status_lights = false;
       
      }
    }
    clap = 0;
}

PaulS

Quote
Hi, can you please help me with my code
Certainly. Send me your Arduinos, your mysterious digital sound sensor, your radios, and your computer. I'll check for myself what your code actually does (since you didn't explain that). I'll guess what you expect the code to do (since you didn't explain that). I'll then explain why what I assume your expectations to be are wrong.

Or, you could tell us what the code actually does, which sound sensor produces a digital output, and what you expect the code to do. Then, we can explain why your expectations are wrong.
The art of getting good answers lies in asking good questions.

Robin2

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

How are you powering your nR24s? I don't think the nano's 3.3v output can provide sufficient current. Try powering the nRF24s with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up