Variable variable names for code tidy up?

Hi there,

I am writing some code to collect data from quite a few sensors, the data will be stored in a timestamped array.

I have a nice method for collecting the data, however it seems highly inefficient on paper as I am simply copy/pasting the same piece of code 20 times (for 20 sensors), see a 2 sensor example below:

  // START Read Data S01 
  if(digitalRead(S01pin) != S01laststate)
  {
    // Prepare to pickup change next time
    S01laststate = digitalRead(S01pin);
    // Check if this is a new value or a continuation of an old reading
    if(S01timestamp[S01index] < millis() - readingdelay)
    {
      // If new reading move onto next array value
      S01index++;
    }
    S01reading[S01index]++;
    S01timestamp[S01index] = millis();
  }
  // START Read Data S02 
  if(digitalRead(S02pin) != S02laststate)
  {
    // Prepare to pickup change next time
    S02laststate = digitalRead(S02pin);
    // Check if this is a new value or a continuation of an old reading
    if(S02timestamp[S02index] < millis() - readingdelay)
    {
      // If new reading move onto next array value
      S02index++;
    }
    S02reading[S02index]++;
    S02timestamp[S02index] = millis();
  }

Could I somehow put this in it's own loop to save repeating the same piece of code 20 times?

Many thanks in advance!

this is perfect situation for creating a class. Create your own class library.

Create an instance for each sensor and then loop through them all in the loop function

something like

Sensor *sensor[20];

void setup() {
    sensor[0] = new Sensor(1,2,3); // some parameter into the constructor
    sensor[1] = new Sensor(2,3,4);
   ..
  .. etc
}

void loop() {
     for (var i = 0; i < 20; i++) {
         sensor[i]->doWork();
    }
}

No need for a class. Just put the sensor pin numbers in an array and use a for loop to read them in turn.

No need for a class

... but a good opportunity. You don't need to struggle with library and include files, if you don't want to.

Or use an array of structs, instead of a handful of parallel arrays for pin numbers, laststate, this and that.
A class is just a nice and convenient enhancement of a struct.

@michael_x: Indeed, a class is little more than a struct with functions (i.e., methods).

I'd use classes. THere are a bunch of private like variables he's using specific to the sensor. All he's doing now is adding a number to the variable.

And put the class or struct in its own .h and .cpp files.
Arduino IDE has an obscure bug that will sometimes gotcha if ya don't, or so I have read here.

All he's doing now is adding a number to the variable.

Crying out for an array then.

It's got arrays. It's crying for a loop.

Here's the Arduino IDE for-next loop example. It's stupid but it shows how for-next loops work.

/*
  For Loop Iteration
 
 Demonstrates the use of a for() loop. 
 Lights multiple LEDs in sequence, then in reverse.
 
 The circuit:
 * LEDs from pins 2 through 7 to ground
 
 created 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Tom Igoe 

This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/ForLoop
 */

int timer = 100;           // The higher the number, the slower the timing.

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 2; thisPin < 8; thisPin++)  {
    pinMode(thisPin, OUTPUT);      
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 8; thisPin++) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(thisPin, LOW);    
  }

  // loop from the highest pin to the lowest:
  for (int thisPin = 7; thisPin >= 2; thisPin--) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}

GoForSmoke:
It's got arrays. It's crying for a loop.

yeah, you could be right.