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:
Arduino Uno Code:
#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:
#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;
}