Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- #include "RF24.h"
- // This is just the way the RF24 library works:
- // Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
- #define CE_PIN 3
- #define CSN_PIN 2
- RF24 radio(CE_PIN, CSN_PIN);
- byte addresses[][4] = {"Empf", "Send"};
- // Schalter einrichten
- #define SCHALTER_PIN 10
- // -----------------------------------------------------------------------------
- // SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
- // -----------------------------------------------------------------------------
- void setup() {
- //Debugging einrichten
- {
- Serial.begin(9600);
- Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
- }
- //Radio einrichten
- {
- // Initiate the radio object
- radio.begin();
- // Set the transmit power to lowest available to prevent power supply related issues
- radio.setPALevel(RF24_PA_MIN);
- // Set the speed of the transmission to the quickest available
- // RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
- radio.setDataRate(RF24_250KBPS);
- // Use a channel unlikely to be used by Wifi, Microwave ovens etc
- radio.setChannel(124);
- // Open a writing and reading pipe on each radio, with opposite addresses
- radio.openWritingPipe(addresses[1]);
- radio.openReadingPipe(1, addresses[0]);
- }
- // Schalter einrichten
- pinMode(SCHALTER_PIN, INPUT);
- }
- // -----------------------------------------------------------------------------
- // LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
- // -----------------------------------------------------------------------------
- void loop()
- {
- if (digitalRead(SCHALTER_PIN) == HIGH)
- {
- senden();
- }
- }
- // -----------------------------------------------------------------------------
- // SENDEN SENDEN SENDEN SENDEN SENDEN SENDEN SENDEN
- // -----------------------------------------------------------------------------
- void senden ()
- {
- unsigned long start = micros();
- // unsigned data to submitt
- unsigned char data = 1;
- // Ensure we have stopped listening (even if we're not) or we won't be able to transmit
- radio.stopListening();
- // Did we manage to SUCCESSFULLY transmit that (by getting an acknowledgement back from the other Arduino)?
- // Even we didn't we'll continue with the sketch, you never know, the radio fairies may help us
- if (!radio.write( &data, sizeof(unsigned char) )) {
- Serial.println("No acknowledgement of transmission - receiving radio device connected?");
- }
- // Now listen for a response
- radio.startListening();
- // But we won't listen for long, 200 milliseconds is enough
- unsigned long started_waiting_at = micros();
- // Loop here until we get indication that some data is ready for us to read (or we time out)
- while ( ! radio.available() ) {
- // Oh dear, no response received within our timescale
- if (micros() - started_waiting_at > 100000 ) {
- Serial.println("No response received - timeout!");
- return;
- }
- }
- // Now read the data that is waiting for us in the nRF24L01's buffer
- unsigned char dataRx;
- radio.read( &dataRx, sizeof(unsigned char) );
- unsigned long time_recieved = micros();
- // Show user what we sent and what we got back
- Serial.print("Sent: ");
- Serial.print(data);
- Serial.print(", received: ");
- Serial.print(dataRx);
- Serial.print(", Send-Time: ");
- Serial.print(time_recieved - started_waiting_at);
- Serial.print(" mys, Total-Time: ");
- Serial.print(time_recieved - start);
- Serial.println(" mys.");
- }
Advertisement
Add Comment
Please, Sign In to add comment