#include <SPI.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
#define MIC_PIN A0
#define SPEAKER_PIN 5
#define PTT_BUTTON 2
RF24 radio(CE_PIN, CSN_PIN);
const byte address1[6] = "00001"; // Address for Unit 1
const byte address2[6] = "00002"; // Address for Unit 2
bool isUnit1 = true; // Change to false for the second unit
void setup() {
pinMode(PTT_BUTTON, INPUT_PULLUP);
pinMode(SPEAKER_PIN, OUTPUT);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
if (isUnit1) {
radio.openWritingPipe(address1);
radio.openReadingPipe(1, address2);
} else {
radio.openWritingPipe(address2);
radio.openReadingPipe(1, address1);
}
radio.startListening();
}
void loop() {
if (digitalRead(PTT_BUTTON) == LOW) { // If button is pressed (TX Mode)
radio.stopListening();
int audio = analogRead(MIC_PIN);
audio = map(audio, 0, 1023, 0, 255); // Scale to 8-bit
radio.write(&audio, sizeof(audio));
}
else { // RX Mode (Listening)
radio.startListening();
if (radio.available()) {
int audio;
radio.read(&audio, sizeof(audio));
analogWrite(SPEAKER_PIN, audio);
}
}
}