Hey Everyone,
I'm new to Arduinos but old to code. To start myself off, I thought it would be interesting to get two Arduino (MEGA 2560 R3) boards talking.
In an effort to start simple, I thought I'd just see if each one could read a 'high' line from the other.
On one Arduino, I have used pins 14 and 21, and I have those connected to pin 21 and 14 on the other.
Here's a sorry attempt at ascii art, trying to show that the cables "cross"
14 14
X
21 21
The idea is, pin 14 goes HIGH, a digitalRead takes place on 21 (which should be HIGH)
if the HIGH is detected, turn on a status light, and unset some values so if the cables are unplugged, the light goes off. Should be very simple.
The problem is I'm seeing all kinds of erratic behavior. I have 3 Arduinos here, all of them react to the code below differently. One of them just shows the "Link Light" as on, always, unless I ground pin21. Another two will somehow pass the link-light back and forth, so if one has it shown, the one it's connected to will power up, and then the light turns off on the first one and turns on on the second one.
The underpinning assumption of this code is that these states are persistent unless manually changed - but that doesn't seem to be the case?
void setup() {
pinMode(14, OUTPUT); //Rx
pinMode(21, INPUT); //Tx
pinMode(11, OUTPUT); //Link Light
}
void loop() {
int Link = 0;
digitalWrite(14, HIGH); //send ON
Link = digitalRead(21); //pin21 is connected to pin14 on the other arduino
if(Link == HIGH){ //if pin21 was read as HIGH, link is good
Link = 0; //unset Handshake variable
digitalWrite(11, HIGH); //set handshake light
}
}
I've found in some cases simply picking up an Arduino and rotating it to certain positions will cause the link-light to go on or off. The natural assumption is "bad solder job on the led" or "loose cables". Neither are the case - the connections are firm and the led board is very thoroughly tested - I don't know what more could be done there.
Is there some incredibly obvious sin of Arduino programming I've committed?