Streamline my code!

hi,

my led project is upgrading, and looking at the code, there’s a lot of copy and pasting.

i wonder if someone can help me with squashing this code down. :slight_smile:

variables;

  //calibration routine
  int sensorValue1 = 0;         // the sensor value
  int sensorValue2 = 0;         // the sensor value
  int sensorValue3 = 0;         // the sensor value
  int sensorValue4 = 0;         // the sensor value
  int sensorValue5 = 0;         // the sensor value
  int sensorValue6 = 0;         // the sensor value
  int sensorValue7 = 0;         // the sensor value
  int sensorValue8 = 0;         // the sensor value
  int sensorValue9 = 0;         // the sensor value
  int sensorValue10 = 0;         // the sensor value
  int sensorValue11 = 0;         // the sensor value
  int sensorValue12 = 0;         // the sensor value
  int sensorValue13 = 0;         // the sensor value
  int sensorValue14 = 0;         // the sensor value
  int sensorValue15 = 0;         // the sensor value
  int sensorValue16 = 0;         // the sensor value

main code
in this the variables are;
lastBright1, lastBright2, lastBright3, etc
mappedValue1, mappedValue2, mappedValue3, etc
ledpin1, ledpin2, ledpin3, etc

static int lastBright1 = 0;                                                    // set the lastbright variable
if (mappedValue1<sensitivity) {                                                // check to see if the ir is detecting above the base level 
//  Tlc.clear();                                                               // clear all pins   // probably not needed
  Tlc.set(led1, 0);                                                            // set tlc of led1 to 0
  Tlc.update();                                                                // send the data to the tlc chip
//  analogWrite(ledpin1, LOW);                                                 //old code             // turn the led off
      unsigned long currentMillis = millis();
      if (lastBright1 > 0 && (currentMillis - previousMillis > interval))      // check to see if the last brightness is more than 0 and the interval time has passed
      lastBright1 = lastBright1/100*fadespeed;                                 // Dim the light til it reaches 0
      previousMillis = currentMillis;                            
}  else {
  lastBright1 = maxBright;                                                     //  Set the brightness to full
}
  if (lastBright1 < lowestbrightness) {                                        // check to see if the led has got to a low level
    lastBright1 = 0;                                                           // then turn the led off
  }
//  analogWrite(ledpin1, lastBright1);                                         // old code            // write the brightness to the led
  Tlc.set(led1, lastBright1);                                                  // set the brightness of the led1
  Tlc.update();                                                                // send the data to the tlc

calibration

  // apply the calibration to the sensor reading
  // max is set to 4095 for the 5940
  int mappedValue1 = map(sensorValue1, sensorMin1, sensorMax, 0, 4095);
  int mappedValue2 = map(sensorValue2, sensorMin2, sensorMax, 0, 4095);
  int mappedValue3 = map(sensorValue3, sensorMin3, sensorMax, 0, 4095);
  int mappedValue4 = map(sensorValue4, sensorMin4, sensorMax, 0, 4095);
  int mappedValue5 = map(sensorValue5, sensorMin5, sensorMax, 0, 4095);
  int mappedValue6 = map(sensorValue6, sensorMin6, sensorMax, 0, 4095);
  int mappedValue7 = map(sensorValue7, sensorMin7, sensorMax, 0, 4095);
  int mappedValue8 = map(sensorValue8, sensorMin8, sensorMax, 0, 4095);
  int mappedValue9 = map(sensorValue9, sensorMin9, sensorMax, 0, 4095);
  int mappedValue10 = map(sensorValue10, sensorMin10, sensorMax, 0, 4095);
  int mappedValue11 = map(sensorValue11, sensorMin11, sensorMax, 0, 4095);
  int mappedValue12 = map(sensorValue12, sensorMin12, sensorMax, 0, 4095);
  int mappedValue13 = map(sensorValue13, sensorMin13, sensorMax, 0, 4095);
  int mappedValue14 = map(sensorValue14, sensorMin14, sensorMax, 0, 4095);
  int mappedValue15 = map(sensorValue15, sensorMin15, sensorMax, 0, 4095);
  int mappedValue16 = map(sensorValue16, sensorMin16, sensorMax, 0, 4095);

thanks!

Arrays are your friend, and maybe a FOR loop or two.