#include <SPI.h>
//#include <nRF24L01.h>
#include <RF24.h>
#include <Button.h>
RF24 radio(7, 8); // CE, CSN pins
const byte address[6] = "00001";
const int txButtonPin = 2; // Pin connected to the button
const int txLedPin = 12;
Button txButton(txButtonPin); // Connect your button between pin 2 and GND
void setup() {
Serial.begin(9600);
pinMode(txLedPin, OUTPUT); // Initialize LED pin as output
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_HIGH);
radio.setRetries(15, 15);
txButton.begin();
}
void loop() {
toggleTxButton();
}
void toggleTxButton() {
// Toggled txButton
if (txButton.toggled()) {
if (txButton.read() == Button::PRESSED) {
Serial.println("txButton has been pressed: activate transmission");
digitalWrite(txLedPin, HIGH);
// Stop listening, send data, then start listening again
radio.stopListening(); // Prepare for transmission
sendData();
delay(500); // Debounce delay
radio.startListening(); // Switch back to receiving mode
} else {
Serial.println("txButton has been released: stop transmission");
digitalWrite(txLedPin, LOW);
// Check for incoming data
receiveData();
}
}
}
void sendData() {
const char text[] = "Hello, world!";
Serial.print("Sending: ");
Serial.println(text); // Print the data being sent
bool txStatus = radio.write(&text, sizeof(text));
if (txStatus) {
Serial.println("Data sent.");
} else {
Serial.println("Data send failed.");
}
delay(100); // Small delay to ensure stability
}
void receiveData() {
radio.startListening(); // Ensure the radio is in listening mode
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
// Print the received data
Serial.print("Received: '");
Serial.print(text);
Serial.println("'");
// Debugging: Print each character in hexadecimal
for (int i = 0; i < sizeof(text); i++) {
Serial.print(text[i], HEX);
Serial.print(" ");
}
Serial.println();
} else {
Serial.println("No data received.");
}
}