/*-----( Declare Constants and Pin Numbers )-----*/ #include #include "nRF24L01.h" #include "RF24.h" #include "printf.h" #define CE_PIN 12 #define CSN_PIN 13 #define DEBUG 1 #define TEST 0 #define power 9 int idpin[4] = { A0, A1, A2, A3}; int chpin[4] = { A7, A4, A6, A5 }; int i = 0; /*-----( Declare objects )-----*/ RF24 radio(CE_PIN, CSN_PIN); // Create a Radio /*-----( Declare Variables )-----*/ //uint8_t cmd[ 16 ]; // NOTE: the "LL" at the end of the constant is "LongLong" type const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe uint8_t CMD[ 20 ]; void setup() { pinMode(power, OUTPUT); digitalWrite(power, HIGH); for (int i = 0; i < 4; i++) { pinMode(idpin[i], INPUT); pinMode(chpin[i], INPUT); } if(DEBUG) Serial.begin(9600); // while (!Serial) { // ; // wait for serial port to connect. Needed for Leonardo only // } // delay(4000); radio.begin(); printf_begin(); radio.setChannel(channel()); radio.setDataRate( RF24_250KBPS ); radio.setPALevel( RF24_PA_MAX ); radio.setAutoAck(0); radio.setPayloadSize(16); radio.openReadingPipe(1, pipe); radio.startListening(); radio.printDetails(); if (DEBUG) { Serial.println(); Serial.println("Nrf24L01 Receiver Starting"); Serial.print("Address: "); Serial.print(id()); Serial.print("Address: "); Serial.print(id()); Serial.print(" Channel code: "); Serial.print(code()); Serial.print(" Channel: "); Serial.println(channel(), HEX); } //delay(100); } char tagString[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\0'}; char tagString_previous[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\0'}; int count = 0; void loop() { if (TEST) { // // powerUSB(LOW); // delay(20); // powerUSB(HIGH); // delay(20); for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { // sets the value (range from 0 to 255): analogWrite(power, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } // fade out from max to min in increments of 5 points: for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { // sets the value (range from 0 to 255): analogWrite(power, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } } else { if ( radio.available() ) { // Dump the payloads until we've gotten everything // bool done = false; // while (!done) while (radio.available()) { // Fetch the payload, and see if this was the last one. //done = radio.read( CMD, sizeof(CMD) ); radio.read( CMD, 32 ); // Serial.println("Got command"); // // for(i=0 ; i<8 ; i++) // { // Serial.print(CMD[i]); // Serial.print(","); // } // Serial.println(); // } if (CMD[0]=='D') { uint16_t temp = CMD[2]<<8; temp = temp + CMD[1]; Serial.print(temp); Serial.print(' '); Serial.println(CMD[3]); } else if (CMD[0]=='R') { for(int i=1; i<13; i++) tagString[i-1] = CMD[i]; if (!compareTag(tagString, tagString_previous)) { // Serial.print("const char tx_"); // Serial.print(count); // Serial.print("[] PROGMEM = "); // Serial.print('"'); Serial.print(tagString); // Serial.print('"'); Serial.println(';'); copyTag(tagString); clearTag(tagString); count++; if (count == 14) count=0; } } } } } } byte id() { int id = 0; for (byte i = 0; i < 4; i++) bitWrite(id, i, digitalRead(idpin[i])); return id; } int channel() { int value = code(); switch (value) { case 0: value= 0x4C; break; case 1: value= 0x64; break; default: value= 0x64; break; } return value; } int code() { int value = 0; for (byte i = 0; i < 4; i++) bitWrite(value, i, digitalRead(chpin[i])); return value; } void powerUSB(boolean state) { digitalWrite(power, state); } boolean compareTag(char one[], char two[]){ /////////////////////////////////// //compare two value to see if same, //strcmp not working 100% so we do this /////////////////////////////////// if(strlen(one) == 0) return false; //empty for(int i = 0; i < 11; i++){ if(one[i] != two[i]) return false; } return true; //no mismatches } void copyTag(char one[]){ if(strlen(one) == 0) return; //empty for(int i = 0; i < 13; i++){ tagString_previous[i] = one[i]; } } void clearTag(char one[]){ /////////////////////////////////// //clear the char array by filling with null - ASCII 0 //Will think same tag has been read otherwise /////////////////////////////////// for(unsigned int i = 0; i < strlen(one); i++){ one[i] = 0; } }