Author Topic: using nrf24l01 for 2 way communication  (Read 85 times)

0 Members and 1 Guest are viewing this topic.

Offline R.Naresh

  • Newbie
  • Posts: 2
  • Country: in
using nrf24l01 for 2 way communication
« on: December 05, 2017, 08:05:47 PM »
currently im running a arduino pro mini 5v 16mhz + nrf24l01 + pa + antenna as transmitter
and arduino pro mini + nrf24l01 pcb antenna one as reciver also have a oled on the rx

code:
rx
Code: [Select]




/* 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 UNO
   
    GND    ->   GND
    Vcc    ->   3.3V
    CE     ->   D9
    CSN    ->   D10
    CLK    ->   D13
    MOSI   ->   D11
    MISO   ->   D12

This code should print the received values to the serial monitor
Please, like share and subscribe : [url]http://www.youtube.com/c/ELECTRONOOBS[/url]
*/
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
const uint64_t pipeIn = 0xE8E8F0F0E1LL; //Remember that this code is the same as in the transmitter

RF24 radio(9, 10);

//We could use up to 32 channels
struct MyData {
byte throttle; //We define each byte of data input, in this case just 6 channels
byte yaw;
byte pitch;
byte roll;
byte AUX1;
byte AUX2;
};

MyData data;

int packetCounts[10];
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 254
data.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 monitor
resetData();
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);

radio.openReadingPipe(1,pipeIn);
//we start the radio comunication
radio.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 values
if ( now - lastRecvTime > 1000 ) {
// Signal lost?
resetData();
}
if ( now - lastScreenUpdate < 100 )
    return;
   
  // moving average over 1 second
  packetCountTotal -= packetCounts[packetCountIndex];
  packetCounts[packetCountIndex] = 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)


TX
Code: [Select]
/*A ba
sic 4 channel transmitter using the nRF24L01 module.*/
/* Like, share and subscribe, ELECTRONOOBS */
/* http://www.youtube/c/electronoobs */


/* First we include the libraries. Download it from
   my 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 to
  wear the same unique code*/
 
const uint64_t pipeOut = 0xE8E8F0F0E1LL; //IMPORTANT: The same as in the receiver
const int ledPin = 2;

int ledState = HIGH;
int blinkcount = 0;
unsigned long previousMillis = 0;
const long interval = 100; //blink interval
const long interval2 = 1000; // pause interval

unsigned 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 channals
struct 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 LED
      previousMillis = 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 LED
      previousMillis2 = currentMillis2;
      // set the LED with the ledState of the variable:
      blinkcount = 0;
    }
 }

void setup()
{
  //Start everything up
  Serial.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);
  else
    val = 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 switches
  data.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 stuff :D


i need help to get pps readings on the reiever to test weather it is sufficient for controlling a quadcopter its intention
 and 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
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf