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.
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:
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.
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.
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.