fast communication between two arduinos

I need to pass a 4 digit number very fast between two Due's with a protocol that will not delay ether one of the boards,

I tried the I2C and using as well the analog read / write on both boards but with out success to reach the needed speed.

I need the communication to pass within a few milliseconds.

Any idea?

Thanks,
Yossi

When you say 4 digit number are you referring to ASCII of 4 characters ("9999") or 4 bytes of data? If it's ASCII then you can at least double the speed by just sending 2 bytes (0x27, 0x0F).
Serial might be able to push to 230400 baud (or higher) and is interrupt driven (at least it is on AVR) so as long as the TX buffer does not fill will not block your code.

I mean a 4 digit number and I am using the pulsIn() function so I don't know if there is an interruption with the interrupts.

I read that the SPI protocol is a fast one. can I do this with the due's?

Thanks,

pcyossi:
I mean a 4 digit number and I am using the pulsIn() function so I don't know if there is an interruption with the interrupts.

I read that the SPI protocol is a fast one. can I do this with the due's?

SPI is fast but I don't know if the code is blocking. pulseIn() is probably blocking code though. Why do you need pulseIn? Maybe using an interrupt pin will do the trick and free you main code to do what it wants at what speed it wants.

Describe what your trying to read and why your sending it to another Due as fast as possible.

I need the pulsin() to measure the speed of a rotating wheel that generate a square wave, the wheel speed is changing very fast and I need to catch it at a very specific time.

So the pulsin gives the half time of the cycle. the time that I need to fined is close to the stooping point of the wheel so pulsin really delays my Due so I need a second board to gather other data at a fast rate and send the results to the first one.

Do you have enough unused digital pins to make an 8, 16 or 32 bit communications bus between the two Dues?

Yes, But in case you mean to write the data with the IO's then I do not want to load the system to change 16 IO's every 5 ms in order to update the value.

It need to be something like on demand reading by the master from the slave.
(Master runs the PolsIn() function Slave averages analog inputs at a fast rate).

Thanks,
Yossi

Time how long it takes to set 16 digital pins then calculate the overhead. If the digitalWrite function is too slow then use direct port manipulation.

I would be very surprised if parallel communication "loaded the system" more than serial comms.

5ms? You think that's fast? The Due can perform 420,000 instructions in that time. Loading bits into output pin registers is one of the core instructions so you could definitely do this thousands and thousands of times while you're waiting for 5ms to tick by.

pulseIn() is a blocking function, which doesn't let any part of your program run while it's waiting for the pulse to finish. With a bit of thought, you should be able to do all the necessary processing in the half of the rotation when the pulse isn't active.

Thanks guys,

At the end I re-Checked the I2C protocol and understood it can only pass Chars ether by array or singles and managed to work with the code from here: https://www.arduino.cc/en/Tutorial/MasterReader
and created my version to pass a 4 digit code and it work quite fast (less than 1ms).

// Wire Master Reader
int x,y;
#include <Wire.h>

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200); // start serial for output
}

void loop()
{
Wire.requestFrom(2, 6); // request 6 bytes from slave device #2

int a = Wire.read(); // receive a byte as character
int b = Wire.read(); // receive a byte as character
int c = Wire.read(); // receive a byte as character
int d = Wire.read(); // receive a byte as character
int e = Wire.read(); // receive a byte as character

if(a== 45)
x=-((b-48)*1000+(c-48)*100+(d-48)*10+e-48);
else
x=(b-48)*1000+(c-48)*100+(d-48)*10+e-48;

Serial.println(x);
delay(1);
}

And for the slave

// Wire Slave Sender

#include <Wire.h>

String inString;
char A[6];
int x=0,i,val,Sign;

void setup()
{
Serial.begin(115200); // start serial for output
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}

void loop()
{
Sign=0;
val= -1234; // Value to pass

if(val<0){ // if value negative
val=-val;
Sign=1;}

inString = String(val,DEC);
inString.toCharArray(A,6);

if(inString.length()!=5){ // manipulation to bring carateres to the right if string is less then 5 caracteres
for(x=0; x<(5-inString.length());x++){
for(i=0;i<5;i++)
A[5-i]=A[4-i];
A[0]=48;}}

if (Sign==1)
A[0] = 45;

delay(1);
}

void requestEvent()
{
Wire.write(A); // respond with message of 6 bytes

}