Thank you for your advice!
I have now tried a different approach and made use of arrays (i understand now how much easier it gets) and came up with this:
int rxPins[] = {5, 6, 7, 8, 9, 10, 11, 12}; // input pins for rx 0 thru 7
int rxState = 0; // current rx selected
int count = 0; // counters for rx 0 thru 7
int tx = 13; // output pin for tx
// output pins for multiplexer
int r0 = 0; //value of select pin at the 4051 (s0)
int r1 = 0; //value of select pin at the 4051 (s1)
int r2 = 0; //value of select pin at the 4051 (s2)
int countmux = 0; //which y pin we are selecting
void setup()
{
// initialize rx pins as input
for (count=0;count<8;count++) {
pinMode(rxPins[count], INPUT);
}
// initialize pins as output for multiplexer
pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
}
void loop()
{
// read inputs and count pulses
for (count=0;count<8;count++) {
rxState = digitalRead(rxPins[count], HIGH);
}
// if input is detected, turn on output
if (rxState >= 1)
{
digitalWrite(tx, HIGH);
}
else
{
digitalWrite(tx, LOW);
}
// code for multiplexer
for (countmux=0; countmux<=7; countmux++) {
// select the bit
r0 = bitRead(countmux,0); // use this with arduino 0013 (and newer versions)
r1 = bitRead(countmux,1); // use this with arduino 0013 (and newer versions)
r2 = bitRead(countmux,2); // use this with arduino 0013 (and newer versions)
//r0 = count & 0x01; // old version of setting the bits
//r1 = (count>>1) & 0x01; // old version of setting the bits
//r2 = (count>>2) & 0x01; // old version of setting the bits
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
//Either read or write the multiplexed pin here
}
}
I have no idea if this works but maybe it looks better? Any advice?
The problem now is how to compare the inputs and choose the one with most pulses each 10ms?
Best regards,
Swoshie