Parallel connection between 2 Arduinos

I am trying to make a parallel connection between a Mega2560 and a Nano with the former writing the 8 bits of a byte onto 8 pins which are hardwired to pins on the nano. (The sketch shows another two pins which are not relevant to my enquiry.) The boards are grounded to each other and both have serial monitors working, the USB connectors to a PC.

For this purpose I have hard coded the number to send as 0 and added some diagnostic Serial print lines to each. The value sent is zero and the Mega monitor shows all pins low. A check with a multimeter confirms that.
On the Nano, the meter unsurprisingly confirms that the pins at the other ends of the connecting wires are low. However the monitor on the Nano shows pin[4] as high. This is consistent, no matter how many times I reset and reboot the devices and in what order and it is stable. The line in the Nano code after reading the pins uses bitWrite to reassemble the values returned by digitalRead and it does this correctly as 16 given the wrong pin status.

I have tried a short delay between pin reads/writes, I have used several alternative pin pairs, I have tried a single alternative end, I have swapped the 4th pin in both directions of the array in case there is some endian thing I don't understand. I know that read pin[0] will read the LSB. I swapped the Nano for another Nano. I swapped the Mega2560 for a Uno R4 and got the same result. I disconnected pin 4 (wouldn't expect that to make any difference since I am transmitting zero - and it didn't).I would have said that something is getting scrambled in the Serial output except that a) it is very consistent and b) the bitWrite() function is not reading the monitor.

If I change the input value from 0 to 255, pin[5] on the Nano stays low. Again that is very consistent, any value needing pin 5 high will be wrong and any value needing pin4 low will be wrong.
What have I got wrong?

Sender


//simple //el sender
#define numCVPins 8
#define numColPins 2
byte cvPin[] = {2, 3, 4, 5, 6, 7, 8, 9}; //these map to {A2, A3, A4, A5, A6, 7, 8, 9} on other board 
byte colPin[] = {11, 12};   //10 not avaialable, used by ethernet shield
byte cv =255;
byte col = 2;     //replace magic numbers later

void sendToPins() {
  for (int i = 0; i < numCVPins; i++){ 
    byte pinVal = bitRead(cv, i) ;
    //delayMicroseconds(100); 
    digitalWrite(cvPin[i], pinVal);   //writes LSB to pin[0] etc
    Serial.print("pin no  ");
    Serial.print(cvPin[i]);
    Serial.print(" is ");
    Serial.println(pinVal);
  }
  for (int i = 0; i < numColPins; i++){    
    digitalWrite(colPin[i], bitRead(col, i));
  }
}


void setup() {
  for (int i = 0; i < numCVPins; i++){
    pinMode(cvPin[i],OUTPUT);
  }
  for (int i = 0; i < numColPins; i++){
    pinMode(colPin[i],OUTPUT);
  }
  Serial.begin(9600);
  delay(500);
}

void loop() {
  sendToPins();
  delay(5000);    // so I can read monitor but no different without this

}

Out put: (board pin nos)
pin no 2 is 0
pin no 3 is 0
pin no 4 is 0
pin no 5 is 0
pin no 6 is 0
pin no 7 is 0
pin no 8 is 0
pin no 9 is 0

Receiver

// SimpleParallelRcvr.txt

#define numCVPins 8
#define numColPins 2
byte cvPin[] =   {A2, A3, A4, A5, A6, 7, 8, 9} ; 		//these map to{2, 3, 4, 5, 6, 7, 8, 9}on other board
byte colPin[] = {10, 11};   // 11,12 on other


void rcv() {
	byte cv = 0;
  for (int i = 0; i < numCVPins; i++){ 
    byte p = digitalRead(cvPin[i]);
    //delayMicroseconds(100);
	cv = bitWrite(cv,i,p);
	
    Serial.print(" pin[") ;
    Serial.print(i) ;
    Serial.print("] is  ") ;
    Serial.println(p);	
  }
  Serial.print("cv is  ");
		Serial.println(cv); 
  byte col = 0;
  for (int i = 0; i < numColPins; i++){
		  
    digitalRead(colPin[i]);
	// and do nothing
  }
}


void setup() {
  for (int i = 0; i < numCVPins; i++){
    pinMode(cvPin[i],INPUT);
  }
  for (int i = 0; i < numColPins; i++){
    pinMode(colPin[i],INPUT);
  }
  Serial.begin(9600);
  delay(500);
}

void loop() {
  rcv();
  delay(5000);

}

Output: Pin array nos
pin[0] is 0
pin[1] is 0
pin[2] is 0
pin[3] is 0
pin[4] is 1
pin[5] is 0
pin[6] is 0
pin[7] is 0
cv is 16

you can only use analogRead() on A6 or A7 for the Nano

➜ don't use A6 or you need to modify your code to read like this

  for (int i = 0; i < numCVPins; i++) {
    byte p; 
    if ((cvPin[i]) == A6) || (cvPin[i]) == A7)) 
      p = (analogRead(cvPin[i]) > 512) ? HIGH : LOW; 
    else 
      p = digitalRead(cvPin[i]);
...
1 Like

As well as setting the data bits, you need an additional two bits called "strobe" and "acknowledge"

When you send a new lot of data you set the strobe bit low, when the receiver sees this bit low, it reads the data and then sets the acknowledge high, and when the sender sees this bit as high it removes the strobe setting it to high.

This is known as a hand shaking procedure, and needs to be done with an other set of pins for data transfer in the other direction.

Thanks. I am wondering why that would give me a problem on pin5 which is board 7 at both ends. I'll do some more pin swapping and maybe find out.

OK. On the version of this in my project I have a new data pin which toggles state when data is sent. That was my attempt at the procedure but now I know what I'm looking for I can read up abt it.

Something wrong
When I run the sender sketch it prints out all 1s like it should but your print out shows zeros.

That's because I ran the sketch with input 255 after I tried sending 0. So the code is for 255 and the output is for 0.

Interesting that you get all 1's because I get a low on pin5.

I'm just going buy the serial prints not the actual pin states.
So, it prints all 0s if I change i o Cv=0

well you have

and this is index 4 of this array (so the 5th entry)

➜ which is A6

Yes pin[4] was reading high when it should be low ( changing away from A6 to D6 seems to have fixed that) but addtionally pin[5] - board 7 at both ends - is reading low when it should be High. Looking at it closely when I uploaded with 255 as the value I got one print of all 1's and then every repeat has pin[5] low.

Pin[5] is OK, that was my finger trouble.

:slight_smile: never happened to me

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.