nRF24L01 Morsetræner

Del 1


/*
* Transmitter part 1
*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*
* Connections
*
* UNO/NANO - MEGA - nRF24L01
* 3.3V - 3.3V - VCC
* GND - GND - GND
* 8 - 8 - CSN
* 7 - 7 - CE
* 13 - 52 - SCK
* 11 - 51 - MOSI
* 12 - 50 - MISO
*
* PIN - LED
* 2 - LED R220 to GND
*
* PIN - Button
* 5V - Button in
* 3 - Button out, R10K to GND
*
*/

#include SPI.h
#include nRF24L01.h
#include RF24.h
#define led 2 // LED pin
#define button 3 // Button pin

RF24 radio(7, 8); // CNS, CE

const byte addresses[][6] = {"00001", "00002"};
boolean buttonState = 0;

void setup() {
pinMode(2, OUTPUT);
pinMode(button, INPUT);

radio.begin();
radio.openWritingPipe(addresses[0]); // 00002
radio.openReadingPipe(1, addresses[1]); // 00001
radio.setPALevel(RF24_PA_MIN);
// radio.setPALevel(RF24_PA_LOW);
// radio.setPALevel(RF24_PA_HIGH);
// radio.setPALevel(RF24_PA_MAX);

radio.setDataRate(RF24_250KBPS); // 250kbs (Fast enough - Better range)
// radio.setDataRate(RF24_1MBPS); 1Mbps
// radio.setDataRate(RF24_2MBPS); 2Mbps

radio.setChannel(108); // 2.508 Ghz - Above most Wifi Channels (ch 0-124)

}
void loop() {
delay(5);

radio.startListening(); // Recive

while (!radio.available());

radio.read(&buttonState, sizeof(buttonState));
if (buttonState == HIGH) {
digitalWrite(led, HIGH);

delay(5);

radio.stopListening(); // Send

buttonState = digitalRead(button);
radio.write(&buttonState, sizeof(buttonState));

}
else {
digitalWrite(led, LOW);
}
}

Del 2


/*
* Transmitter part 2
*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*
* Connections
*
* UNO/NANO - MEGA - nRF24L01
* 3.3V - 3.3V - VCC
* GND - GND - GND
* 8 - 8 - CSN
* 7 - 7 - CE
* 13 - 52 - SCK
* 11 - 51 - MOSI
* 12 - 50 - MISO
*
* PIN - LED
* 2 - LED R220 to GND
*
* PIN - Button
* 5V - Button in
* 3 - Button out, R10K to GND
*
*/

#include SPI.h
#include nRF24L01.h
#include RF24.h
#define led 2 // LED pin
#define button 3 // Button pin

RF24 radio(7, 8); // CNS, CE

const byte addresses[][6] = {"00001", "00002"};
boolean buttonState = 0;

void setup() {
pinMode(2, OUTPUT);
pinMode(button, INPUT);

radio.begin();
radio.openWritingPipe(addresses[1]); // 00001
radio.openReadingPipe(1, addresses[0]); // 00002

radio.setPALevel(RF24_PA_MIN);
// radio.setPALevel(RF24_PA_LOW);
// radio.setPALevel(RF24_PA_HIGH);
// radio.setPALevel(RF24_PA_MAX);

radio.setDataRate(RF24_250KBPS); // 250kbs (Fast enough - Better range)
// radio.setDataRate(RF24_1MBPS); 1Mbps
// radio.setDataRate(RF24_2MBPS); 2Mbps

radio.setChannel(108); // 2.508 Ghz - Above most Wifi Channels (ch 0-124)

}
void loop() {
delay(5);
radio.stopListening(); // Send

buttonState = digitalRead(button);
radio.write(&buttonState, sizeof(buttonState));

delay(5);
radio.startListening(); // Recive

while (!radio.available());

radio.read(&buttonState, sizeof(buttonState));
if (buttonState == HIGH) {
digitalWrite(led, HIGH);

}
else {
digitalWrite(led, LOW);
}
}