Reading Stored Variables

Hi everyone,

I'm sending myself two pieces of information using a NRF transciever at different times and at different rates. I'm trying to read the incoming information and split the two pieces of information up so they can be stored as seperate variables. They don't always arrive together and I need a way to recognise which is which.

My thinking is to use some sort of identifier on the end of the more frequent variable so if the arduino sees this identifier it knows which it is and to store it as a specific variable. My problem is once I read the radio transmission and store it as a variable I don't know a function that can re-read the received message and look for the identifier.

Is this possible or should I start looking at other methods? Apologies for not posting the code but the problem is I don't have any for this specifically, the radio.read function looks as follows so that I have what is read and its size:

if (radio.available()) { //Checks if something is being sent
    radio.read(&val, sizeof(val));

Hopefully this is a fairly rudimentary questions and one of you wizards can help me out or suggest a better approach.

Cheers in advance :smiley:

My problem is once I read the radio transmission and store it as a variable I don't know a function that can re-read the received message and look for the identifier.

You do not need to re-read the received message. You receive the message ONCE, and store it in a global or static, NOT local, variable.

Then, you can ACCESS that variable as often as you need to.

Post ALL of your code, for both ends.

Yes, sorry that wasn't clear, I have stored what was received as a global variable and can access that variable to call it again no problem however I want to split it up into two variables using some function so I can receive two pieces of information from the transmitter.

There's a whole lot of code built up already which I'm not really inclined to put here and sorting through for the relevant parts would be quite the task. Are there any solutions you can think of that I might be able to try more as a thought experiment?

Sorry and thank you.

Yes, sorry that wasn't clear, I have stored what was received as a global variable and can access that variable to call it again no problem however I want to split it up into two variables using some function so I can receive two pieces of information from the transmitter.

Whoa, dude, breathe. You'll pass out at this rate.

Use some punctuation and capital letters so you don't look like you are just babbling.

You do NOT call variables. You call functions.

You STILL need to post your code for both ends. You can't parse data without SOME idea what the data looks like, and how it is stored.

Just_Rhys:
There's a whole lot of code built up already which I'm not really inclined to put here...

Would you be inclined to let us at least have a peek at what the data looks like? As an experiment?
Just a thought.

hahaha, Alright, well, thank you, for, the, reminder, breathing, well, now. I’ve had a pick through and I think this is all the stuff that’s relevant, hopefully it’ll be of some use.

Transmitter:

//Libraries:

#include <printf.h>
#include <RF24_config.h>
#include <SPI.h> //This is includ ed so we can command non-computer connected boards
#include <nRF24L01.h> //To bring in the inbuilt device software
#include <RF24.h> //to include the inbuilt communication software

//Using Trigometic Functions, https://gist.github.com/vertrigo/4033545
#include <math.h>

//Constants
const byte addresses[][6] = {"00001", "00002"};
const byte charsInBuffer = 9;

const float pi = 3.1415926535;

const int baudRate = 9600;

char input [charsInBuffer]; //Store the user input prior to showing on screen

//Variable Integers
boolean dataIsReady = false;
int val;        //blade length user input
int mph;        //Wind speed
int CE = 7;     //Transciever Pin Declaration
int CSN = 8;    //Transciever Pin Declaration


//Initialisations
RF24 radio(CE, CSN); //Transciever Pins


void setup() {
  Serial.begin(baudRate);
  Serial.println("Enter a Number: "); //Write instructions

  radio.begin(); //Start Communications
  //Allowing bi-directional communication
  radio.openWritingPipe(addresses[1]); // 00001
  radio.openReadingPipe(1, addresses[0]); // 00002
  radio.setPALevel(RF24_PA_MIN);

}

void loop() {

  delay(5); //Small delay so the transciever and the serial monitor doesn't get overwhelmed by


  char incomingByte; //Store for stream of data coming from serial monitor (keyboard input)
  static byte index = 0;

  radio.stopListening();

  if (Serial.available()) { //If the input isn't available then don't check
    incomingByte = Serial.read(); //Store the incoming data stream

    if (incomingByte == '\n') { //Check to see if the user has hit enter - enter sends the current input
      input[index] = '\0';
      index = 0;
      dataIsReady = true;
    }
    else { //Capture user input until the user is ready to start
      input[index] = incomingByte; //Store latest character
      index++;
      if (index >= charsInBuffer) index = charsInBuffer - 1; //Test???
    }
  }
  //Transmitting Instructions
  if (dataIsReady) {
    val = atoi(input);
    Serial.print("New Length: ");
    Serial.println((val));
    radio.write(&val, sizeof(val));
    dataIsReady = false;
  }

  Serial.println (val); //Check we're still calling the right number outside of it's if loop

**A bunch of maths to work some stuff out** 

  radio.write(&mph, sizeof(mph));
//We need a way of changing val or mph so it is identifiable at the reciever

  //Receiving Instructions
  radio.startListening();//Becomes a Reciever
  if (radio.available()) { //We need to check if there's anything in the pipe to do anything
    char text[72] = ""; //Preparing for message
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

Receiver:

//Libraries:
#include <Servo.h>

#include <printf.h>
#include <RF24_config.h>
#include <SPI.h> //This is included so we can command non-computer connected boards
#include <nRF24L01.h> //To bring in the inbuilt device software
#include <RF24.h> //to include the inbuilt communication software

//Using Trigometic Functions, https://gist.github.com/vertrigo/4033545
#include <math.h>

//Constants

const float conv = 0.447; //Conversion constant from RPM or mph
const byte addresses[][6] = {"00001", "00002"}; //Oppening communication pipelines
const float pi = 3.1415926535;
const int baudRate = 9600;

//Variable Integers
char terminator = "£"; //Reading values from ground station indicator, part of the idea I had not essential
int CE = 7; //Transciever Pin Declaration
int CSN = 8; //Transciever Pin Declaration


int mph; //Wind speed
int val;
double thetapan;

//Initialises:
RF24 radio(CE, CSN); //Transciever Pins
Servo myServo;

void setup() {
  Serial.begin(baudRate);

  myServo.attach(servoLocation); //Including servo hardware
  // myServo.write(0);  // Set servo to mid-point


  radio.begin(); //Starting communications

  //Allowing bi-directional communication
  radio.openWritingPipe(addresses[0]); // 00002
  radio.openReadingPipe(1, addresses[1]); // 00001
  // radio.openReadingPipe(2, addresses[2]); //00003

  radio.setPALevel(RF24_PA_MIN);
}

void loop() {

  //Recieve Instructions
  delay(5);
  radio.startListening(); //Becomes a Reciever

  if (radio.available()) { //Checks if something is being sent
    radio.read(&val, sizeof(val));

mph = val.readStringUntil(terminator); //The start of trying to implement what I was thinking, non-essential

    //We need to receive two different variables. radio.read again will manage it but I
    //need to know how to distinguish which is which. mph will be calculated 
    //and updated continuously so will need to be read over and over but val
    //only on the occasion when it is entered

    //Trans Instructions
    radio.stopListening();
    const char text[] = "Values Changed";
    radio.write(&text, sizeof(text));
    delay (500);
  }

  //Motor Motion Maths

  tanval = ((2 * val * tan((60 * pi) / 360)) / Ls); //(rad)
  steptime = (((val / 3) * pi * pi * stepsize) / ((conv) * mph * 180)) * (1000 / atan(tanval) ); //(s)
  thetapan = (atan(tanval)) * (180 / pi); //(deg)


  for (pos = 0; pos <= thetapan; pos += stepsize) {
    myServo.write(pos);
    delay((steptime, DEC));
    //steptime, DEC
  }
  for (pos = thetapan; pos >= 0; pos -= thetapan) {
    myServo.write(0);
    delay ((steptime, DEC));
  }
}

Sorry for the delay in all of this, lunch was calling.

    val = atoi(input);
    Serial.print("New Length: ");
    Serial.println((val));
    radio.write(&val, sizeof(val));

That does NOT look to me like you are writing any kind of identifier with the value, so there will be NO way, on the receiving end, to determine if the data represents this val(ue) or mph.

You COULD use Robin2’s Serial Input Basics techniques, and send “<L:val>” or “<S:mph”’, and easily determine if the value represented as a string is a length or a speed.

Aye yea I hadn't put the identifier in yet, apologies. Hmm, thanks I'll look into Robin2's stuff now cheers.