Array , Byte, to activate multiple pins

Hi,
I'm working in a project (tester fixture) where I would cycle multiple Analog ports when activating different stimulus using Digital ports

Reading (Analog) : I have 8 ports to read and compare to a threshold to determine the Pass / Fail state (High/ Low).
This is assigned into and int SenVal1, SenVal2…etc

Output: 8 digital ports to be circled through (on 10 combinations) and take the reading of the Analog sensors on each time.

The routine will be:

for (int x = 1; x <= 10; x++){
switch (x) {
case 1:
Set Combination1 (by setting 8 digital ports)
Read 8 Analog sensors:
Read Analog Value, compare to threshold, assign binary (for pass/fail)
Assign each binary (pass/fail) to a byte length Variable_ byte SensorBinar
Compare that SensorBinary variable to the expected byte value
If (comparation fail) {
Print error
Break;
}
Case 2:
Set Combination2 (by setting 8 digital ports)
Read 8 Analog sensors:
Read Analog Value, compare to threshold, assign binary (for pass/fail)
Assign each binary (pass/fail) to a byte length Variable_ byte SensorBinar
Compare that SensorBinary variable to the expected byte value
If (comparation fail) {
Print error
Break;
}
Case 3:

}
…and so on until running the 10 combinations. If any fails, the test should stop and report Fail.
The test woud end after the 10 cycles and report (Pass/Fail)

Can I have help with example code routine to accomplish this or any better way to archive similar results?

Thanks!

PD. Ardware is Arduino Mega 2560

Rather than ten repetitive sections of code create an array to hold the combinations. Put the testing code inside a for loop and use the loop index to iterate through the combinations, substituting each one in turn.

for(int i=0; i<10; i++){
  Set combination[i];
  All the other testing stuff
}

Read up on arrays, for() loops, and other stuff: Arduino Reference - Arduino Reference

First start by defining arrays for your pins

// digital pins for pattern
const uint8_t patternPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
// analog pins for reading
const uint8_t measPins[] = {A0, A1, A2, A3, A4, A5, A6, A7};

To know how many elements there are in any type of array (char, int, float, struct, ...), you can use a macro; add below to the top of your code

// calculate the number of elements in any type of array (char, int, float, struct, ...)
#define NUMELEMENTS(x) (sizeof(x) / sizeof(x[0]))

You have a certain pattern that you apply on the digital pins and you have an expected resulting pattern on the analog pins. Combine a stimulus pattern with the resulting pattern in e.g a struct or class. I'm still far more a C programmer than a C++ programmer and use a struct.

struct PATTERN
{
  const uint8_t pattern[NUMELEMENTS(patternPins)];  // stimulus pattern on digital pins
  const uint8_t expected[NUMELEMENTS(measPins)];    // expected pattern on analogRead
  int16_t readings[NUMELEMENTS(measPins)];          // readings of analogRead
};

I've added a field for the analog readings. Using NUMELEMENTS makes that anything is scalable; if you only need 4 digital pins, change patternPins and pattern will follow. If you have different thresholds for a certain pattern, you can add a threshold field or an array of thresholds for every analog pin.

Now you can create an array of patterns.

PATTERN patterns[] = {
  {{LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW},  // stimulus pattern
   {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW},  // expected pattern; LOW indicates below threshold
   {0, 0, 0, 0, 0, 0, 0, 0}},                 // initial measurement
  {{HIGH, LOW, LOW, LOW, LOW, LOW, LOW, LOW},
   {LOW, HIGH, LOW, LOW, LOW, LOW, LOW, LOW},
   {0, 0, 0, 0, 0, 0, 0, 0}},
};

The above contains 2 patterns each having a stimulus pattern, an expected resulting pattern and initial values for the ananlog readings.

In setup(), you can now iterate through the patternPins and set them to output. You don't have to do anything for the ananlog pins.

void setup()
{
  Serial.begin(115200);

  Serial.println(F("Setting up pattern pins"));
  for (uint8_t cnt = 0; cnt < NUMELEMENTS(patternPins); cnt++)
  {
    pinMode(patternPins[cnt], OUTPUT);
  }

  Serial.println(F("Tester ready"));
}

Note that this will immediately apply a pattern LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW to the output pins.

In loop() you can iterate through the patterns using e.g. a for-loop.

void loop()
{
  for (uint8_t patternCnt = 0; patternCnt < NUMELEMENTS(patterns); patternCnt++)
  {
    Serial.println(F("Setting pattern"));
    for (uint8_t pinCnt = 0; pinCnt < NUMELEMENTS(patternPins); pinCnt++)
    {
      ...
      ...
    }
    Serial.println(F("Reading"));
    for (uint8_t pinCnt = 0; pinCnt < NUMELEMENTS(measPins); pinCnt++)
    {
      ...
      ...
    }
    Serial.println(F("Comparing"));
    for (uint8_t pinCnt = 0; pinCnt < NUMELEMENTS(patterns[patternCnt].readings); pinCnt++)
    {
      ...
      ...
    }
  }
}

For now, I'll leave the rest of the puzzle to you. This is just to give you the idea.

It will probably be better to write a function that handles one pattern and call that from loop() with e.g. an index to indicate the pattern to handle.

Great inputs . Thanks!
I will exercise with the examples

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.