For my Arduino Mega (which has multiple serial ports), I'm writing some code which is the same for the different ports.
So a part of that code could be: Serial2.available() Serial3.available()
But this way, I have to write everything twice!
-> Is there a way to replace the numers 2 & 3 by a variable? So that it becomes something like Serial+number.available()?
(in flash actionscript you could do: ["serial"+number].available() )
So at one moment, I might want to run the code for Serial 2. But at another moment, I want to run it for Serial 3.
I think your example runs the code for both Serials at the same time right?
pap, why not post some psuedo code of what you would like to do.
If you want to use an index variable you could create an array of Serial ports:
HardwareSerial port[] = {Serial, Serial1, Serial2, Serial3};
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
for(int i=0; i < 3; i++)
port[i].begin(9600);
}
void loop() // run over and over again
{
port[0].print("sizeof port ");
port[0].println(sizeof(port));
port[1].println("hello");
}
The array holds the entire structure so this uses 18 bytes per port. You could use an array of pointers that is similar to what AlphaBeta posted except it uses an index instead of the Serial pointer directly
HardwareSerial* port[] = {&Serial, &Serial1, &Serial2, &Serial3};
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
for(int i=0; i < 1; i++)
port[i]->begin(9600);
}
void loop() // run over and over again
{
port[0]->print("sizeof port ");
port[0]->println(sizeof(port));
}