Anyway to create this code in a simpler way?

Hello everyone. My first post along with my first code. This is a simple indicator setup that will light up an LED when a box is in the location. The code works fine, I'm just curious if there would be a shorter way to write it.

const int analogPin1 = A1;  // pin that sensor 1 is attached to
const int analogPin2 = A2;  // pin that sensor 2 is attached to
const int analogPin3 = A3;  // pin that sensor 3 is attached to
const int analogPin4 = A4;  // pin that sensor 4 is attached to
const int analogPin5 = A5;  // pin that sensor 5 is attached to
const int analogPin6 = A6;  // pin that sensor 6 is attached to
const int analogPin7 = A7;  // pin that sensor 7 is attached to
const int analogPin8 = A8;  // pin that sensor 8 is attached to
const int analogPin9 = A9;  // pin that sensor 9 is attached to
const int ledPin1 = 2;      // pin that LED 1 is attached to
const int ledPin2 = 3;      // pin that LED 2 is attached to
const int ledPin3 = 4;      // pin that LED 3 is attached to
const int ledPin4 = 5;      // pin that LED 4 is attached to
const int ledPin5 = 6;      // pin that LED 5 is attached to
const int ledPin6 = 7;      // pin that LED 6 is attached to
const int ledPin7 = 8;      // pin that LED 7 is attached to
const int ledPin8 = 9;      // pin that LED 8 is attached to
const int ledPin9 = 10;     // pin that LED 9 is attached to
const int threshold = 170;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize serial communications:
  Serial.begin(9600);
  // initialize LED pin1 as an output:
  pinMode(ledPin1, OUTPUT);
  // initialize LED pin2 as an output:
  pinMode(ledPin2, OUTPUT);
  // initialize LED pin3 as an output:
  pinMode(ledPin3, OUTPUT);
  // initialize LED pin4 as an output:
  pinMode(ledPin4, OUTPUT);
  // initialize LED pin5 as an output:
  pinMode(ledPin5, OUTPUT);
  // initialize LED pin6 as an output:
  pinMode(ledPin6, OUTPUT);
  // initialize LED pin7 as an output:
  pinMode(ledPin7, OUTPUT);
  // initialize LED pin8 as an output:
  pinMode(ledPin8, OUTPUT);
  // initialize LED pin9 as an output:
  pinMode(ledPin9, OUTPUT);
}

void loop() {
  // read the value of the sensor:
  int analogValue1 = analogRead(analogPin1);
  int analogValue2 = analogRead(analogPin2);
  int analogValue3 = analogRead(analogPin3);
  int analogValue4 = analogRead(analogPin4);
  int analogValue5 = analogRead(analogPin5);
  int analogValue6 = analogRead(analogPin6);
  int analogValue7 = analogRead(analogPin7);
  int analogValue8 = analogRead(analogPin8);
  int analogValue9 = analogRead(analogPin9);

  // if the analog value is high enough, turn on LED1:
  if (analogValue1 > threshold) {
    digitalWrite(ledPin1, HIGH);
  } else {
    digitalWrite(ledPin1, LOW);
  }
  // if the analog value is high enough, turn on LED2:
  if (analogValue2 > threshold) {
    digitalWrite(ledPin2, HIGH);
  } else {
    digitalWrite(ledPin2, LOW);
  }
  // if the analog value is high enough, turn on LED3:
  if (analogValue3 > threshold) {
    digitalWrite(ledPin3, HIGH);
  } else {
    digitalWrite(ledPin3, LOW);
  }
  // if the analog value is high enough, turn on LED4:
  if (analogValue4 > threshold) {
    digitalWrite(ledPin4, HIGH);
  } else {
    digitalWrite(ledPin4, LOW);
  }
  // if the analog value is high enough, turn on LED5:
  if (analogValue5 > threshold) {
    digitalWrite(ledPin5, HIGH);
  } else {
    digitalWrite(ledPin5, LOW);
  }
  // if the analog value is high enough, turn on LED6:
  if (analogValue6 > threshold) {
    digitalWrite(ledPin6, HIGH);
  } else {
    digitalWrite(ledPin6, LOW);
  }
  // if the analog value is high enough, turn on LED7:
  if (analogValue7 > threshold) {
    digitalWrite(ledPin7, HIGH);
  } else {
    digitalWrite(ledPin7, LOW);
  }
  // if the analog value is high enough, turn on LED8:
  if (analogValue8 > threshold) {
    digitalWrite(ledPin8, HIGH);
  } else {
    digitalWrite(ledPin8, LOW);
  }
  // if the analog value is high enough, turn on LED9:
  if (analogValue9 > threshold) {
    digitalWrite(ledPin9, HIGH);
  } else {
    digitalWrite(ledPin9, LOW);
  }

  // print the analog value:
  Serial.println(analogValue1);
  Serial.println(analogValue2);
  Serial.println(analogValue3);
  Serial.println(analogValue4);
  Serial.println(analogValue5);
  Serial.println(analogValue6);
  Serial.println(analogValue7);
  Serial.println(analogValue8);
  Serial.println(analogValue9);
  // delay in between reads for stability
  delay(100);
}

Read up on arrays.

Do you need to do that?

If so why not name the pins something useful?

Such as this const int analogPin9 = A9; // pin that sensor 9 is attached to to const int TheSensorFromThor = A9; // pin that sensor 9 is attached to``` because A9 already means Analog pin 9.

Have you heard about arrays?

I have heard of arrays, but currently do not have enough knowledge to use them which is why I posted. I would love to see an example of the code I posted using an array.

Wow. Cool. So there is thing, an internet searchie thingy, where you can type in words like "arduino gpio array" and you'll like get these things, results, that you can click on. And those things that were clicked on can give other results like this, array - Arduino Reference where you can get examples of things you love to see.

I don't know that the sarcasm was necessary. I can't imagine that is how you interact socially.

Anyway, I imagine this would be my new setup code:

  for (int thisPin = 2; thisPin < 11; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }

How would I call on the array here?:

  // if the analog value is high enough, turn on LED1:
  if (analogValue1 > threshold) {
    digitalWrite(ledPin1, HIGH);
  } else {
    digitalWrite(ledPin1, LOW);
  }

Its pretty simple, add indices and loop over all the entries with for loops - there are other syntaxes in C++ too, but walk before you can run :slight_smile:

const byte pin_count = 9 ;

const byte analogPins [pin_count] = {A1, A2, A3, A4, A5, A6, A7, A8, A9};
const byte ledPins [pin_count] = {2,3,4,5,6,7,8,9,10} ;

const int threshold = 170;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  Serial.begin(9600);
  for (byte i = 0 ; i < pin_count ; i++)
    pinMode (ledPins[i], OUTPUT) ;
}

void loop() {
  for (byte i = 0 ; i < pin_count ; i++)
  {
    int analogValue = analogRead (analogPins[i]);
    // if the analog value is high enough, turn on LED
    digitalWrite (ledPins[i], analogValue > threshold) ;
    Serial.println (analogValue);
  }
  Serial.println ();  // group output so can figure out which is which.
  delay(100);
}

I've removed some of the comments that are unnecessary repetition of the code - no need to explain what Serial.begin() does, it describes itself. BTW 9600 is glacially slow for a serial baud rate, I'd start using 115200 from the get-go, much less likely for your sketches to become I/O bound.

Comment when the intention isn't necessarily clear from the code, good code should be easy to read as a human if possible.

I was not being sarcastic. Most often using these micro controllers requires research. Also, instead and waiting for an answer, which may not come in a timely fashion, one could find doings one's own research can obtain an answer far quicker then waiting.