You can use two arrays, one to store the values and one to store the pin that triggered the storing of a value. Below demonstrates (assuming that a HIGH signal triggered.
const uint8_t maxStorage = 100;
uint16_t reading[maxStorage];
uint8_t pinIndex[maxStorage];
const uint8_t trigPins[] = {3, 4, 5};
void setup()
{
// put your setup code here, to run once:
}
void loop()
{
static uint8_t index;
for (uint8_t cnt = 0; cnt < sizeof(trigPins) / sizeof(trigPins[0]); cnt++)
{
if (digitalRead(trigPins[cnt]) == HIGH)
{
reading[index] = analogRead(A0);
pinIndex[index] = cnt;
}
index++;
if (index == maxStorage)
{
index = 0;
}
}
}
In above the index of the trigger pin is stored at the same time as the reading. I've used the pin index instead of the physical pin number to prepare for the last code below.
It would be neater to use a struct that can allow you to indicate which pin is associated with the entry. That would look like below
const uint8_t maxStorage = 100;
const uint8_t trigPins[] = {3, 4, 5};
struct READING
{
uint16_t reading;
uint8_t pinIndex;
};
READING readings[maxStorage];
void setup()
{
// put your setup code here, to run once:
}
void loop()
{
static uint8_t index;
for (uint8_t cnt = 0; cnt < sizeof(trigPins) / sizeof(trigPins[0]); cnt++)
{
if (digitalRead(trigPins[cnt]) == HIGH)
{
readings[index].reading = analogRead(A0);
readings[index].pinIndex = cnt;
}
index++;
if (index == maxStorage)
{
index = 0;
}
}
}
And if you're worried about RAM usage, you can use bitfields.
const uint8_t maxStorage = 100;
const uint8_t trigPins[] = {3, 4, 5};
struct READING
{
uint16_t reading: 10; // analogRead returns a 10 bit value
uint16_t pinIndex: 2; // 2 bits allow for 4 trigger pins
};
READING readings[maxStorage];
void setup()
{
// put your setup code here, to run once:
}
void loop()
{
static uint8_t index;
for (uint8_t cnt = 0; cnt < sizeof(trigPins) / sizeof(trigPins[0]); cnt++)
{
if (digitalRead(trigPins[cnt]) == HIGH)
{
readings[index].reading = analogRead(A0);
readings[index].pinIndex = cnt;
}
index++;
if (index == maxStorage)
{
index = 0;
}
}
}
The change is in the struct where above code now specifies how many bits a variable uses. You can check the difference by printing sizeof(READING) in setup. For the first code with the struct the size should be 3 bytes, for the second one it should be two bytes.