using nrf24l01 for 2 way communication - Page 1
Forum section: electronics / rf-microwave

author: R.Naresh
author_rank: Contributor
timestamp: December 05, 2017, 09:05:47 am
content: currently im running a arduino pro mini 5v 16mhz + nrf24l01 + pa + antenna as transmitterand arduino pro mini + nrf24l01 pcb antenna one as reciver also have a oled on the rxcode:rxCode:/* Test code for the Radio control transmitter* Install the NRF24 library to your IDE* Upload this code to the Arduino UNO* Connect a NRF24 module to it:Module // Arduino UNOGND -> GNDVcc -> 3.3VCE -> D9CSN -> D10CLK -> D13MOSI -> D11MISO -> D12This code should print the received values to the serial monitorPlease, like share and subscribe : https://www.youtube.com/c/ELECTRONOOBS*/#include <Adafruit_SSD1306.h>#include <SPI.h>#include <nRF24L01.h>#include <RF24.h>#define OLED_RESET 4Adafruit_SSD1306 display(OLED_RESET);const uint64_t pipeIn = 0xE8E8F0F0E1LL; //Remember that this code is the same as in the transmitterRF24 radio(9, 10);//We could use up to 32 channelsstruct MyData {byte throttle; //We define each byte of data input, in this case just 6 channelsbyte yaw;byte pitch;byte roll;byte AUX1;byte AUX2;};MyData data;int packetCounts;int packetCountIndex = 0;int packetCountTotal = 0;void resetData(){//We define the inicial value of each data input//3 potenciometers will be in the middle position so 127 is the middle from 254data.throttle = 0;data.yaw = 127;data.pitch = 127;data.roll = 127;data.AUX1 = 0;data.AUX2 = 0;}/**************************************************/void setup(){Serial.begin(9600); //Set the speed to 9600 bauds if you want.//You should always have the same speed selected in the serial monitorresetData();radio.begin();radio.setAutoAck(false);radio.setDataRate(RF24_250KBPS);radio.openReadingPipe(1,pipeIn);//we start the radio comunicationradio.startListening();display.begin(SSD1306_SWITCHCAPVCC, 0x3C);memset(&data, 0, sizeof(MyData));memset( packetCounts, 0, sizeof(packetCounts) );}/**************************************************/unsigned long packetsRead = 0;unsigned long lastScreenUpdate = 0;unsigned long lastAvgUpdate = 0;unsigned long lastRecvTime = 0;unsigned long drops = 0;void recvData(){while ( radio.available() ) {radio.read(&data, sizeof(MyData));lastRecvTime = millis(); //here we receive the data}}/**************************************************/void loop(){recvData();unsigned long now = millis();//Here we check if we've lost signal, if we did we reset the valuesif ( now - lastRecvTime > 1000 ) {// Signal lost?resetData();}if ( now - lastScreenUpdate < 100 )return;// moving average over 1 secondpacketCountTotal -= packetCounts;packetCounts = packetsRead;packetCountTotal += packetsRead;packetCountIndex = (packetCountIndex + 1) % 10;packetsRead = 0;Serial.print("Throttle: "); Serial.print(data.throttle); Serial.print(" ");Serial.print("Yaw: "); Serial.print(data.yaw); Serial.print(" ");Serial.print("Pitch: "); Serial.print(data.pitch); Serial.print(" ");Serial.print("Roll: "); Serial.print(data.roll); Serial.print(" ");Serial.print("Aux1: "); Serial.print(data.AUX1); Serial.print(" ");Serial.print("Aux2: "); Serial.print(data.AUX2); Serial.print("\n");display.clearDisplay();display.setTextColor(WHITE);display.setTextSize(0.5);display.setCursor(0, 0);display.println("A0");display.setCursor(20, 0);display.println(data.throttle);display.setCursor(45, 0);display.println("A1");display.setCursor(65, 0);display.println(data.yaw);display.setCursor(0, 10);display.println("A2");display.setCursor(20, 10);display.println(data.pitch);display.setCursor(45, 10);display.println("A3");display.setCursor(60, 10);display.println(data.roll);display.setCursor(0,20);display.println("PPS");display.setCursor(30, 20);display.println(packetCountTotal);display.setCursor(40,20);display.println("AUX2");display.setCursor(70, 20);display.println(data.AUX2);display.display();}/**************************************************/note the base code is from electronoobs and the pps part which i tried is from iforce2d but it doesnt work(HELP HERE)TXCode:/*A basic 4 channel transmitter using the nRF24L01 module.*//* Like, share and subscribe, ELECTRONOOBS *//* http://www.youtube/c/electronoobs *//* First we include the libraries. Download it frommy webpage if you donw have the NRF24 library */#include <SPI.h>#include <nRF24L01.h>#include <RF24.h>/*Create a unique pipe out. The receiver has towear the same unique code*/const uint64_t pipeOut = 0xE8E8F0F0E1LL; //IMPORTANT: The same as in the receiverconst int ledPin = 2;int ledState = HIGH;int blinkcount = 0;unsigned long previousMillis = 0;const long interval = 100; //blink intervalconst long interval2 = 1000; // pause intervalunsigned long currentMillis ;unsigned long previousMillis2 = 0;RF24 radio(9, 10); // select CSN pin// The sizeof this struct should not exceed 32 bytes// This gives us up to 32 8 bits channalsstruct MyData {byte throttle;byte yaw;byte pitch;byte roll;byte AUX1;byte AUX2;};MyData data;void resetData(){//This are the start values of each channal// Throttle is 0 in order to stop the motors//127 is the middle value of the 10ADC.data.throttle = 0;data.yaw = 127;data.pitch = 127;data.roll = 127;data.AUX1 = 0;data.AUX2 = 0;}void blinkSteps0to2() {// save the last time you blinked the LEDpreviousMillis = currentMillis;// if the LED is off turn it on and vice-versa:if (ledState == HIGH) {ledState = LOW;blinkcount++;} else {ledState = HIGH;blinkcount++;}// set the LED with the ledState of the variable:digitalWrite(ledPin, ledState);}void blinkStep3(){unsigned long currentMillis2 = millis();ledState = HIGH;if (currentMillis2 - previousMillis2 >= interval2) {// save the last time you blinked the LEDpreviousMillis2 = currentMillis2;// set the LED with the ledState of the variable:blinkcount = 0;}}void setup(){//Start everything upSerial.begin(9600);radio.begin();pinMode(ledPin, OUTPUT);radio.setAutoAck(false);radio.setDataRate(RF24_250KBPS);radio.openWritingPipe(pipeOut);resetData();}/**************************************************/// Returns a corrected value for a joystick position that takes into account// the values of the outer extents and the middle of the joystick range.int mapJoystickValues(int val, int lower, int middle, int upper, bool reverse){val = constrain(val, lower, upper);if ( val < middle )val = map(val, lower, middle, 0, 128);elseval = map(val, middle, upper, 128, 255);return ( reverse ? 255 - val : val );}void loop(){// The calibration numbers used here should be measured// for your joysticks till they send the correct values.data.throttle = mapJoystickValues( analogRead(A0), 13, 524, 1015, true );data.yaw = mapJoystickValues( analogRead(A1), 1, 505, 1020, true );data.pitch = mapJoystickValues( analogRead(A2), 12, 544, 1021, true );data.roll = mapJoystickValues( analogRead(A3), 34, 522, 1020, true );data.AUX1 = digitalRead(4); //The 2 toggle switchesdata.AUX2 = digitalRead(5);Serial.print("Throttle: "); Serial.print(data.throttle); Serial.print(" ");Serial.print("Yaw: "); Serial.print(data.yaw); Serial.print(" ");Serial.print("Pitch: "); Serial.print(data.pitch); Serial.print(" ");Serial.print("Roll: "); Serial.print(data.roll); Serial.print(" ");Serial.print("Aux1: "); Serial.print(data.AUX1); Serial.print(" ");Serial.print("Aux2: "); Serial.print(data.AUX2); Serial.print("\n");radio.write(&data, sizeof(MyData));currentMillis = millis();if (currentMillis - previousMillis >= interval) {if (blinkcount <= 3) blinkSteps0to2() ;else blinkStep3() ;}}The above code is based of the code of electronoobs and the blink without delays sketch parts was included and the led blinks with out messing the transmitter stuffi need help to get pps readings on the reiever to test weather it is sufficient for controlling a quadcopter its intentionand also to send 3 - 4 voltage readings form RX > TX ( 3s - 4s lipo is planned to be used)anyone who has do this kinda stuff before please help