I'd like a controlling device to send a 1/0 to one of the arduino's (and a complementary logic level
to the other arduino) which, if 1, will set that Arduino's RX/TX line active so that arduino can
RX/TX with the controlling device.
I am trying to make an I/O expanded (using arduino's only), but I don't know do to swtich on/off Serial.begin(speed).
Can this be done and how?
I should say the ControllingDevice only has one RX/TX line hence I need some switching method to turn one arduino while 'sleeping' the other.
I think it can be done. You must be careful with the TX output of the two Arduinos; if the one sends a logical 1 and the other one a logical 0, it's quite unhealthy.
The device that is not sending needs to place its TX in tristate (pinMode INPUT). Next the other device can do a Serial.begin() and transmit.
The device (Arduino) code could look something like below. No idea if it works and use at own risk; it would be something that I would try though if it was my requirement not to add any additional hardware.
void loop()
{
static bool serialEnabled = false;
// if we had the serial connection and the request pin is low
if (requested == false && serialEnabled == true)
{
// flush remaining data and stop
Serial.end();
// tx pin tristate
pinMode(1, INPUT);
// indicate we no longer have the serial connection
serialEnabled = false;
}
// if we did not have the serial connection and the request pin is high
if(requested == true && serialEnabled == false)
{
// setup serial connection
Serial.begin(9600);
// indicate that we have the serial connection
serialEnabled = true;
}
// if we have the serial connection
if(serialEnabled == true)
{
// send data
Serial.println(someData);
}
}
The better way though would be a multiplexer (as mentioned) or use RS485. The latter is a bus and takes care of the tristate condition (if implemented correctly).
There are simpler and more sensible ways to increase the i/o of an Arduino than using multiple Arduinos.
First step: post your schematic. There are often ways to reduce the i/o pins needed by selecting the right components, e.g. i2c backpacks for LCD displays or arranging buttons or LEDs into matrices.
If the number of i/o cannot be easily reduced, there are several types of i/o expansion chips available, depending on what type of i/o are needed.