code enhancement

Status
Not open for further replies.

heymann03

Member
Hello, I'm using teensy 3.2 to read resistance value of one resistor using a reference resistor, and want to transmit through nRF24L01 and receive through one more nRF24L01. Any suggestions would be helpful and highly appreciated..

//The code and the sketch//

View attachment NRF24L_Transmitter_renato.ino Capture.jpg
 
However, IIRC the NRF24 sends packets of 16 bytes each and if NUM_CHANNELS is defined to be 8, you will try to write 20 bytes. I presume that this is why the comment here:
Code:
  int data[ NUM_CHANNELS ];  //2 x 6 = 12 bytes
implies that NUM_CHANNELS should be 6 so that the code will write a 16 byte packet (12 bytes of data plus 4 bytes for the "long int td").

Pete
 
If you're only going to use one channel then NUM_CHANNELS should 1 but then you must delete, or at least comment, the following statements:
Code:
/* Don't need these
pSRData->data[1]=analogRead(A1);
pSRData->data[2]=analogRead(A2);
pSRData->data[3]=analogRead(A3);
pSRData->data[4]=analogRead(A4);
pSRData->data[5]=analogRead(A5);
pSRData->data[6]=analogRead(A6);
pSRData->data[7]=analogRead(A7);
*/

But first, I'd try to send the "Hello World" string to the receiver to make sure that the Tx and Rx are working properly.
Then worry about an ADC value.

Pete
 
I got it I had overwritten the number of channels..I have changed the code. I'm not sure to what extent its correct. What i want it to do is read the values and transmit it to the receiver. And display on the serial monitor..

// The modified code (transmitter)......//


#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

const int SensorPin = A0;//Analog Input pin that senses vout
int SensorValue = 0;//sensorpn default value
float Vin = 5;//input vloltage
float Vout = 0;//Vout default value
float Rref = 18000;//reference resistors value in ohms
float R = 0;//tested resistor's default value

RF24 radio(7, 8);

const byte rxAddr[6] = "00001";

#define NUM_CHANNELS 1
typedef struct
{
//long int td;
int data [ NUM_CHANNELS ];
}
SRF24_Data;
SRF24_Data RData;
SRF424_Data*pSRData = &RData;

void setup()
{
Serial.begin(9600);
radio.begin();
radio.setRetries(15, 15);
radio.openWritingPipe(rxAddr);

radio.stopListening();
}

void loop()
{
SensorValue = analogRead(SensorPin);//read vout on analog Input pin A0(arduino can sense from 0-1023, 1023 is 5v)

Vout = (Vin* SensorValue)/1023; //convert Vout to volts
R = Rref* (1/((Vin/Vout)-1));//formula to calculate tested resistors value//
Serial.print("\nR: ");
Serial.print(R);


const char text[] = "R ";
//const char text[1] = "HEYMANN";
radio.write(&text, sizeof(text));


//long int t = millis();
radio.write(pSRData, sizeof(SRF24_Data));

delay(1000);
}

//I'm getting the following error//

[[
Arduino: 1.8.6 (Windows 10), TD: 1.43, Board: "Arduino/Genuino Uno"

sketch_sep28tr:24:1: error: 'SRF424_Data' does not name a type

SRF424_Data*pSRData = &RData;

^

C:\Users\Hemanth Kumar\Desktop\Project\examples\sketch_sep28tr\sketch_sep28tr.ino: In function 'void loop()':

sketch_sep28tr:51:15: error: 'pSRData' was not declared in this scope

radio.write(pSRData, sizeof(SRF24_Data));

^

Multiple libraries were found for "nRF24L01.h"
Used: C:\Users\Hemanth Kumar\Documents\Arduino\libraries\RF24-master
Not used: C:\Users\Hemanth Kumar\Documents\Arduino\libraries\RF24
Not used: C:\Users\Hemanth Kumar\Documents\Arduino\libraries\RF24
Not used: C:\Users\Hemanth Kumar\Documents\Arduino\libraries\RF24
Not used: C:\Users\Hemanth Kumar\Documents\Arduino\libraries\RF24
exit status 1
'SRF424_Data' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
]]
 
I would also like to emphasize this excellent advice:

But first, I'd try to send the "Hello World" string to the receiver to make sure that the Tx and Rx are working properly.
Then worry about an ADC value.

Get the communication working first with the simplest possible code.

Likewise for this ADC stuff, from a "big picture" perspective, every problem here is the result of trying to take steps too large. The path to a successful project involves starting small & simple and getting things to work first, then growing in small steps where you run each and get it working before trying to go farther.

If you write too much complex code without testing and checking simpler code first, the troubleshooting is far more work than you would have spent to take the smaller steps.
 
??????????????

I really wanted to help, but that's where I quit.

Is it because I said I'm using teensy and using genuino uno. My another set of components have to arrive. So, for now I'm using uno for transmitter and teensy 3.2 for receiver...
 
I would also like to emphasize this excellent advice:



Get the communication working first with the simplest possible code.

Likewise for this ADC stuff, from a "big picture" perspective, every problem here is the result of trying to take steps too large. The path to a successful project involves starting small & simple and getting things to work first, then growing in small steps where you run each and get it working before trying to go farther.

If you write too much complex code without testing and checking simpler code first, the troubleshooting is far more work than you would have spent to take the smaller steps.

Got the simple communication code working ("Hello World"). Now trying to read and send the resistance values!!
 
I have been trying to receive the data from master. I have used ack payload now but im still not able to receive require output.

// the receiver code..
Code:
 #include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include<printf.h>

//#define LOG_AVERAGE   0
//#define LOG_TIMESTAMP 0
//#define VCC_SUPPLY    5

//#define AVG_NUM       20

//#define R1
//#define R2
#define Rref  
#define Vout


//float avg[ AVG_NUM ];
//int   num = 0;
//int   idx = 0;
//float sum = 0;;


RF24 radio(5,6); // CE, CSN

const byte rxAddr[6] = "00001";

int remoteNodeData[2] = {1,1}; //simple integer array for remote node data, in the form(node_id, returned_count)

int dataFromMaster = 0; //integer to store count of successful transmisiions

#define NUM_CHANNELS  1

typedef struct {
  long int td;               //4 bytes
  int data[ NUM_CHANNELS ];  //2 x 6 = 12 bytes
}   SRF24_Data;

SRF24_Data  RData;
SRF24_Data* pSRData = &RData;

void setup()
{
  
  while (!Serial);
  //if (radio.available()){
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, rxAddr);
  //radio.setPALevel(RF_24_PA_MIN);
  //radio.setDataRate(RF_24_250KBPS);
  radio.setChannel(0x76);
  radio.enableAckPayload();// enable ack payload - slave replies with data using this feature
 // radio.writeAckPayload(&SRF24_Data, sizeof(SR24_Data)); // preload the payload with intial data- sent after an incoming message is read
  

 // printf_begin();
  //radio.printDetails();
  radio.startListening();

} 

//#define PRIMARY_VOLTAGE_DIVIDER_REF   47    //In KOhms


long int t;
long int lastTd;
//long int seq_number=0;

void loop()
{
 
 if (radio.available()){
  long int t;
 }
 radioCheckAndReply();
   pSRData->td = millis( );
    radio.read(pSRData, sizeof( SRF24_Data ) );
    Serial.print(pSRData->td);
    Serial.print("Mathew");
    delay(1000);
 
 }

  void radioCheckAndReply(void)
  //{
   // if (radio.available())
  {
    radio.read(&dataFromMaster, sizeof(dataFromMaster));
    Serial.println(" Received request from master - sending preloaded data.");
   // long int t;

    
    char text[32] = {};
    radio.read(&text, sizeof(text));
    Serial.println(text); 
  
    
   // pSRData->td = millis( );
    //radio.read(pSRData, sizeof( SRF24_Data ) );
    //Serial.print(pSRData->td);
    //Serial.print("Mathew");
    //delay(1000);
  }
[*** on serial monitor this is what i'm getting***]
0 received data from master - sending preloaded data

[***instead of this***]
Naidu
Hello World
R: 54270.12
4.48
Naidu
Hello World
R: 53130.41
4.49
Naidu
Hello World
R: 41455.91
4.59
Naidu
 
Code:
 if (radio.available()){
  long int t;
 }
This does nothing useful.
My guess is that the close brace should be after delay(1000) so that you only check for a reply if the radio has something available. Otherwise you will call radioCheckAndReply every time through the loop even when there's nothing available.

+edit: you shouldn't need a delay there anyway. All it would do is force the RF24 to buffer received data during the delay. That could easily cause a buffer overflow.

Pete
 
Testing for 24v and 5v supply. Just not Working!!!!!!!

I have been working on a project. where i have to measure change in resistance with the help of another reference (should this reference resistor value be small??) which is basically a voltage divider. The voltage drop(Vout) is given to inverting input of an opamp(ADA4522-1ARZ, which is renamed). The opamp is used as a buffer(voltage follower). There is another voltage divider(R1, R2). which is used to give reduced voltage to the teensy(3.2). The analog pin reads the value through the second voltage divider.(connections in detail are given in the schematic). I want to read the unknown resistance value in the serial monitor and transmit at the same time and receive with help of nRF24L01 modules. I'm not getting the desired output at transmitter end(wrong values) and also at the receiver end. The receiver end is just blank not receiving anything. Any suggestions and help would be appreciated.

//the transmitter code//
Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

float Rg = 0;
const float Vin = 24.0;
float Vout = 0.0;
const float Rref = 16000;
RF24 radio(7, 8); // CE, CSN

const byte rxAddr[6] = "00001";

#define NUM_CHANNELS  1

 typedef struct {
 long int td;               //4 bytes
 int data[ NUM_CHANNELS ];  //2 x 6 = 12 bytes
} SRF24_Data;

SRF24_Data  RData;
SRF24_Data* pSRData = &RData;

//int receiveNodeData = {1,1};

//#define Vout
//#define Rg
//#define Rref  //470Kohm
//#define Vin 

void setup()
{
  Serial.begin(9600);
  Serial.println("[*][*][*] Beginning nRF24L01 transmit program[*][*][*]");
  radio.begin();
  radio.setRetries(15, 15);
  radio.enableAckPayload();
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(rxAddr);
  radio.setChannel(0x76);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop()
{
  Serial.print("Hello World\n");
  const char text[32] = "Hello World";
  radio.write(&text, sizeof(text));
  //currentTime = millis();
  pSRData->td = millis();
  int ADCREAD = 0;
 // analogReadResolution(1);
  ADCREAD = analogRead(A0);
  //int SensorValue = analogRead(A0);
  //float Voltage = SensorValue * (5.0/1023);
  float Vout = (Vin * ADCREAD) / 1024;//(ADCREAD; //* 5)/4096;
  float Rg = (Rref * (Vin / Vout)- Rref);
  pSRData->data[0] = ADCREAD;

  //radio.write(pSRData, sizeof(SRF24_Data));



  long int t = millis();
  radio.write(pSRData, sizeof(SRF24_Data));
  Serial.print("R: ");
  Serial.println(Rg);
  Serial.println(Vout);
  Serial.println(ADCREAD);
  //Serial.println(Voltage);
  delay(100);

Serial.println("Naidu");
//  const char text[] = "This is NAIDU";
  //radio.write(&text, sizeof(text));
  //receiveNodeData();
  delay(1000);
}
//***the output-Tx***//
Hello World
R: 31627.91
1.68
344
Naidu


//****The receiver code***//
Code:
 #include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//#define Vout
//#define Rg
//#define Vin
//#define R1
//#define R2
//#define Rref

const int SensorPin = A0;
int SensorValue = 0;
float Vin = 24;
float Vout = 0;
float Rref = 16000;
float Rg = 0;

RF24 radio(7, 8);

const byte rxAddr[6] = "00001";

#define NUM_CHANNELS 1

typedef struct{
  long int td;
  int data[NUM_CHANNELS];
}
SRF24_Data;
SRF24_Data RData;
SRF24_Data* pSRData = &RData;

void setup()
{
  while (!Serial);
  Serial.begin(9600);
  
  radio.begin();
  radio.openReadingPipe(1, rxAddr);
  radio.enableAckPayload();
  radio.setChannel(0x76);
  radio.startListening();
}



long int lastTd;
void loop()
{
  if (radio.available())
  {
    long int t;

    SensorValue = analogRead(SensorPin);
   float Vout = (Vin* SensorValue)/1023;
    float Rg = (Rref * (Vin / Vout)- Rref);
    //Rg = (((Vin*Rref)/Vout)-1);
    //Rg = Rref * (1/((Vin/Vout)-1));
     
    
    radio.read(pSRData, sizeof(SRF24_Data));
    //float Vout = (pSRData->data[0]*3.27)/1023;
    Serial.print("Hello World");
    Serial.print(pSRData->td);
    Serial.print("R:");
    Serial.println(Rg);
    Serial.println(Vout);
    delay(1000);
    //Serial.print(R);
    
    //char text[] = {0};
    //radio.read(&text, sizeof(text));
    // Serial.println(text);
  }
}
 

Attachments

  • res1.zip
    11.7 KB · Views: 68
I want to transmit and receive float values. I'm creating a package in order to that.
Code:
byte package[16];
byte package2[16];
is this correct?
 
Status
Not open for further replies.
Back
Top