Hi, for a project i need to communicate an arduino due with an arduino nano every. The final purpose is for the Due to send instructions to the nano (to control the position of a motor), and for the nano to send back data about the position of the encoder of the motor.
I was thinking of using serial communication however i am struggling to make the code work.
Connection :
Ground due => ground nano every
TX Serial3 due => logic converter => RX Serial nano
RX Serial 3 due => logic converter => TX Serial Nano
The two boards are connected by two usb wire to the same PC.
I partially created the code and I encounter problems already :
code DUE (taken from a tutorial on the forum except for comE) :
int long t=0;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
Serial.begin(9600);
t=millis();
}
void comE(double cons){
Serial1.println(String(cons));
}
void loop() {
if (millis()-t>100){
comE(2.3);
t=millis();
}
}
Apparently no, 8 byte for due and 4 byte for nano every. Does this create a problem ? i changed the numChars variable to 64 but it doesn't change anything.
int long t=0;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
Serial.begin(9600);
t=millis();
}
void comE(uint32_t cons){
Serial1.println(String(uint32_t (cons)));
}
void loop() {
if (millis()-t>100){
comE(2);
t=millis();
}
}
int long t=0;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
Serial.begin(9600);
t=millis();
}
void comE(int cons){
Serial1.println(String(cons));
}
void loop() {
if (millis()-t>100){
comE(2);
t=millis();
}
}
I get the same result
I tried with an arduino Uno instead of the Due and the sketch work correctly only 2 is print.
The whole way you are receiving the data is full of problems. As written, it is unlikely to work reliably. You attempt to receive using an end marker, but also print based on time. ALL you need is the end marker. You set a flag that says you got the end marker, but then don't seem to use it. You also make no accommodation for multiple characters arriving between calls to the receive function. Keep calling the receive function UNTIL the flag is set, then print the result, and flush the received string to start over. Get rid of the timing part of the receive code entirely. Also, you are not checking for a buffer over-run, which WILL bite you in the a$$ at some point.
Also, if you send the numeric data as ASCII strings, then you don't need to care if the sizes of ints and floats are different on different processors, nor do you need to care if one is big-endian and the other is little endian. And, ASCII makes it far to debug, since you can easily SEE the received data.
I have decided to replace the nano by an other due as i can't replace the first due. It work fine and their is no weird value. The problem clearly come from the different format of precessors, I don't precisly know why.
The whole way you are receiving the data is full of problems.
Yes I will ameliorate this now, but the problem of having on my serial '5' don't seem to come from this as I had already tried other functions to retrieve data which gave me the same result.
Anyway thanks you both for your time, have a great day.
Sorry for the double post that'll teach me to not read the rules diagonally.
I only opened the due console, but I always had the the two boards powered by the same pc on which I programmed.