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);
}