I am sending three signals from one Arduino mega to another using xbees. On the receiving end I am using these signals in a Simulink code to control servo motors. Currently it is set up to read from analog pins without the wireless set up. With the wireless technology the signals are all being sent and received through one serial port. I believe I would need to change the inputs in Simulink to a serial receive block instead of an analog pin block. The problem is the documentation for the serial receive block says you can not assign more than one serial receive block to the same serial port. If anyone has a solution to this it would be greatly appreciated!
receiving code
int received1;
int received2;
int received3;
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available() )
{
received1, received2, received3 = Serial.read();
}
}
transmitting code:
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
int sensorValue3 = analogRead(A2);
Serial.write(sensorValue1, sensorValue2, sensorValue3);
delay(1);
}
-
How are the three signals encoded in the bitstream?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2014年03月24日 01:35:18 +00:00Commented Mar 24, 2014 at 1:35
-
Here is my receiving end arduino code. Not sure if its correct yet. I will add it to the question.user734– user7342014年03月24日 02:44:12 +00:00Commented Mar 24, 2014 at 2:44
-
What does your sending code look like?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2014年03月24日 03:15:07 +00:00Commented Mar 24, 2014 at 3:15
-
I will add that to the question as welluser734– user7342014年03月24日 04:07:33 +00:00Commented Mar 24, 2014 at 4:07
-
What is the result shown on the serial monitor? Let the three values received on the receiver code be shown on the serial monitor. Tell us exactly what do you have? Is it a trash? or nothing is received?Adel Bibi– Adel Bibi2014年03月24日 18:04:22 +00:00Commented Mar 24, 2014 at 18:04
1 Answer 1
You need to put your data into a packet and then send the packet to Simulink.
You have tried this by this line in your transmitting code:
Serial.write(sensorValue1, sensorValue2, sensorValue3);
However, as you can see from the Serial Write documentation it does not work like that. The function cannot accept 3 arguments.
Try this instead:
Serial.write(sensorValue1);
Serial.write(sensorValue2);
Serial.write(sensorValue3);