That, however, is unlikely to send your ints in a way you expect. It is "casting" them to a different data type.
If you want to send those ints (16-bit signed numbers) as such, it would be better in your case to set up a loop, and in that loop do a Serial.print of each element, with something in-between (like a space, comma, newline, etc.).
but does someone know how you can read it from the serial port so it will be placed in an array again.
I use xbees for the communication and I'm looking for a way to send multiple parameters in 1 massage.
So I was thinking an array would be ideal to store each parameter.
After this post I know am able to send an array over serial connection, but I still do not have a way to read it in my second arduino ( as an array) so I can use those parameters that were placed in the array at the transmitter side
but I still do not have a way to read it in my second arduino ( as an array) so I can use those parameters that were placed in the array at the transmitter side
We still don't know how you are sending the data. As ASCII strings or as binary values.
How to read, and use, the data depends on how you send it.
Serial Input Basics - updated has a lot of information on sending and receiving serial data. If that doesn't answer all of your questions, post the sending code and the receiving code, and we can help you further.
I have switched my method of sending. I know Serial.write (which means it is an 8-bit binary value if i'm not mistaken) all the parameters behind each other.
If i am right it gets send to the serial buffer where it is saved then.
So I read the serial port and place it in an array, after that i read the port again and place it in the second spot of that array. I haven't been able to fully test the code so I'm not sure if it is functional.
I am also not a 100% sure if I correctly placed the value in the array. It would be really helpfull if you could take a quick look.
Sender:
int V1 = 1;
int V2 = 55;
int V3 = 158;
void setup() {
Serial.begin(9600);
}
void loop() {
//Serial.write("A");
Serial.write(V1);
Serial.write(V2);
Serial.write(V3);
delay(50);
}
Receiver:
char myArray[3] = {0, 0, 0,};
int sentDat1 = 0;
int sentDat2 = 0;
int sentDat3 = 0;
void setup() {
pinMode(2, OUTPUT);
pinMode(6, OUTPUT);
pinMode(6, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
/* first byte */
sentDat1 = Serial.read(); //gets one byte from serial buffer
myArray[0] = sentDat1;
/* second byte */
sentDat2 = Serial.read(); //gets one byte from serial buffer
myArray[1] = sentDat2;
/* third byte */
sentDat3 = Serial.read(); //gets one byte from serial buffer
myArray[2] = sentDat3;
delay(1000);
}
}//END OF LOOP
It would be really helpfull if you could take a quick look.
It would be really helpful if YOU read the documentation for Serial.write(). The write() method does NOT expect an int. So, don't pass it an int.
if (Serial.available() > 0) {
If there is one or more bytes in the buffer, how many can you read? THREE is the WRONG answer.
You do understand, I hope, that the transmission of serial data does NOT follow the UPS delivery model, where they guarantee to deliver your package or return it to you. It follows the USPS model, where they guarantee to try to deliver you package, undamaged. If they can't, or if gets damaged, it is simply thrown away, and you are NOT told.
Sending binary data IS faster, but you have NO way of keeping it in sync. If you are sending R, G, B values, for instance, and the G byte gets lost, you'll apply the blue component to the green LED and the next red component to the blue LED, which will cause the color to be all wrong. And there is nothing you can send that is not a valid value, so you have no way of keeping in sync or of knowing that you are out of sync.
Sending ASCII data results in more data, but with lots of ways to know that serial data got lost/corrupted, so you know whether to use the data, or not.
PaulS:
It would be really helpful if YOU read the documentation for Serial.write(). The write() method does NOT expect an int. So, don't pass it an int.
the serial communication is with Xbees (series1). i can only use Serial.write(). I have tried to use Serial.print() or Serial.println() but those don't send the appropriate value
the serial communication is with Xbees (series1). i can only use Serial.write(). I have tried to use Serial.print() or Serial.println() but those don't send the appropriate value
You CAN use Serial.print() and Serial.println() with the XBees. If you are getting "wrong values" it is your code that is wrong.