Variable Passing

I have a Global Variable that I would like to pass through a function.. the code will explain below and I'm going to give a brief overview of it rather than posting the entire thing but the concept code represents exactly how the real code runs.

const char* key;

void setup(){
    Serial.begin(19200);
    Serial1.begin(19200);
}

void loop(){
// Bunch of conditions here, main one is:
 if(//some condition){
     readSerial();
}

void readSerial(){
   readS1(); //Constantly reads Serial1 and performs an XOR function
// Bunch of choices here that come from options called "Choice: X"

    if (choice == 5){
        key = keyA.c_str();  // Variable I want to pass
       // this is a String that loops through a token and gets a 256-bit key.. 
       "32d98798a7sdh2138797.. etc"
       xorOperation(inputMessage, encryptedMessage, true);
    if(choice == 6){
        key = "&" // Variable I want to pass
        xorOperation(inputMessage, encryptedMessage, true);

        }
    }
}
void xorOperation(input, output, boolean) {
  int keyLen = key.length(); //Key variable gets here fine
}

void readS1(){

    Serial.println(key); // Now I know that "key" isn't a parameter in the void function, but it isn't in xorOperation either but yet makes it there perfectly? 
    Serial.print(Serial1.readString());
   }
}

Now, while this is a very brief explanation, it should provide an overview. It's a program that has the option to do an XOR function using "&" or a 256-bit key.

The key variable makes it perfectly fine into the xorOperation void but does not make it into the readS1 function.

readS1 exists to communicate between 2 Arduinos (It's an End 2 End System). The program does exactly what I want it to do - BUT I, for the life of me; using parameters or not, cannot get the "key" variable to show "&" or the 256-bit key in the readS1 function despite the fact it works fine in xorOperation when defined in Choice == 5 or Choice == 6.

Thank you.

This doesn't look like valid C++ to me. You also have various mismatched brackets and bad indentation. I'm surprised this code compiles.

  • i don't see where the "key" variable is being passed as an argument to xorOperation().
  • 'key' is defined as a const, so it can be used as a variable
  • have no idea what the 1st 2 argument types to xorOperation() are. should 'inputMessage' be 'key'?
  • the body of xorOperation() give no hint what you're trying to do
  • int keyLen = key.length(); in xorOperation() suggests that key is a String type
  • if 'key' is a global, couldn't it just be used within xorOperation() ?

As mentioned, this code is far from accurate when it comes to my actual code. It compiles, it works. It's demonstration code, the brackets aren't meant to be accurate. It actually looks like this:

void xorOperation(const char* input, char* output, bool encrypt){
}

But as mentioned, it's a skeleton code written up to represent the application.

xorOperation() isn't the problem here.

the passable arguments for xorOperation are:

int len = serial.readBytesUntil('\n', inputMessage, sizeof(inputMessage)-1);

Then the function is

xorOperation(inputMessage, encryptedMessage, true);

// Used by:

void xorOperation(const char* input, char* output, bool encrypt)

I need key to end up in readS1().

"key" makes it perfectly fine into xorOperation() without xorOperation() ever being told about it.

Example:

Key = "&"

xorOperation() knows that key is "&"
readS1() does not know that key is "&"

Neither voids are told about key.

You'll have more luck getting help if you post your actual code. Posting half-broken non-compiling code is not efficient.

Nonsense, what you posted is junk. Instead of posting incorrect "skeleton" code (and a handwaving, word salad explanation) , post a small, complete example that actually compiles and demonstrates the problem. This is known as an MCVE. It doesn't have to be your full code, just a very small version that people can copy into the IDE, try for themselves, and observe the issue. Preferably, the problem will be demonstrated by the code printing results to the Serial Monitor.

why can't is be passed as an argument 'readS1 (key);'?

const char* key;

key is a global variable (albeit a const) so it does not need to be passed to any function

It is possible to assign to a const char*. The pointer is not const, just the pointed-to memory. That being said, using writable global variables is usually pretty questionable practice.

But readS1() never sees it

Yes, correct, what i posted is junk. It doesn't work.

Incorrect, as it's defined and pointed at nothing it can be set within a conditional statement. This works in my program. None of that is the issue.

The issue is I cannot see it in void readS1()

I'm guessing @zacoryphillips is just a troll.

It's a continous function that listens to Serial1, so if I added a pass through statement, I'd have to have it in the conditional IF statements. (Choice == 5, Choice == 6) & then it's no longer continuous because it relies on a condition to fire off.

Even if i did do

void readS1(const char* key){
}

readS1(key) never gets retrieved into the void function even if I do it though.

The reply to the poster where I say "it compiles, it works" is about the actual piece of software that runs, not the posted code.

The posted code does not work, as inidicated by both you and me

So why would post useless junk instead of the real code you're having a problem with?

See Post #7.

i don't understand.

  • what does "continuous function" mean?
  • what do you mean yb "pass through statement"?
  • what do you mean by "have to have it in the conditional IF statements"?

can you post readS1()? if it's being called with readSerial() which is repeatedly called by loop() it must be doing somehitng like

    while (Serial.available()) {
           doeSomethingWith (Serial.read());
    }

As it's a two-way system, I realised I wasn't passing a variable from Arduino A to ArduinoB when something happens.

Thanks for all the help, useful (& useless) - at least now I know next time not to rely on the wonders of the Arduino forum for help.

forum members don't understand all aspects oof what you're doing and it's only fair to ask questions when something is not understood.

How can you expect an answer to a question using terminology that's not clear?