document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2. This program is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU General Public License
  4. version 2 as published by the Free Software Foundation.
  5.  
  6.     rf24ping85.ino by tong67 ( https://github.com/tong67 )
  7.     This is an example of how to use the RF24 class to communicate with ATtiny85 and other node.
  8.     Write this sketch to an ATtiny85. It will act like the \'transmit\' mode of GettingStarted.ino
  9.     Write GettingStarted.ino sketch to UNO (or other board or RPi) and put the node in \'receiver\' mode.
  10.     The ATtiny85 will transmit a counting number every second starting from 1.
  11.     The ATtiny85 uses the tiny-core by CodingBadly (https://code.google.com/p/arduino-tiny/)
  12.     When direct use of 3v3 does not work (UNO boards have bad 3v3 line) use 5v with LED (1.8V ~ 2.2V drop)
  13.     For low power consumption solutions floating pins (SCK and MOSI) should be pulled high or low with eg. 10K
  14.  
  15.     modified by Luis Díaz 2016
  16.  
  17.     ** Hardware configuration **
  18.       ATtiny24/44/84 Pin map with CE_PIN 8 and CSN_PIN 7
  19.     Schematic provided and successfully tested by Carmine Pastore (https://github.com/Carminepz)
  20.                                   +---\\/---+
  21.     nRF24L01  VCC, pin2 --- VCC  1|o       |14 GND --- nRF24L01  GND, pin1
  22.                             PB0  2|D10   D0|13 AREF --- nRF24L01   CE, pin3
  23.                             PB1  3|D9    D1|12 PA1 --- nRF24L01  CSN, pin4
  24.                             PB3  4|RST   D2|11 PA2
  25.                             PB2  5|D8    D3|10 PA3
  26.                             PA7  6|D7    D4|9  PA4 --- nRF24L01  SCK, pin5
  27.     nRF24L01 MOSI, pin7 --- PA6  7|D6    D5|8  PA5 --- nRF24L01 MISO, pin6
  28.                                   +--------+
  29. */
  30.  
  31. // CE and CSN are configurable, specified values for ATtiny85 as connected above
  32. #define CE_PIN 0
  33. #define CSN_PIN 1
  34. //#define CSN_PIN 3 // uncomment for ATtiny85 3 pins solution
  35.  
  36. #include "RF24.h"
  37.  
  38. RF24 radio(CE_PIN, CSN_PIN);
  39.  
  40. byte addresses[][6] = {
  41.   "1Node","2Node"};
  42. unsigned long payload = 0;
  43. bool radioNumber=1;
  44.  
  45. int bLed=8;
  46. int rLed=7;
  47.  
  48. void setup() {
  49.   // Setup and configure rf radio
  50.   radio.begin(); // Start up the radio
  51.   radio.setAutoAck(1); // Ensure autoACK is enabled
  52.   radio.setRetries(15,15); // Max delay between retries & number of retries
  53.  
  54.   // Open a writing and reading pipe on each radio, with opposite addresses
  55.   if(radioNumber){
  56.     radio.openWritingPipe(addresses[1]);
  57.     radio.openReadingPipe(1,addresses[0]);
  58.   }else{
  59.     radio.openWritingPipe(addresses[0]);
  60.     radio.openReadingPipe(1,addresses[1]);
  61.   }
  62.  
  63.   radio.startListening(); // Start listening
  64. }
  65.  
  66. void loop(void){
  67.  
  68.   if (radioNumber){
  69.     radio.stopListening(); // First, stop listening so we can talk.
  70.     payload++;
  71.     radio.write( &payload, sizeof(unsigned long) );
  72.  
  73.     radio.startListening();
  74.     unsigned long back_payload;
  75.     unsigned long started_waiting_at = millis();               // Set up a timeout period, get the current microseconds
  76.     boolean timeout = false;                                   // Set up a variable to indicate if a response was received or not
  77.    
  78.     while ( ! radio.available() ){                             // While nothing is received
  79.       if (millis() - started_waiting_at > 200 ){            // If waited longer than 200ms, indicate timeout and exit while loop
  80.           timeout = true;
  81.           digitalWrite(rLed,HIGH);                            //red led on if timeout occurs
  82.           delay(1000);
  83.           digitalWrite(rLed,LOW);
  84.           break;
  85.       }      
  86.     }
  87.     radio.read(&back_payload, sizeof(unsigned long));
  88.     if (back_payload == payload){  //if the number I sent comes back
  89.     digitalWrite(bLed,HIGH);
  90.     delay(100);
  91.     digitalWrite(bLed,LOW);
  92.      
  93.     }
  94.    
  95.     delay(900);
  96.  
  97.    
  98.   }else{
  99.     unsigned long got_payload;
  100.      
  101.       if( radio.available()){
  102.                                                                       // Variable for the received timestamp
  103.         while (radio.available()) {                                   // While there is data ready
  104.           radio.read( &got_payload, sizeof(unsigned long) );             // Get the payload
  105.           digitalWrite(bLed,LOW);
  106.         }
  107.        
  108.         radio.stopListening();                                        // First, stop listening so we can talk  
  109.         if (got_payload%10==0) digitalWrite(bLed,HIGH);               //
  110.         radio.write( &got_payload, sizeof(unsigned long) );              // Send the final one back.      
  111.         radio.startListening();                                       // Now, resume listening so we catch the next packets.    
  112.        
  113.      }
  114.   }
  115. }
');