Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- #include <SPI.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
- RF24 radio(7, 8);
- byte addresses[][6] = { "1Node", "2Node" };
- char receivedData[32];
- char data1[32];
- int j = 33;
- // -----------------------------------------------------------------------------
- // SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
- // -----------------------------------------------------------------------------
- void setup() {
- Serial.begin(9600);
- Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
- // 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
- radio.setDataRate(RF24_2MBPS);
- // 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]);
- // Random number seeding (we're going to be sending a single random number)
- randomSeed(analogRead(A0));
- }
- // -----------------------------------------------------------------------------
- // LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
- // -----------------------------------------------------------------------------
- void loop() {
- // Generate a single random character to transmit
- char data = 'a';
- // 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(&data1, sizeof(data1))) {
- // Serial.println("No acknowledgement of transmission - receiving radio device connected?");
- }
- */
- String neki = "datatosend";
- neki.toCharArray(data1, 32);
- if (!radio.write(&data1, sizeof(data1))) {
- // 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 = millis();
- // 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 (millis() - started_waiting_at > 200) {
- Serial.println("No response received - timeout!");
- return;
- }
- }
- j++;
- // Now read the data that is waiting for us in the nRF24L01's buffer
- unsigned char dataRx;
- radio.read(&receivedData, sizeof(receivedData));
- // Show user what we sent and what we got back
- Serial.print("Sent: ");
- Serial.print(j);
- Serial.print(", received: ");
- Serial.println(receivedData);
- if (j > 129) j = 33;
- // Try again 1s later
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement