void send_binary_string(int A2, char* A5)
{
int a = 0; while( a < A2 ) { a++;
Serial.println(" ");Serial.print(", ");Serial.print(a);Serial.print(", ");Serial.print(A5);Serial.print(", ");
digitalWrite(0,HIGH); delayMicroseconds(A3);digitalWrite(0, LOW); delayMicroseconds(A4);
//unsigned char *word = A55;
//strcpy("A55", "A5");
char *word = "A5";
for(int i = 0; word[i]; i++) {
if (word[i] == '1') {
// "1" long high + short low
digitalWrite(0,HIGH); delayMicroseconds(A6); digitalWrite(0, LOW); delayMicroseconds(A7); Serial.print("1");
} else {
// "0" short high + long low
digitalWrite(0,HIGH); delayMicroseconds(A7); digitalWrite(0, LOW); delayMicroseconds(A6); Serial.print("0");
}
}
Serial.println(" "); }
}
I can't get the semantics for the string right. I know it's going in because Serial.print(A5) prints it to the serial monitor. I have tried changing the function definition for the string to just about everything I can think of, eg
char* A5
String A5
char *word A5
std:string A5
etc etc
I have tried strcpy as shown too.
I know it's not being pulled into char *word = "A5"; because
Serial.print("1"); and
Serial.print("0");
are outputting nothing on the serial monitor.
But if I remove the variable A5 from the function and the call and replace A5 with
"0111000000111111000011000000"
it works.
The name of the variable, A5, is NOT in any way, shape, or form, related, at run time, to the string "A5". It really isn't clear what you are trying to do with the string you passed in.
You are doing digitalWrite() to pin 0 which is one of the two Serial pins on an Arduino UNO. You should not do that when you are trying to use Serial.
Using the names of various analog input pins (A0, A1, A2, A3, A4, A5) as variable names is confusing. Especially when you leave out the part of the sketch that defines those names.
Power_Broker:
As to answer your question, why not just create the string outside of the function call and then pass the pointer?
If you define the variable outside the function, you don't need to pass anything. However, making data global simply because you don't know how to pass a pointer is not the right way to solve the problem. The better way is to learn how to use pointers properly. Also, instead of using global data, a better way is to define the string inside the function, minimizing outside contamination, and then simply pass the array to the function.
econjack:
If you define the variable outside the function, you don't need to pass anything. However, making data global simply because you don't know how to pass a pointer is not the right way to solve the problem. The better way is to learn how to use pointers properly. Also, instead of using global data, a better way is to define the string inside the function, minimizing outside contamination, and then simply pass the array to the function.
No, no, no.
I'm not saying to use a global variable, I'm just saying that you can initialize the array outside the scope of the function in question (but still not global - i.e. initialize the string in loop() or another user defined function instead of outside all functions) and then pass the pointer (which would still be required since it isn't a global variable).