/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
rf24ping85.ino by tong67 ( https://github.com/tong67 )
This is an example of how to use the RF24 class to communicate with ATtiny85 and other node.
Write this sketch to an ATtiny85. It will act like the \'transmit\' mode of GettingStarted.ino
Write GettingStarted.ino sketch to UNO (or other board or RPi) and put the node in \'receiver\' mode.
The ATtiny85 will transmit a counting number every second starting from 1.
The ATtiny85 uses the tiny-core by CodingBadly (https://code.google.com/p/arduino-tiny/)
When direct use of 3v3 does not work (UNO boards have bad 3v3 line) use 5v with LED (1.8V ~ 2.2V drop)
For low power consumption solutions floating pins (SCK and MOSI) should be pulled high or low with eg. 10K
modified by Luis Díaz 2016
** Hardware configuration **
ATtiny24/44/84 Pin map with CE_PIN 8 and CSN_PIN 7
Schematic provided and successfully tested by Carmine Pastore (https://github.com/Carminepz)
+---\\/---+
nRF24L01 VCC, pin2 --- VCC 1|o |14 GND --- nRF24L01 GND, pin1
PB0 2|D10 D0|13 AREF --- nRF24L01 CE, pin3
PB1 3|D9 D1|12 PA1 --- nRF24L01 CSN, pin4
PB3 4|RST D2|11 PA2
PB2 5|D8 D3|10 PA3
PA7 6|D7 D4|9 PA4 --- nRF24L01 SCK, pin5
nRF24L01 MOSI, pin7 --- PA6 7|D6 D5|8 PA5 --- nRF24L01 MISO, pin6
+--------+
*/
// CE and CSN are configurable, specified values for ATtiny85 as connected above
#define CE_PIN 0
#define CSN_PIN 1
//#define CSN_PIN 3 // uncomment for ATtiny85 3 pins solution
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte addresses[][6] = {
"1Node","2Node"};
unsigned long payload = 0;
bool radioNumber=1;
int bLed=8;
int rLed=7;
void setup() {
// Setup and configure rf radio
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
// Open a writing and reading pipe on each radio, with opposite addresses
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
radio.startListening(); // Start listening
}
void loop(void){
if (radioNumber){
radio.stopListening(); // First, stop listening so we can talk.
payload++;
radio.write( &payload, sizeof(unsigned long) );
radio.startListening();
unsigned long back_payload;
unsigned long started_waiting_at = millis(); // Set up a timeout period, get the current microseconds
boolean timeout = false; // Set up a variable to indicate if a response was received or not
while ( ! radio.available() ){ // While nothing is received
if (millis() - started_waiting_at > 200 ){ // If waited longer than 200ms, indicate timeout and exit while loop
timeout = true;
digitalWrite(rLed,HIGH); //red led on if timeout occurs
delay(1000);
digitalWrite(rLed,LOW);
break;
}
}
radio.read(&back_payload, sizeof(unsigned long));
if (back_payload == payload){ //if the number I sent comes back
digitalWrite(bLed,HIGH);
delay(100);
digitalWrite(bLed,LOW);
}
delay(900);
}else{
unsigned long got_payload;
if( radio.available()){
// Variable for the received timestamp
while (radio.available()) { // While there is data ready
radio.read( &got_payload, sizeof(unsigned long) ); // Get the payload
digitalWrite(bLed,LOW);
}
radio.stopListening(); // First, stop listening so we can talk
if (got_payload%10==0) digitalWrite(bLed,HIGH); //
radio.write( &got_payload, sizeof(unsigned long) ); // Send the final one back.
radio.startListening(); // Now, resume listening so we catch the next packets.
}
}
}