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